PHPackages                             imagecow/imagecow - 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. imagecow/imagecow

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

imagecow/imagecow
=================

PHP library to manipulate and generate responsive images

v2.4.1(7y ago)245327.8k↑44.4%29[6 issues](https://github.com/oscarotero/imagecow/issues)13MITPHPPHP &gt;=5.5

Since Dec 4Pushed 4y ago11 watchersCompare

[ Source](https://github.com/oscarotero/imagecow)[ Packagist](https://packagist.org/packages/imagecow/imagecow)[ Docs](https://github.com/oscarotero/imagecow/)[ RSS](/packages/imagecow-imagecow/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (37)Used By (13)

Imagecow
========

[](#imagecow)

[![Build Status](https://camo.githubusercontent.com/9a6777b13529f55305a1034b25a52951020f59045279c7f95785ad6538f4200c/68747470733a2f2f7472617669732d63692e6f72672f6f736361726f7465726f2f696d616765636f772e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/oscarotero/imagecow)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/ee7043263e6c1e7243d31ebb56c8a48cae79145f364b058a7e41b407e3c320f2/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6f736361726f7465726f2f696d616765636f772f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/oscarotero/imagecow/?branch=master)

Created by Oscar Otero

What is Imagecow?
-----------------

[](#what-is-imagecow)

It's a php library to manipulate images to web.

- PHP &gt;= 5.5
- Use GD2 or Imagick libraries
- Very simple, fast and easy to use. There is not a lot of features, just the basics: crop, resize, resizeCrop, etc.

Simple usage example:

```
use Imagecow\Image;

Image::fromFile('my-image.gif')
    ->autoRotate()
    ->resizeCrop(300, 400, 'center', 'middle')
    ->format('png')
    ->save('converted-image.png')
    ->show();
```

How use it?
-----------

[](#how-use-it)

### Installation

[](#installation)

This package is installable and autoloadable via Composer as [imagecow/imagecow](https://packagist.org/packages/imagecow/imagecow).

```
$ composer require imagecow/imagecow
```

### Creating a Imagecow\\Image instance:

[](#creating-a-imagecowimage-instance)

```
use Imagecow\Image;

//Using Imagick:
$image = Image::fromFile('my-image.jpg', Image::LIB_IMAGICK);

//Detect the available library automatically
//(in order of preference: Imagick, Gd)
$image = Image::fromFile('my-image.jpg');

//Create an instance from a string
$image = Image::fromString(file_get_contents('my-image.jpg'));
```

### resize

[](#resize)

`Image::resize($width, $height = 0, $cover = false)`

Resizes the image keeping the aspect ratio.

**Note:** If the new image is bigger than the original, the image wont be resized

- `$width`: The new max-width of the image. You can use percentages or numbers (pixels). If it's `0`, it will be calculated automatically using the height
- `$height`: The new max-height of the image. As width, you can use percentages or numbers and it will be calculated automatically if it's `0`
- `$cover`: If it's `true`, the new dimensions will cover both width and height values. It's like css's `image-size: cover`.

```
//Assuming the original image is 1000x500

$image->resize(200);                    // change to 200x100
$image->resize(0, 200);                 // change to 400x200
$image->resize(200, 300);               // change to 200x100
$image->resize(2000, 2000);             // keeps 1000x500
```

### crop

[](#crop)

`Image::crop($width, $height, $x = 'center', $y = 'middle')`

Crops the image:

- `$width`: The width of the cropped image. It can be number (pixels) or percentage
- `$height`: The height of the cropped image. It can be number (pixels) or percentage
- `$x`: The horizontal offset of the crop. It can be a number (for pixels) or percentage. You can also use the keywords `left`, `center` and `right`. If it's not defined, used the value by default (`center`).
- `$y`: The vertical offset of the crop. As with $x, it can be a number or percentage. You can also use the keywords `top`, `middle` and `bottom`. If it's not defined, used the value by default (`middle`).

```
$image->crop(200, 300);                 // crops to 200x300px
$image->crop(200, 300, 'left', 'top');  // crops to 200x300px from left and top
$image->crop(200, 300, 20, '50%');      // crops to 200x300px from 20px left and 50% top
$image->crop('50%', '50%');             // crops to half size
```

#### Automatic cropping

[](#automatic-cropping)

Imagecow includes some code copied from the great library [stojg/crop](https://github.com/stojg/crop) to calculate the most important parts of the image to crop and resizeCrop automatically. The available methods are:

- `Image::CROP_ENTROPY` [more info](https://github.com/stojg/crop#cropentropy)
- `Image::CROP_BALANCED` [more info](https://github.com/stojg/crop#cropbalanced)

Note: **these methods are available only for Imagick**. If you use Gd, the methods fallback to "center", "middle" positions.

To use them:

```
$image->crop(500, 200, Image::CROP_ENTROPY);  // crops to 500x200 using the Entropy method to calculate the center point
$image->crop(500, 200, Image::CROP_BALANCED); // The same as above but using the Balanced method
```

### resizeCrop

[](#resizecrop)

`Image::resizeCrop($width, $height, $x = 'center', $y = 'middle')`

Resizes and crops the image. See [resize](resize) and [crop](crop) for the arguments description.

```
$image->resizeCrop(200, 300);                  //Resizes and crops to 200x300px.
$image->resizeCrop('50%', 300);                //Resizes and crops to half width and 300px height
$image->resizeCrop(200, 300, 'left', '100%'); //Resizes and crops to 200x300px from left and bottom
$image->resizeCrop(200, 300, Image::CROP_BALANCED); //Resizes and crops to 200x300px using the CROP_BALANCED method
```

### rotate

[](#rotate)

`Image::rotate($angle)`

Rotates the image

- `$angle`: Rotation angle in degrees (anticlockwise)

```
$image->rotate(90); // rotates the image 90 degrees
```

### autoRotate

[](#autorotate)

`Image::autoRotate()`

Autorotates the image according its EXIF data

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

### opacity

[](#opacity)

`Image::opacity($value)`

Set the alpha channel of the image. The value must be between 0 (transparent) to 100 (opaque). Note that the image will be converted to png (if it's not already)

```
$image->opacity(50);
```

### blur

[](#blur)

`Image::blur($loops = 4)`

Applies the gaussian blur to the image. The more loops, the more the image blurs.

```
$image->blur(8);
```

### watermark

[](#watermark)

`Image::watermark($image, $x = 'right', $y = 'bottom')`

Applies a image as a watermark. You can configure the position and opacity.

```
$image = Image::fromFile('photo.jpg');
$logo = Image::fromFile('logo.png');

$logo->opacity(50);

$image->watermark($logo);
```

### format

[](#format)

`Image::format($format)`

Converts the image to other format.

- `$format`: The format name. It can be "jpg", "png", "gif", or "webp"\*.

```
$image->format('png'); // converts to png
```

\*Note: `webp` format is only supported when using Imagick. [ImageMagick must be built with WEBP support](#installing-imagemagick-with-webp-support).

### save

[](#save)

Save the image to a file.

- `$filename`: The filename for the saved image. If it's not defined, overwrite the file (only if has been loaded from a file).

```
$image->save('my-new-image.png'); // save to this file
$image->save(); // overwrite file
```

### setBackground

[](#setbackground)

`Image::setBackground(array $background)`

Set a default background used in some transformations: for example on convert a transparent png to jpg.

- `$background`: An array with the RGB value of the color

```
$image->setBackground(array(255, 255, 255)); // set the background to white
```

### quality

[](#quality)

`Image::quality($quality)`

Defines the image compression quality for jpg images

- `$quality`: An integer value between 0 and 100

```
$image->quality(80); // change the quality to 80
```

### setClientHints

[](#setclienthints)

`Image::setClientHints(array $clientHints)`

Defines the client hints to fix the final size of the image and generate responsive images. The available client hints are:

- `dpr` Device pixel ratio
- `width` The final image width
- `viewport-width` The viewport width

```
$image->setClientHints([
    'dpr' => 2,
    'width' => 300,
    'viewport-width' => 1024,
]);
```

More information about [client hints below](#responsive-images).

### Display the image

[](#display-the-image)

Send the HTTP header with the content-type, output the image data and die:

```
$image->show(); // you should see this image in your browser
```

Insert the image as base64 url:

```
echo '';
```

### Get image info:

[](#get-image-info)

There are other functions to returns image info:

- `$image->getWidth()`: Returns the image width in pixels
- `$image->getHeight()`: Returns the image height in pixels
- `$image->getMimeType()`: Returns the image mime-type
- `$image->getExifData()`: Returns the EXIF data of the image
- `$image->getString()`: Returns a string with the image content

#### Execute multiple functions

[](#execute-multiple-functions)

You can execute some of these functions defined as a string. This is useful to get images transformed dinamically using variables, for example: `image.php?transform=resize,200,300|format,png`. All operations are separated by `|` and use commas for the arguments:

```
$image->transform('resize,200,50%|format,png|crop,100,100,CROP_ENTROPY');

//This is the same than:
$image
	->resize(200, '50%')
	->format('png')
	->crop(100, 100, Image::CROP_ENTROPY);
```

### Responsive images

[](#responsive-images)

Imagecow has support for client hints, that allows to generate responsive images without using cookies or javascript code (like in 1.x version of imagecow). Client Hints is introduced by Google becoming a standard. [Here's a deep explain of how to use it](https://www.smashingmagazine.com/2016/01/leaner-responsive-images-client-hints/)

Note that currently this is supported only by [chrome and opera browsers](http://caniuse.com/#feat=client-hints-dpr-width-viewport).

Simple example:

In your webpage, add the following code:

```
>

    My webpage

```

Now, in the server side:

```
use Imagecow\Image;

$file = __DIR__.'/'.$_GET['file'];
$transform = isset($_GET['transform']) ? $_GET['transform'] : null;

//Create the image instance
$image = Image::fromFile($file);

//Set the client hints
$image->setClientHints([
    'dpr' => isset($_SERVER['HTTP_DPR']) ? $_SERVER['HTTP_DPR'] : null,
    'width' => isset($_SERVER['HTTP_WIDTH']) ? $_SERVER['HTTP_WIDTH'] : null,
    'viewport-width' => isset($_SERVER['HTTP_VIEWPORT_WIDTH']) ? $_SERVER['HTTP_VIEWPORT_WIDTH'] : null,
]);

//Transform the image and display the result:
$image->transform($transform)->show();
```

### Other utils

[](#other-utils)

#### IconExtractor.

[](#iconextractor)

**Only for Imagick**. Class to extract the images from an .ico file and convert to png.

```
use Imagecow\Utils\IconExtractor;

$icon = new IconExtractor('favicon.ico');

//Gets the better image from the icon (quality = color_depth + (width * height))
$image = $icon->getBetterQuality();

//Do imagecow stuff
$image->resize(100)->save('my-image.png');
```

#### SvgExtractor.

[](#svgextractor)

**Only for Imagick** This class allows generate images from a svg file (useful for browsers that don't support svg format):

```
use Imagecow\Utils\SvgExtractor;

$svg = new SvgExtractor('image.svg');

//Gets the image
$image = $svg->get();

//Now you can execute the imagecow methods:
$image->resize(200)->format('jpg')->save('image.jpg');
```

### Installing ImageMagick with WEBP support

[](#installing-imagemagick-with-webp-support)

#### macOS

[](#macos)

Via Homebrew:

```
brew install webp
brew install imagemagick --with-webp
```

#### CentOS/RHEL

[](#centosrhel)

```
yum install libwebp-devel rpm-build
mkdir /tmp/imagemagick
cd /tmp/imagemagick
yum-builddep ImageMagick -y
yumdownloader --source ImageMagick
rpm -ivh ImageMagick*
sed -i '/BuildRequires:\tghostscript-devel/a BuildRequires:\tlibwebp-devel' /root/rpmbuild/SPECS/ImageMagick.spec
sed -i '/Requires: pkgconfig/a Requires: libwebp' /root/rpmbuild/SPECS/ImageMagick.spec
rpmbuild -ba /root/rpmbuild/SPECS/ImageMagick.spec
rpm -Uvh --force /root/rpmbuild/RPMS/x86_64/ImageMagick-*.rpm
yum-config-manager --save --setopt=updates.exclude=ImageMagick*;
```

#### Ubuntu

[](#ubuntu)

```
mkdir /tmp/imagemagick
cd /tmp/imagemagick
apt-get build-dep imagemagick
apt-get install libwebp-dev devscripts
apt-get source imagemagick
cd imagemagick-*
debuild -uc -us
dpkg -i ../*magick*.deb
```

### Maintainers:

[](#maintainers)

- @oscarotero (creator)
- @eusonlito (collaborator)
- [and more...](https://github.com/oscarotero/imagecow/graphs/contributors)

### Thanks to

[](#thanks-to)

Stig Lindqvist and Julien Deniau jdeniau for the [stojg/crop library](https://github.com/stojg/crop)

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity54

Moderate usage in the ecosystem

Community33

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 79.1% 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 ~56 days

Recently: every ~86 days

Total

34

Last Release

2683d ago

Major Versions

v0.6.0 → v1.0.02014-04-19

v1.5.0 → v2.0.02016-01-22

v1.5.1 → v2.0.12016-02-04

v2.4.0 → v3.x-dev2019-01-08

PHP version history (3 changes)v0.4.5PHP &gt;=5.3.0

v2.0.0PHP &gt;=5.5

v3.x-devPHP &gt;=7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/e99df56b617f4af8cf0556a51a0ca20c7420104920c57d7e9eab093f92bb744f?d=identicon)[oscarotero](/maintainers/oscarotero)

---

Top Contributors

[![oscarotero](https://avatars.githubusercontent.com/u/377873?v=4)](https://github.com/oscarotero "oscarotero (170 commits)")[![eusonlito](https://avatars.githubusercontent.com/u/644551?v=4)](https://github.com/eusonlito "eusonlito (24 commits)")[![nysos3](https://avatars.githubusercontent.com/u/1896665?v=4)](https://github.com/nysos3 "nysos3 (5 commits)")[![aemr3](https://avatars.githubusercontent.com/u/6818719?v=4)](https://github.com/aemr3 "aemr3 (5 commits)")[![endelwar](https://avatars.githubusercontent.com/u/28512?v=4)](https://github.com/endelwar "endelwar (4 commits)")[![kevbaldwyn](https://avatars.githubusercontent.com/u/2512883?v=4)](https://github.com/kevbaldwyn "kevbaldwyn (2 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (2 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")[![hrach](https://avatars.githubusercontent.com/u/284263?v=4)](https://github.com/hrach "hrach (1 commits)")[![kuczmaja](https://avatars.githubusercontent.com/u/5076084?v=4)](https://github.com/kuczmaja "kuczmaja (1 commits)")

---

Tags

client-hintsgdimageimagickresponsive-imagesimage

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/imagecow-imagecow/health.svg)

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

###  Alternatives

[intervention/image

PHP Image Processing

14.3k194.3M2.2k](/packages/intervention-image)[league/glide

Wonderfully easy on-demand image manipulation library with an HTTP based API.

2.6k51.2M116](/packages/league-glide)[liip/imagine-bundle

This bundle provides an image manipulation abstraction toolkit for Symfony-based projects.

1.7k38.3M217](/packages/liip-imagine-bundle)[spatie/image

Manipulate images with an expressive API

1.4k54.4M138](/packages/spatie-image)[intervention/image-laravel

Laravel Integration of Intervention Image

1496.5M102](/packages/intervention-image-laravel)[intervention/gif

PHP GIF Encoder/Decoder

5520.3M9](/packages/intervention-gif)

PHPackages © 2026

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