.gitignore 0000644 00000000032 15025006061 0006522 0 ustar 00 .idea
vendor
composer.lock composer.json 0000644 00000001663 15025006061 0007267 0 ustar 00 {
"name": "gumlet/php-image-resize",
"description": "PHP class to re-size and scale images",
"keywords": ["php", "image", "resize", "scale"],
"type": "library",
"homepage": "https://github.com/gumlet/php-image-resize",
"license": "MIT",
"authors": [
{
"name": "Aditya Patadia",
"homepage": "http://aditya.patadia.org/"
}],
"require":
{
"php": ">=5.6.0",
"ext-gd": "*",
"ext-fileinfo": "*"
},
"suggest":
{
"ext-exif": "Auto-rotate jpeg files"
},
"autoload":
{
"psr-4":
{
"Gumlet\\": "lib/"
}
},
"autoload-dev":
{
"psr-4":
{
"Gumlet\\": "test/"
}
},
"require-dev":
{
"phpunit/phpunit": "^8.5",
"apigen/apigen": "^4.1",
"php-coveralls/php-coveralls": "^2.1",
"ext-exif": "*",
"ext-gd": "*"
}
}
lib/ImageResize.php 0000644 00000055706 15025006061 0010237 0 ustar 00 filters[] = $filter;
return $this;
}
/**
* Apply filters.
*
* @param $image resource an image resource identifier
* @param $filterType filter type and default value is IMG_FILTER_NEGATE
*/
protected function applyFilter($image, $filterType = IMG_FILTER_NEGATE)
{
foreach ($this->filters as $function) {
$function($image, $filterType);
}
}
/**
* Loads image source and its properties to the instanciated object
*
* @param string $filename
* @return ImageResize
* @throws ImageResizeException
*/
public function __construct($filename)
{
if (!defined('IMAGETYPE_WEBP')) {
define('IMAGETYPE_WEBP', 18);
}
if (!defined('IMAGETYPE_BMP')) {
define('IMAGETYPE_BMP', 6);
}
if ($filename === null || empty($filename) || (substr($filename, 0, 5) !== 'data:' && !is_file($filename))) {
throw new ImageResizeException('File does not exist');
}
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$checkWebp = false;
if (strstr(finfo_file($finfo, $filename), 'image') === false) {
if (version_compare(PHP_VERSION, '7.0.0', '<=') && strstr(file_get_contents($filename), 'WEBPVP8') !== false) {
$checkWebp = true;
$this->source_type = IMAGETYPE_WEBP;
} else {
throw new ImageResizeException('Unsupported file type');
}
} elseif(strstr(finfo_file($finfo, $filename), 'image/webp') !== false) {
$checkWebp = true;
$this->source_type = IMAGETYPE_WEBP;
}
if (!$image_info = getimagesize($filename, $this->source_info)) {
$image_info = getimagesize($filename);
}
if (!$checkWebp) {
if (!$image_info) {
if (strstr(finfo_file($finfo, $filename), 'image') !== false) {
throw new ImageResizeException('Unsupported image type');
}
throw new ImageResizeException('Could not read file');
}
$this->original_w = $image_info[0];
$this->original_h = $image_info[1];
$this->source_type = $image_info[2];
}
switch ($this->source_type) {
case IMAGETYPE_GIF:
$this->source_image = imagecreatefromgif($filename);
break;
case IMAGETYPE_JPEG:
$this->source_image = $this->imageCreateJpegfromExif($filename);
// set new width and height for image, maybe it has changed
$this->original_w = imagesx($this->source_image);
$this->original_h = imagesy($this->source_image);
break;
case IMAGETYPE_PNG:
$this->source_image = imagecreatefrompng($filename);
break;
case IMAGETYPE_WEBP:
$this->source_image = imagecreatefromwebp($filename);
$this->original_w = imagesx($this->source_image);
$this->original_h = imagesy($this->source_image);
break;
case IMAGETYPE_BMP:
if (version_compare(PHP_VERSION, '7.2.0', '<')) {
throw new ImageResizeException('For bmp support PHP >= 7.2.0 is required');
}
$this->source_image = imagecreatefrombmp($filename);
break;
default:
throw new ImageResizeException('Unsupported image type');
}
if (!$this->source_image) {
throw new ImageResizeException('Could not load image');
}
finfo_close($finfo);
return $this->resize($this->getSourceWidth(), $this->getSourceHeight());
}
// http://stackoverflow.com/a/28819866
public function imageCreateJpegfromExif($filename)
{
$img = imagecreatefromjpeg($filename);
if (!function_exists('exif_read_data') || !isset($this->source_info['APP1']) || strpos($this->source_info['APP1'], 'Exif') !== 0) {
return $img;
}
try {
$exif = @exif_read_data($filename);
} catch (Exception $e) {
$exif = null;
}
if (!$exif || !isset($exif['Orientation'])) {
return $img;
}
$orientation = $exif['Orientation'];
if ($orientation === 6 || $orientation === 5) {
$img = imagerotate($img, 270, 0);
} elseif ($orientation === 3 || $orientation === 4) {
$img = imagerotate($img, 180, 0);
} elseif ($orientation === 8 || $orientation === 7) {
$img = imagerotate($img, 90, 0);
}
if ($orientation === 5 || $orientation === 4 || $orientation === 7) {
imageflip($img, IMG_FLIP_HORIZONTAL);
}
return $img;
}
/**
* Saves new image
*
* @param string $filename
* @param integer $image_type
* @param integer $quality
* @param integer $permissions
* @param boolean $exact_size
* @return static
*/
public function save($filename, $image_type = null, $quality = null, $permissions = null, $exact_size = false)
{
$image_type = $image_type ?: $this->source_type;
$quality = is_numeric($quality) ? (int) abs($quality) : null;
switch ($image_type) {
case IMAGETYPE_GIF:
if( !empty($exact_size) && is_array($exact_size) ){
$dest_image = imagecreatetruecolor($exact_size[0], $exact_size[1]);
} else{
$dest_image = imagecreatetruecolor($this->getDestWidth(), $this->getDestHeight());
}
$background = imagecolorallocatealpha($dest_image, 255, 255, 255, 1);
imagecolortransparent($dest_image, $background);
imagefill($dest_image, 0, 0, $background);
imagesavealpha($dest_image, true);
break;
case IMAGETYPE_JPEG:
if( !empty($exact_size) && is_array($exact_size) ){
$dest_image = imagecreatetruecolor($exact_size[0], $exact_size[1]);
$background = imagecolorallocate($dest_image, 255, 255, 255);
imagefilledrectangle($dest_image, 0, 0, $exact_size[0], $exact_size[1], $background);
} else{
$dest_image = imagecreatetruecolor($this->getDestWidth(), $this->getDestHeight());
$background = imagecolorallocate($dest_image, 255, 255, 255);
imagefilledrectangle($dest_image, 0, 0, $this->getDestWidth(), $this->getDestHeight(), $background);
}
break;
case IMAGETYPE_WEBP:
if (version_compare(PHP_VERSION, '5.5.0', '<')) {
throw new ImageResizeException('For WebP support PHP >= 5.5.0 is required');
}
if( !empty($exact_size) && is_array($exact_size) ){
$dest_image = imagecreatetruecolor($exact_size[0], $exact_size[1]);
$background = imagecolorallocate($dest_image, 255, 255, 255);
imagefilledrectangle($dest_image, 0, 0, $exact_size[0], $exact_size[1], $background);
} else{
$dest_image = imagecreatetruecolor($this->getDestWidth(), $this->getDestHeight());
$background = imagecolorallocate($dest_image, 255, 255, 255);
imagefilledrectangle($dest_image, 0, 0, $this->getDestWidth(), $this->getDestHeight(), $background);
}
imagealphablending($dest_image, false);
imagesavealpha($dest_image, true);
break;
case IMAGETYPE_PNG:
if (!$this->quality_truecolor || !imageistruecolor($this->source_image)) {
if( !empty($exact_size) && is_array($exact_size) ){
$dest_image = imagecreate($exact_size[0], $exact_size[1]);
} else{
$dest_image = imagecreate($this->getDestWidth(), $this->getDestHeight());
}
} else {
if( !empty($exact_size) && is_array($exact_size) ){
$dest_image = imagecreatetruecolor($exact_size[0], $exact_size[1]);
} else{
$dest_image = imagecreatetruecolor($this->getDestWidth(), $this->getDestHeight());
}
}
imagealphablending($dest_image, false);
imagesavealpha($dest_image, true);
$background = imagecolorallocatealpha($dest_image, 255, 255, 255, 127);
imagecolortransparent($dest_image, $background);
imagefill($dest_image, 0, 0, $background);
break;
case IMAGETYPE_BMP:
if (version_compare(PHP_VERSION, '7.2.0', '<')) {
throw new ImageResizeException('For WebP support PHP >= 7.2.0 is required');
}
if(!empty($exact_size) && is_array($exact_size)) {
$dest_image = imagecreatetruecolor($exact_size[0], $exact_size[1]);
$background = imagecolorallocate($dest_image, 255, 255, 255);
imagefilledrectangle($dest_image, 0, 0, $exact_size[0], $exact_size[1], $background);
} else {
$dest_image = imagecreatetruecolor($this->getDestWidth(), $this->getDestHeight());
$background = imagecolorallocate($dest_image, 255, 255, 255);
imagefilledrectangle($dest_image, 0, 0, $this->getDestWidth(), $this->getDestHeight(), $background);
}
break;
}
imageinterlace($dest_image, $this->interlace);
if ($this->gamma_correct) {
imagegammacorrect($this->source_image, 2.2, 1.0);
}
if( !empty($exact_size) && is_array($exact_size) ) {
if ($this->getSourceHeight() < $this->getSourceWidth()) {
$this->dest_x = 0;
$this->dest_y = ($exact_size[1] - $this->getDestHeight()) / 2;
}
if ($this->getSourceHeight() > $this->getSourceWidth()) {
$this->dest_x = ($exact_size[0] - $this->getDestWidth()) / 2;
$this->dest_y = 0;
}
}
imagecopyresampled(
$dest_image,
$this->source_image,
$this->dest_x,
$this->dest_y,
$this->source_x,
$this->source_y,
$this->getDestWidth(),
$this->getDestHeight(),
$this->source_w,
$this->source_h
);
if ($this->gamma_correct) {
imagegammacorrect($dest_image, 1.0, 2.2);
}
$this->applyFilter($dest_image);
switch ($image_type) {
case IMAGETYPE_GIF:
imagegif($dest_image, $filename);
break;
case IMAGETYPE_JPEG:
if ($quality === null || $quality > 100) {
$quality = $this->quality_jpg;
}
imagejpeg($dest_image, $filename, $quality);
break;
case IMAGETYPE_WEBP:
if (version_compare(PHP_VERSION, '5.5.0', '<')) {
throw new ImageResizeException('For WebP support PHP >= 5.5.0 is required');
}
if ($quality === null) {
$quality = $this->quality_webp;
}
imagewebp($dest_image, $filename, $quality);
break;
case IMAGETYPE_PNG:
if ($quality === null || $quality > 9) {
$quality = $this->quality_png;
}
imagepng($dest_image, $filename, $quality);
break;
case IMAGETYPE_BMP:
imagebmp($dest_image, $filename, $quality);
break;
}
if ($permissions) {
chmod($filename, $permissions);
}
imagedestroy($dest_image);
return $this;
}
/**
* Convert the image to string
*
* @param int $image_type
* @param int $quality
* @return string
*/
public function getImageAsString($image_type = null, $quality = null)
{
$string_temp = tempnam(sys_get_temp_dir(), '');
$this->save($string_temp, $image_type, $quality);
$string = file_get_contents($string_temp);
unlink($string_temp);
return $string;
}
/**
* Convert the image to string with the current settings
*
* @return string
*/
public function __toString()
{
return $this->getImageAsString();
}
/**
* Outputs image to browser
* @param string $image_type
* @param integer $quality
*/
public function output($image_type = null, $quality = null)
{
$image_type = $image_type ?: $this->source_type;
header('Content-Type: ' . image_type_to_mime_type($image_type));
$this->save(null, $image_type, $quality);
}
/**
* Resizes image according to the given short side (short side proportional)
*
* @param integer $max_short
* @param boolean $allow_enlarge
* @return static
*/
public function resizeToShortSide($max_short, $allow_enlarge = false)
{
if ($this->getSourceHeight() < $this->getSourceWidth()) {
$ratio = $max_short / $this->getSourceHeight();
$long = (int) ($this->getSourceWidth() * $ratio);
$this->resize($long, $max_short, $allow_enlarge);
} else {
$ratio = $max_short / $this->getSourceWidth();
$long = (int) ($this->getSourceHeight() * $ratio);
$this->resize($max_short, $long, $allow_enlarge);
}
return $this;
}
/**
* Resizes image according to the given long side (short side proportional)
*
* @param integer $max_long
* @param boolean $allow_enlarge
* @return static
*/
public function resizeToLongSide($max_long, $allow_enlarge = false)
{
if ($this->getSourceHeight() > $this->getSourceWidth()) {
$ratio = $max_long / $this->getSourceHeight();
$short = (int) ($this->getSourceWidth() * $ratio);
$this->resize($short, $max_long, $allow_enlarge);
} else {
$ratio = $max_long / $this->getSourceWidth();
$short = (int) ($this->getSourceHeight() * $ratio);
$this->resize($max_long, $short, $allow_enlarge);
}
return $this;
}
/**
* Resizes image according to the given height (width proportional)
*
* @param integer $height
* @param boolean $allow_enlarge
* @return static
*/
public function resizeToHeight($height, $allow_enlarge = false)
{
$ratio = $height / $this->getSourceHeight();
$width = (int) ($this->getSourceWidth() * $ratio);
$this->resize($width, $height, $allow_enlarge);
return $this;
}
/**
* Resizes image according to the given width (height proportional)
*
* @param integer $width
* @param boolean $allow_enlarge
* @return static
*/
public function resizeToWidth($width, $allow_enlarge = false)
{
$ratio = $width / $this->getSourceWidth();
$height = (int) ($this->getSourceHeight() * $ratio);
$this->resize($width, $height, $allow_enlarge);
return $this;
}
/**
* Resizes image to best fit inside the given dimensions
*
* @param integer $max_width
* @param integer $max_height
* @param boolean $allow_enlarge
* @return static
*/
public function resizeToBestFit($max_width, $max_height, $allow_enlarge = false)
{
if ($this->getSourceWidth() <= $max_width && $this->getSourceHeight() <= $max_height && $allow_enlarge === false) {
return $this;
}
$ratio = $this->getSourceHeight() / $this->getSourceWidth();
$width = $max_width;
$height = (int) ($width * $ratio);
if ($height > $max_height) {
$height = $max_height;
$width = (int) ($height / $ratio);
}
return $this->resize($width, $height, $allow_enlarge);
}
/**
* Resizes image according to given scale (proportionally)
*
* @param integer|float $scale
* @return static
*/
public function scale($scale)
{
$width = (int) ($this->getSourceWidth() * $scale / 100);
$height = (int) ($this->getSourceHeight() * $scale / 100);
$this->resize($width, $height, true);
return $this;
}
/**
* Resizes image according to the given width and height
*
* @param integer $width
* @param integer $height
* @param boolean $allow_enlarge
* @return static
*/
public function resize($width, $height, $allow_enlarge = false)
{
if (!$allow_enlarge) {
// if the user hasn't explicitly allowed enlarging,
// but either of the dimensions are larger then the original,
// then just use original dimensions - this logic may need rethinking
if ($width > $this->getSourceWidth() || $height > $this->getSourceHeight()) {
$width = $this->getSourceWidth();
$height = $this->getSourceHeight();
}
}
$this->source_x = 0;
$this->source_y = 0;
$this->dest_w = $width;
$this->dest_h = $height;
$this->source_w = $this->getSourceWidth();
$this->source_h = $this->getSourceHeight();
return $this;
}
/**
* Crops image according to the given width, height and crop position
*
* @param integer $width
* @param integer $height
* @param boolean $allow_enlarge
* @param integer $position
* @return static
*/
public function crop($width, $height, $allow_enlarge = false, $position = self::CROPCENTER)
{
if (!$allow_enlarge) {
// this logic is slightly different to resize(),
// it will only reset dimensions to the original
// if that particular dimenstion is larger
if ($width > $this->getSourceWidth()) {
$width = $this->getSourceWidth();
}
if ($height > $this->getSourceHeight()) {
$height = $this->getSourceHeight();
}
}
$ratio_source = $this->getSourceWidth() / $this->getSourceHeight();
$ratio_dest = $width / $height;
if ($ratio_dest < $ratio_source) {
$this->resizeToHeight($height, $allow_enlarge);
$excess_width = (int) (($this->getDestWidth() - $width) * $this->getSourceWidth() / $this->getDestWidth());
$this->source_w = $this->getSourceWidth() - $excess_width;
$this->source_x = $this->getCropPosition($excess_width, $position);
$this->dest_w = $width;
} else {
$this->resizeToWidth($width, $allow_enlarge);
$excess_height = (int) (($this->getDestHeight() - $height) * $this->getSourceHeight() / $this->getDestHeight());
$this->source_h = $this->getSourceHeight() - $excess_height;
$this->source_y = $this->getCropPosition($excess_height, $position);
$this->dest_h = $height;
}
return $this;
}
/**
* Crops image according to the given width, height, x and y
*
* @param integer $width
* @param integer $height
* @param integer $x
* @param integer $y
* @return static
*/
public function freecrop($width, $height, $x = false, $y = false)
{
if ($x === false || $y === false) {
return $this->crop($width, $height);
}
$this->source_x = $x;
$this->source_y = $y;
if ($width > $this->getSourceWidth() - $x) {
$this->source_w = $this->getSourceWidth() - $x;
} else {
$this->source_w = $width;
}
if ($height > $this->getSourceHeight() - $y) {
$this->source_h = $this->getSourceHeight() - $y;
} else {
$this->source_h = $height;
}
$this->dest_w = $width;
$this->dest_h = $height;
return $this;
}
/**
* Gets source width
*
* @return integer
*/
public function getSourceWidth()
{
return $this->original_w;
}
/**
* Gets source height
*
* @return integer
*/
public function getSourceHeight()
{
return $this->original_h;
}
/**
* Gets width of the destination image
*
* @return integer
*/
public function getDestWidth()
{
return $this->dest_w;
}
/**
* Gets height of the destination image
* @return integer
*/
public function getDestHeight()
{
return $this->dest_h;
}
/**
* Gets crop position (X or Y) according to the given position
*
* @param integer $expectedSize
* @param integer $position
* @return integer
*/
protected function getCropPosition($expectedSize, $position = self::CROPCENTER)
{
$size = 0;
switch ($position) {
case self::CROPBOTTOM:
case self::CROPRIGHT:
$size = $expectedSize;
break;
case self::CROPCENTER:
case self::CROPCENTRE:
$size = $expectedSize / 2;
break;
case self::CROPTOPCENTER:
$size = $expectedSize / 4;
break;
}
return (int) $size;
}
/**
* Enable or not the gamma color correction on the image, enabled by default
*
* @param bool $enable
* @return static
*/
public function gamma($enable = false)
{
$this->gamma_correct = $enable;
return $this;
}
}
lib/ImageResizeException.php 0000644 00000000203 15025006061 0012074 0 ustar 00 Gumlet.com is a **free** service which can process images in real-time and serve worldwide through CDN.
------------------
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:
```json
{
"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.
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.
For PHP versions >= 7.2, `2.0.1` or above version of this library should be used.
Otherwise:
```php
include '/path/to/ImageResize.php';
```
Because this class uses namespacing, when instantiating the object, you need to either use the fully qualified namespace:
```php
$image = new \Gumlet\ImageResize();
```
Or alias it:
```php
use \Gumlet\ImageResize;
$image = new ImageResize();
```
> Note: This library uses GD class which do not support resizing animated gif files
------------------
Resize
------
To scale an image, in this case to half it's size (scaling is percentage based):
```php
$image = new ImageResize('image.jpg');
$image->scale(50);
$image->save('image2.jpg');
```
To resize an image according to one dimension (keeping aspect ratio):
```php
$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):
```php
$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):
```php
$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:
```php
$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:
```php
$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
----
To to crop an image:
```php
$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:
```php
$image = new ImageResize('image.jpg');
$image->crop(200, 200);
$image->save('image2.jpg');
```
```php
$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.
For instance passing the crop mode `CROPTOP` will result as 100px taken off the bottom leaving you with 200px × 200px.
```php
$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.
```php
$image = new ImageResize('image.jpg');
$image->crop(200, 200, true, 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:
```php
$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:
```php
$image = ImageResize::createFromString(base64_decode('R0lGODlhAQABAIAAAAQCBP///yH5BAEAAAEALAAAAAABAAEAAAICRAEAOw=='));
$image->scale(50);
$image->save('image.jpg');
```
You can also return the result as a string:
```php
$image = ImageResize::createFromString(base64_decode('R0lGODlhAQABAIAAAAQCBP///yH5BAEAAAEALAAAAAABAAEAAAICRAEAOw=='));
$image->scale(50);
echo $image->getImageAsString();
```
Magic `__toString()` is also supported:
```php
$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');`
To render the image directly into the browser, you can call `$image->output()`;
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:
```php
$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:
```php
$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:
```php
$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
-----------
By default, [image interlacing](http://php.net/manual/en/function.imageinterlace.php) is turned on. It can be disabled by setting `$interlace` to `0`:
```php
$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.
This is useful for creating multiple sizes:
```php
$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.
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)
```php
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.
```php
$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.
```php
$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.
Gamma color correction
--------
You can enable the gamma color correction which is disabled by default.
```php
$image = new ImageResize('image.png');
$image->gamma(true);
```
API Doc
-------
https://gumlet.github.io/php-image-resize/index.html
------------------
Maintainer
----------
This library is maintained by Gumlet.com
[
](https://www.gumlet.com)
Licence.md 0000644 00000002065 15025006061 0006426 0 ustar 00 MIT License
Copyright (c) 2018 Turing Analytics LLP
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
.github/workflows/php-testing.yml 0000644 00000001530 15025006061 0013120 0 ustar 00 name: PHP CI
on: ["push", "pull_request"]
jobs:
run:
runs-on: ${{ matrix.operating-system }}
strategy:
fail-fast: false
matrix:
operating-system: [ubuntu-20.04]
php-versions: ['7.2', '7.3', '7.4', '8.0', '8.1']
name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: gd, xdebug, fileinfo
coverage: xdebug
tools: php-cs-fixer, phpunit
- name: Composer Install
run: composer install --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist
- name: Running Tests
run: |
mkdir -p build/logs
./vendor/bin/phpunit
test/ImageResizeExceptionTest.php 0000644 00000002263 15025006061 0013155 0 ustar 00 assertEquals("", $e->getMessage());
$this->assertInstanceOf('\Gumlet\ImageResizeException', $e);
}
public function testExceptionMessage()
{
$e = new ImageResizeException("General error");
$this->assertEquals("General error", $e->getMessage());
$this->assertInstanceOf('\Gumlet\ImageResizeException', $e);
}
public function testExceptionExtending()
{
$e = new ImageResizeException("General error");
$this->assertInstanceOf('\Exception', $e);
}
public function testExceptionThrown()
{
try{
throw new ImageResizeException("General error");
} catch (\Exception $e) {
$this->assertEquals("General error", $e->getMessage());
$this->assertInstanceOf('\Gumlet\ImageResizeException', $e);
return;
}
$this->fail();
}
}
// It's pretty easy to get your attention these days, isn't it? :D
test/ressources/test_bmp.bmp 0000644 00000000216 15025006061 0012227 0 ustar 00 BM | #. #. BGRs _R test/ressources/test_webp.webp 0000644 00000073160 15025006061 0012575 0 ustar 00 RIFFhv WEBPVP8 \v Ҿ*&p>NCѴQ93u>ʧ-:c *d/Uo经OnzO3wK[;>x?{/@s=@?`/~y-WG_
u?eQlŠs78е[Ks7R!iʧV^4K*:'ѩOw*w
>kMmGF
W\_|=|= ʟ04OPg~ݢs(+0\ߎ,lnl2S
3qw%^Yw$UiJ N
p5GܽGYh#[{`nbGEpN$,8#x`e