PHPackages                             sokil/php-image - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Image &amp; Media](/categories/media)
4. /
5. sokil/php-image

ActiveLibrary[Image &amp; Media](/categories/media)

sokil/php-image
===============

Realisation of some operations with images like croping and scaling.

1.0.0(5y ago)51694[2 issues](https://github.com/sokil/php-image/issues)MITPHPPHP &gt;=7.2

Since Oct 8Pushed 5y ago3 watchersCompare

[ Source](https://github.com/sokil/php-image)[ Packagist](https://packagist.org/packages/sokil/php-image)[ RSS](/packages/sokil-php-image/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (8)Dependencies (2)Versions (9)Used By (0)

php-image
=========

[](#php-image)

[![Build Status](https://camo.githubusercontent.com/ea07239ba32ab5ac25fc2dffcd53337f4324c2943630925c2ddb2b8457746fc1/68747470733a2f2f7472617669732d63692e6f72672f736f6b696c2f7068702d696d6167652e706e673f6272616e63683d6d61737465722631)](https://travis-ci.org/sokil/php-mongo)[![Latest Stable Version](https://camo.githubusercontent.com/72b2b91280d3a0f3a92c0041adbf03181110c131fd6c14648223922e52c2b5e3/68747470733a2f2f706f7365722e707567782e6f72672f736f6b696c2f7068702d696d6167652f762f737461626c652e706e67)](https://packagist.org/packages/sokil/php-image)[![Coverage Status](https://camo.githubusercontent.com/4ddff2c474a3270b5d9ea26a7de9e3263786af19fcccd328011e851a9810824e/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f736f6b696c2f7068702d696d6167652f62616467652e706e673f6272616e63683d6d6173746572)](https://coveralls.io/r/sokil/php-image?branch=master)[![Gitter](https://camo.githubusercontent.com/7166d62bfff304085d72e8e42d984583de58b15c6ae8b832ae89ecd43d7ca158/68747470733a2f2f6261646765732e6769747465722e696d2f4a6f696e436861742e737667)](https://gitter.im/sokil/php-image?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

- [Installation](#installation)
- [Open image](#open-image)
- [Resize image](#resize-image)
- [Crop image](#crop-image)
- [Rotate image](#rotate-image)
- [Flip image](#flip-image)
- [Filters](#filters)
- [Image elements](#image-elements)
    - [Adding elements to image](#adding-elements-to-image)
    - [Writing text](#writing-text)
- [Save image](#save-image)

Installation
------------

[](#installation)

You may install library through composer:

```
composer require sokil/php-image

```

Open image
----------

[](#open-image)

Create instance of image:

```
$image = new \Sokil\Image\Image($pathToImage);
```

Factory incapsulates instantiating of all image objects and allow to confirure created images:

```
$factory = new \Sokil\Image\Factory;
```

Opening from filename:

```
$factory->openImage('/path/to/image.jpeg');
```

Opening from GD resource:

```
$factory->openImage($imageResource);
```

Creating new image:

```
$image = $factory->createImage(300, 200);

```

Resize image
------------

[](#resize-image)

There is four resize modes: 'scale', 'fit', 'crop' and 'cache'.

```
$newImage = $factory->resizeImage($image, $mode, $width, $height);
```

If you want to register own resize strategy, extend class from \\Sokil\\Image\\AbstractResizeStrategy and add namespase:

```
// through factory constructor
$factory = new \Sokil\Image\Factory([
    'namespace' => [
        'resize' => '\Vendor\ResizeStrategy',
    ],
]);
// through factory method
$factory->addResizeStrategyNamespace('\Vendor\ResizeStrategy');
// directly to image
$image->addResizeStrategyNamespace('\Vendor\ResizeStrategy');
```

Classes searches in priority of adding.

Crop image
----------

[](#crop-image)

To get part of image by specified wifth and height and in defined coordinates use:

```
$x = 10;
$y = 10;
$width = 20;
$height = 20;

$image->crop($x, $y, $width, $height);
```

Rotate image
------------

[](#rotate-image)

Rotating is counter clockwise;

Rotate on 90 degrees:

```
$image->rotate(90);
```

Rotate on 45 degrees, and fill empty field with black color:

```
$image->rotate(45, '#000000');
```

Rotate on 45 degrees, and fill empty field with transparent green color:

```
$image->rotate(45, '#8000FF00');
```

Flip image
----------

[](#flip-image)

Flip in vertical direction:

```
$image->flipVertical();
```

Flip in horisontal direction

```
$image->flipHorisontal();
```

Flip in both directions

```
$image->flipBoth();
```

Filters
-------

[](#filters)

Greyscale image:

```
$factory->filterImage($image, 'greyscale');
```

If you want to register own filter strategy to support new filters, extend class from \\Sokil\\Image\\AbstractFilterStrategy and add namespase:

```
// through factory constructor
$factory = new \Sokil\Image\Factory([
    'namespace' => [
        'filter' => '\Vendor\FilterStrategy',
    ],
]);
// through factory method
$factory->addFilterStrategyNamespace('\Vendor\FilterStrategy');
// or directly to image
$image->addFilterStrategyNamespace('\Vendor\FilterStrategy');
```

Classes searches in priority of adding.

Image elements
--------------

[](#image-elements)

### Adding elements to image

[](#adding-elements-to-image)

Element is everything that can me append to image: text, shape, other image. First we need to create element instabce and configure it:

```
$someElement = $factory->createElement('someElement')->setParam1('someValue');
```

Than element placed to image to some coordinates:

```
$image->appendElementAtPosition($someElement, 30, 30);
```

You can create your own elements that inherits \\Sokil\\Image\\AbstractElement class, and register namespace:

```
namespace Vendor\Elements;

class Circle extends \Sokil\Image\AbstractElement
{
    public function setRadius($r) { // code to set radius }

    public function draw($resource, $x, $y)
    {
        // code to draw circle on image $resouce at coordinates ($x, $y)
    }
}

// through factory constructor
$factory = new \Sokil\Image\Factory([
    'namespace' => [
        'element' => '\Vendor\Element',
    ],
]);
// through factory method
$factory->addElementNamespace('\Vendor\Elements');
```

Now you can draw your own circles:

```
$circle = $factory->createElement('circle')->setRadiud(100);
$image->appendElementAtPosition($circle, 100, 100);
```

### Writing text

[](#writing-text)

To create text element you can use one of methods:

```
$textElement = $factory->createElement('text');
// or through helper
$textElement = $factory->createTextElement();
```

First we need to configure text element:

```
$factory = new \Sokil\Image\Factory();

// text element
$element = $factory
    ->createTextElement()
    ->setText('hello world')
    ->setAngle(20)
    ->setSize(40)
    ->setColor('#ababab')
    ->setFont(__DIR__ . '/FreeSerif.ttf');
```

Now we need to place element in image at some coordinates:

```
$image->appendElementAtPosition($element, 50, 150);
```

Save image
----------

[](#save-image)

Library supports three formats of image: 'jpeg', 'png' and 'gif'.

To write image to disk you must define format of image and configure write strategy:

```
$factory->writeImage($image, 'jpeg', function(\Sokil\Image\WriteStrategy\JpegWriteStrategy $strategy) {
    $strategy->setQuality(98)->toFile('/path/to/file.jpg');
});
```

To send image to STDOUT you must define format of image and configure write strategy:

```
$factory->writeImage($image, 'jpeg', function(\Sokil\Image\WriteStrategy\JpegWriteStrategy $strategy) {
    $strategy->setQuality(98)->toStdout();
});
```

If you want to register own write strategy to support new image format, extend class from \\Sokil\\Image\\AbstractWriteStrategy and add namespase:

```
// through factory constructor
$factory = new \Sokil\Image\Factory([
    'namespace' => [
        'write' => '\Vendor\WriteStrategy',
    ],
]);
// through factory method
$factory->addWriteStrategyNamespace('\Vendor\WriteStrategy');
// or directly to image
$image->addWriteStrategyNamespace('\Vendor\WriteStrategy');
```

Classes searches in priority of adding.

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance12

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~309 days

Recently: every ~541 days

Total

8

Last Release

2109d ago

Major Versions

0.8.3 → 1.0.02020-09-12

PHP version history (2 changes)0.5PHP &gt;=5.3

1.0.0PHP &gt;=7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/902e21ecf6517341b1f2a3c2f93a3eb115396fc6524effaeabc816b481909e64?d=identicon)[sokil](/maintainers/sokil)

---

Top Contributors

[![sokil](https://avatars.githubusercontent.com/u/1829948?v=4)](https://github.com/sokil "sokil (111 commits)")

---

Tags

imageimage-processingphp

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/sokil-php-image/health.svg)

```
[![Health](https://phpackages.com/badges/sokil-php-image/health.svg)](https://phpackages.com/packages/sokil-php-image)
```

###  Alternatives

[char0n/ffmpeg-php

PHP wrapper for FFmpeg application

495236.3k1](/packages/char0n-ffmpeg-php)[goat1000/svggraph

Generates SVG graphs

133890.0k3](/packages/goat1000-svggraph)[imagekit/imagekit

PHP library for Imagekit

46877.3k10](/packages/imagekit-imagekit)[gravatarphp/gravatar

Gravatar URL builder which is most commonly called as a Gravatar library

12644.1k2](/packages/gravatarphp-gravatar)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
