2017 © Pedro Peláez
 

library php-image-resize

PHP class to re-size and scale images

image

eventviva/php-image-resize

PHP class to re-size and scale images

  • Wednesday, October 4, 2017
  • by adityapatadia
  • Repository
  • 32 Watchers
  • 425 Stars
  • 372,750 Installations
  • PHP
  • 14 Dependents
  • 2 Suggesters
  • 154 Forks
  • 3 Open issues
  • 26 Versions
  • 10 % Grown

The README.md

php-image-resize

PHP library to resize, scale and crop images., (*1)

Build Status Latest Stable Version Monthly Downloads Coverage Status, (*2)

Cloud Solution

If you don't want to crop, resize and store images on your server, Gumlet.com is a free service which can process images in real-time and serve worldwide through CDN., (*3)


Setup

This package is available through Packagist with the vendor and package identifier the same as this repo., (*4)

If using Composer, in your composer.json file add:, (*5)

{
    "require": {
        "gumlet/php-image-resize": "2.0.*"
    }
}

If you are still using PHP 5.3, please install version 1.7.0 and if you are using PHP 5.4, please install version 1.8.0 of this library., (*6)

WebP support is added with PHP 5.6.0 and current version of library supports that. If you are facing issues, please use 1.9.2 version of this library., (*7)

For PHP versions >= 7.2, 2.0.1 or above version of this library should be used., (*8)

Otherwise:, (*9)

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

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

$image = new \Gumlet\ImageResize();

Or alias it:, (*11)


use \Gumlet\ImageResize; $image = new ImageResize();

Note: This library uses GD class which do not support resizing animated gif files, (*12)


Resize

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

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

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

$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):, (*15)

$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):, (*16)

$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:, (*17)

$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:, (*18)

$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., (*19)

Crop

To to crop an image:, (*20)

$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., (*21)

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., (*22)

Crop modes:, (*23)

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:, (*24)

$image = new ImageResize('image.jpg');
$image->crop(200, 200);
$image->save('image2.jpg');
$image = new ImageResize('image.jpg');
$image->crop(200, 200, true, 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., (*25)

For instance passing the crop mode CROPTOP will result as 100px taken off the bottom leaving you with 200px × 200px., (*26)

$image = new ImageResize('image.jpg');
$image->crop(200, 200, true, 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., (*27)

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

Freecrop:, (*28)

There is also a way to define custom crop position. You can define $x and $y in freecrop method:, (*29)

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

Loading and saving images from string

To load an image from a string:, (*30)

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

You can also return the result as a string:, (*31)

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

Magic __toString() is also supported:, (*32)

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

Displaying

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

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

Image Types

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

If you would like to save/output in a different image type, you need to pass a (supported) PHP IMAGETYPE_* constant:, (*36)

  • IMAGETYPE_GIF
  • IMAGETYPE_JPEG
  • IMAGETYPE_PNG

This allows you to save in a different type to the source:, (*37)

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

Quality

The properties $quality_jpg, $quality_webp and $quality_png are available for you to configure:, (*38)

$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() and imagepng() for more info., (*39)

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

$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., (*41)

Interlacing

By default, image interlacing is turned on. It can be disabled by setting $interlace to 0:, (*42)

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

Chaining

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

This is useful for creating multiple sizes:, (*44)

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

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

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

Exceptions

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

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 (http://php.net/manual/en/ref.image.php), (*46)

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

Filters

You can apply special effects for new image like blur or add banner., (*47)

$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

Flips an image using a given mode and this method is only for PHP version 5.4., (*48)

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

$image->addFilter(function ($image) {
    imageflip($image, IMG_FLIP_HORIZONTAL);
});

Both functions will be used in the order in which they were added., (*49)

Gamma color correction

You can enable the gamma color correction which is disabled by default., (*50)

$image = new ImageResize('image.png');
$image->gamma(true);

API Doc

https://gumlet.github.io/php-image-resize/index.html, (*51)


Maintainer

This library is maintained by Gumlet.com, (*52)

, (*53)

The Versions

04/10 2017

dev-master

9999999-dev https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *

 

php image resize scale

09/09 2017

1.6.8

1.6.8.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *

 

php image resize scale

02/08 2017

1.6.7

1.6.7.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *

 

php image resize scale

11/07 2017

1.6.6

1.6.6.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *

 

php image resize scale

12/05 2017

1.6.5

1.6.5.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *

 

php image resize scale

20/04 2017

1.6.4

1.6.4.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *

 

php image resize scale

13/03 2017

1.6.3

1.6.3.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *
  • ext-exif *

 

php image resize scale

19/01 2017

1.6.2

1.6.2.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *
  • ext-exif *

 

php image resize scale

14/10 2016

1.6.1

1.6.1.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *
  • ext-exif *

 

php image resize scale

27/09 2016

1.6.0

1.6.0.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *
  • ext-exif *

 

php image resize scale

17/08 2016

1.5.5

1.5.5.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *
  • ext-exif *

 

php image resize scale

06/05 2016

1.5.4

1.5.4.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *

 

php image resize scale

06/03 2016

1.5.3

1.5.3.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *

 

php image resize scale

06/01 2016

1.5.2

1.5.2.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *

 

php image resize scale

06/01 2016

1.5.1

1.5.1.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *

 

php image resize scale

23/06 2015

1.5.0

1.5.0.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *

 

php image resize scale

24/03 2015

1.4.0

1.4.0.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *

 

php image resize scale

23/02 2015

1.3.0

1.3.0.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *

 

php image resize scale

25/08 2014

1.2.3

1.2.3.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *

 

php image resize scale

01/08 2014

1.2.2

1.2.2.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *

 

php image resize scale

04/07 2014

1.2.1

1.2.1.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *

 

php image resize scale

04/07 2014

1.2.0

1.2.0.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *

 

php image resize scale

03/07 2014

1.1.2

1.1.2.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *

 

php image resize scale

02/07 2014

1.1.1

1.1.1.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *

 

php image resize scale

25/06 2014

1.1.0

1.1.0.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *

 

php image resize scale

13/03 2014

1.0.0

1.0.0.0 https://github.com/eventviva/php-image-resize

PHP class to re-size and scale images

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-gd *

 

php image resize scale