PHPackages                             salamzadeh/php-image-resize - 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. salamzadeh/php-image-resize

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

salamzadeh/php-image-resize
===========================

PHP class to re-size and scale images

01PHP

Since Oct 19Pushed 4y ago1 watchersCompare

[ Source](https://github.com/salamzadeh/php-image-resize)[ Packagist](https://packagist.org/packages/salamzadeh/php-image-resize)[ RSS](/packages/salamzadeh-php-image-resize/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

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

[](#php-image-resize)

PHP library to resize, scale and crop images.

[![Build Status](https://camo.githubusercontent.com/f4178afa44b4a9e899dfdac47e62f24a0d80141f5c03ba625e5d9b45ef4aca8b/68747470733a2f2f7472617669732d63692e6f72672f73616c616d7a616465682f7068702d696d6167652d726573697a652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/salamzadeh/php-image-resize) [![Latest Stable Version](https://camo.githubusercontent.com/43fd4f92fb8f4068667f702a46d4196ad21bf98225b5a0bd79a72d0f858a6893/68747470733a2f2f706f7365722e707567782e6f72672f73616c616d7a616465682f7068702d696d6167652d726573697a652f762f737461626c65)](https://packagist.org/packages/salamzadeh/php-image-resize) [![Monthly Downloads](https://camo.githubusercontent.com/eccd1b7368766d64fc9e771cf029615c0ba35417dde96ac10d8ba86593290f30/68747470733a2f2f706f7365722e707567782e6f72672f73616c616d7a616465682f7068702d696d6167652d726573697a652f642f6d6f6e74686c79)](https://packagist.org/packages/salamzadeh/php-image-resize)

Hosted Solution
---------------

[](#hosted-solution)

If you don't want to crop, resize and store images on your server, [Salamzadeh.net](https://www.salamzadeh.net) is a **free** service which can process images in real-time and serve worldwide through CDN.

---

Setup
-----

[](#setup)

This package is available through Packagist with the vendor and package identifier the same as this repo.

If using [Composer](https://getcomposer.org/), in your `composer.json` file add:

```
{
    "require": {
        "salamzadeh/php-image-resize": "1.8.*"
    }
}
```

If you are still using PHP 5.3, please install version `1.7.0` or below of this library.

Otherwise:

```
include '/path/to/ImageResize.php';
```

Because this class uses namespacing, when instantiating the object, you need to either use the fully qualified namespace:

```
$image = new \Salamzadeh\ImageResize();
```

Or alias it:

```
use \Salamzadeh\ImageResize;

$image = new ImageResize();
```

> Note: This library uses GD class which do not support resizing animated gif files

---

Resize
------

[](#resize)

To scale an image, in this case to half it's size (scaling is percentage based):

```
$image = new ImageResize('image.jpg');
$image->scale(50);
$image->save('image2.jpg')
```

To resize an image according to one dimension (keeping aspect ratio):

```
$image = new ImageResize('image.jpg');
$image->resizeToHeight(500);
$image->save('image2.jpg');

$image = new ImageResize('image.jpg');
$image->resizeToWidth(300);
$image->save('image2.jpg');
```

To resize an image according to a given measure regardingless its orientation (keeping aspect ratio):

```
$image = new ImageResize('image.jpg');
$image->resizeToLongSide(500);
$image->save('image2.jpg');

$image = new ImageResize('image.jpg');
$image->resizeToShortSide(300);
$image->save('image2.jpg');
```

To resize an image to best fit a given set of dimensions (keeping aspet ratio):

```
$image = new ImageResize('image.jpg');
$image->resizeToBestFit(500, 300);
$image->save('image2.jpg');
```

All resize functions have `$allow_enlarge` option which is set to false by default. You can enable by passing `true` to any resize function:

```
$image = new ImageResize('image.jpg');
$image->resize(500, 300, $allow_enlarge = True);
$image->save('image2.jpg');
```

If you are happy to handle aspect ratios yourself, you can resize directly:

```
$image = new ImageResize('image.jpg');
$image->resize(800, 600);
$image->save('image2.jpg');
```

This will cause your image to skew if you do not use the same width/height ratio as the source image.

Crop
----

[](#crop)

To to crop an image:

```
$image = new ImageResize('image.jpg');
$image->crop(200, 200);
$image->save('image2.jpg');
```

This will scale the image to as close as it can to the passed dimensions, and then crop and center the rest.

In the case of the example above, an image of 400px × 600px will be resized down to 200px × 300px, and then 50px will be taken off the top and bottom, leaving you with 200px × 200px.

Crop modes:

Few crop mode options are available in order for you to choose how you want to handle the eventual exceeding width or height after resizing down your image. The default crop mode used is the `CROPCENTER`. As a result those pieces of code are equivalent:

```
$image = new ImageResize('image.jpg');
$image->crop(200, 200);
$image->save('image2.jpg');
```

```
$image = new ImageResize('image.jpg');
$image->crop(200, 200, ImageResize::CROPCENTER);
$image->save('image2.jpg');
```

In the case you have an image of 400px × 600px and you want to crop it to 200px × 200px the image will be resized down to 200px × 300px, then you can indicate how you want to handle those 100px exceeding passing the value of the crop mode you want to use.

For instance passing the crop mode `CROPTOP` will result as 100px taken off the bottom leaving you with 200px × 200px.

```
$image = new ImageResize('image.jpg');
$image->crop(200, 200, ImageResize::CROPTOP);
$image->save('image2.jpg');
```

On the contrary passing the crop mode `CROPBOTTOM` will result as 100px taken off the top leaving you with 200px × 200px.

```
$image = new ImageResize('image.jpg');
$image->crop(200, 200, ImageResize::CROPBOTTOM);
$image->save('image2.jpg');
```

Freecrop:

There is also a way to define custom crop position. You can define $x and $y in `freecrop` method:

```
$image = new ImageResize('image.jpg');
$image->freecrop(200, 200, $x =  20, $y = 20);
$image->save('image2.jpg');
```

Loading and saving images from string
-------------------------------------

[](#loading-and-saving-images-from-string)

To load an image from a string:

```
$image = ImageResize::createFromString(base64_decode('R0lGODlhAQABAIAAAAQCBP///yH5BAEAAAEALAAAAAABAAEAAAICRAEAOw=='));
$image->scale(50);
$image->save('image.jpg');
```

You can also return the result as a string:

```
$image = ImageResize::createFromString(base64_decode('R0lGODlhAQABAIAAAAQCBP///yH5BAEAAAEALAAAAAABAAEAAAICRAEAOw=='));
$image->scale(50);
echo $image->getImageAsString();
```

Magic `__toString()` is also supported:

```
$image = ImageResize::createFromString(base64_decode('R0lGODlhAQABAIAAAAQCBP///yH5BAEAAAEALAAAAAABAAEAAAICRAEAOw=='));
$image->resize(10, 10);
echo (string)$image;
```

Displaying
----------

[](#displaying)

As seen above, you can call `$image->save('image.jpg');`

To render the image directly into the browser, you can call `$image->output()`;

Image Types
-----------

[](#image-types)

When saving to disk or outputting into the browser, the script assumes the same output type as input.

If you would like to save/output in a different image type, you need to pass a (supported) PHP [`IMAGETYPE_`\* constant](http://www.php.net/manual/en/image.constants.php):

- `IMAGETYPE_GIF`
- `IMAGETYPE_JPEG`
- `IMAGETYPE_PNG`

This allows you to save in a different type to the source:

```
$image = new ImageResize('image.jpg');
$image->resize(800, 600);
$image->save('image.png', IMAGETYPE_PNG);
```

Quality
-------

[](#quality)

The properties `$quality_jpg` and `$quality_png` are available for you to configure:

```
$image = new ImageResize('image.jpg');
$image->quality_jpg = 100;
$image->resize(800, 600);
$image->save('image2.jpg');
```

By default they are set to 85 and 6 respectively. See the manual entries for [`imagejpeg()`](http://www.php.net/manual/en/function.imagejpeg.php) and [`imagepng()`](http://www.php.net/manual/en/function.imagepng.php) for more info.

You can also pass the quality directly to the `save()`, `output()` and `getImageAsString()` methods:

```
$image = new ImageResize('image.jpg');
$image->crop(200, 200);
$image->save('image2.jpg', null, 100);

$image = new ImageResize('image.jpg');
$image->resizeToWidth(300);
$image->output(IMAGETYPE_PNG, 4);

$image = new ImageResize('image.jpg');
$image->scale(50);
$result = $image->getImageAsString(IMAGETYPE_PNG, 4);
```

We're passing `null` for the image type in the example above to skip over it and provide the quality. In this case, the image type is assumed to be the same as the input.

Interlacing
-----------

[](#interlacing)

By default, [image interlacing](http://php.net/manual/en/function.imageinterlace.php) is turned on. It can be disabled by setting `$interlace` to `0`:

```
$image = new ImageResize('image.jpg');
$image->scale(50);
$image->interlace = 0;
$image->save('image2.jpg');
```

Chaining
--------

[](#chaining)

When performing operations, the original image is retained, so that you can chain operations without excessive destruction.

This is useful for creating multiple sizes:

```
$image = new ImageResize('image.jpg');
$image
    ->scale(50)
    ->save('image2.jpg')

    ->resizeToWidth(300)
    ->save('image3.jpg')

    ->crop(100, 100)
    ->save('image4.jpg')
;
```

Exceptions
----------

[](#exceptions)

ImageResize throws ImageResizeException for it's own for errors. You can catch that or catch the general \\Exception which it's extending.

It is not to be expected, but should anything go horribly wrong mid way then notice or warning Errors could be shown from the PHP GD and Image Functions ()

```
try{
    $image = new ImageResize(null);
    echo "This line will not be printed";
} catch (ImageResizeException $e) {
    echo "Something went wrong" . $e->getMessage();
}
```

Filters
-------

[](#filters)

You can apply special effects for new image like blur or add banner.

```
$image = new ImageResize('image.jpg');

// Add blure
$image->addFilter(function ($imageDesc) {
    imagefilter($imageDesc, IMG_FILTER_GAUSSIAN_BLUR);
});

// Add banner on bottom left corner
$image18Plus = 'banner.png'
$image->addFilter(function ($imageDesc) use ($image18Plus) {
    $logo = imagecreatefrompng($image18Plus);
    $logo_width = imagesx($logo);
    $logo_height = imagesy($logo);
    $image_width = imagesx($imageDesc);
    $image_height = imagesy($imageDesc);
    $image_x = $image_width - $logo_width - 10;
    $image_y = $image_height - $logo_height - 10;
    imagecopy($imageDesc, $logo, $image_x, $image_y, 0, 0, $logo_width, $logo_height);
});
```

Flip
----

[](#flip)

Flips an image using a given mode and this method is only for PHP version 5.4.

```
$flip = new ImageResize('image.png');
$image = imagecreatetruecolor(200, 100);

$flip->imageFlip($image, 0);
```

Both functions will be used in the order in which they were added.

API Doc
-------

[](#api-doc)

As soon as possible ;-)

---

Maintainer
----------

[](#maintainer)

This library is maintained by [Salamzadeh.net](https://www.salamzadeh.net)

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity1

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity28

Early-stage or recently created project

 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.

### Community

Maintainers

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

---

Top Contributors

[![salamzadeh](https://avatars.githubusercontent.com/u/4796598?v=4)](https://github.com/salamzadeh "salamzadeh (1 commits)")

### Embed Badge

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

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

###  Alternatives

[milon/barcode

Barcode generator like Qr Code, PDF417, C39, C39+, C39E, C39E+, C93, S25, S25+, I25, I25+, C128, C128A, C128B, C128C, 2-Digits UPC-Based Extention, 5-Digits UPC-Based Extention, EAN 8, EAN 13, UPC-A, UPC-E, MSI (Variation of Plessey code)

1.5k13.3M39](/packages/milon-barcode)[bkwld/croppa

Image thumbnail creation through specially formatted URLs for Laravel

510496.0k23](/packages/bkwld-croppa)[char0n/ffmpeg-php

PHP wrapper for FFmpeg application

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

Generates SVG graphs

132849.6k3](/packages/goat1000-svggraph)[cohensive/embed

Media Embed (for Laravel or as a standalone).

120370.4k](/packages/cohensive-embed)[netresearch/rte-ckeditor-image

Image support in CKEditor for the TYPO3 ecosystem - by Netresearch

63991.3k4](/packages/netresearch-rte-ckeditor-image)

PHPackages © 2026

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