All Projects → gumlet → Php Image Resize

gumlet / Php Image Resize

Licence: mit
PHP library to resize, scale and crop images. Cloud solution available at:

Projects that are alternatives of or similar to Php Image Resize

Gimage
A PHP library for easy image handling. 🖼
Stars: ✭ 148 (-84.5%)
Mutual labels:  scale, crop, resize
downscale
Better image downscale with canvas.
Stars: ✭ 80 (-91.62%)
Mutual labels:  resize, scale, crop
Lipo
👄 Free image manipulation API service built on top of Sharp (an alternative to Jimp, Graphics Magic, Image Magick, and PhantomJS)
Stars: ✭ 101 (-89.42%)
Mutual labels:  scale, crop, resize
Ucrop
Image Cropping Library for Android
Stars: ✭ 11,003 (+1052.15%)
Mutual labels:  scale, crop
Cdn
Content Delivery Network on the top of MongoDb GridFs with on-the-fly image crop/resize
Stars: ✭ 117 (-87.75%)
Mutual labels:  crop, resize
Mergi
go library for image programming (merge, crop, resize, watermark, animate, ease, transit)
Stars: ✭ 127 (-86.7%)
Mutual labels:  crop, resize
Igrphototweaks
Drag, Rotate, Scale and Crop
Stars: ✭ 212 (-77.8%)
Mutual labels:  scale, crop
pdf-thumbnail
npm package to create the preview of a pdf file
Stars: ✭ 23 (-97.59%)
Mutual labels:  resize, crop
vue-responsive-text
↔ Vue component that scales its child node in relation to its parent node's width
Stars: ✭ 23 (-97.59%)
Mutual labels:  resize, scale
pixcat
CLI and Python 3.6+ API to display images on a kitty terminal with optional resizing.
Stars: ✭ 21 (-97.8%)
Mutual labels:  resize, scale
node-imaginary
Minimalist node.js command-line & programmatic API client for imaginary
Stars: ✭ 94 (-90.16%)
Mutual labels:  resize, crop
Bimg
Go package for fast high-level image processing powered by libvips C library
Stars: ✭ 1,394 (+45.97%)
Mutual labels:  crop, resize
Docker Nginx Image Proxy
on the fly image cropping with gravity, resize and compression microservice
Stars: ✭ 79 (-91.73%)
Mutual labels:  crop, resize
Imaging
Imaging is a simple image processing package for Go
Stars: ✭ 4,023 (+321.26%)
Mutual labels:  crop, resize
Nova Advanced Image Field
🌄📐 A Laravel Nova advanced image field with cropping and resizing using Cropper.js and Intervention Image
Stars: ✭ 67 (-92.98%)
Mutual labels:  crop, resize
image-php
很多PHP框架竟然没有图片设定宽高居中剪裁的功能,比如CodeIgniter,所以我自己封装了一个图片处理类:可设定宽高居中剪裁、设定宽高等比缩放、创建缩略图
Stars: ✭ 17 (-98.22%)
Mutual labels:  resize, crop
Fitty
✨ Makes text fit perfectly
Stars: ✭ 3,321 (+247.75%)
Mutual labels:  scale, resize
Sharp
High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP, AVIF and TIFF images. Uses the libvips library.
Stars: ✭ 21,131 (+2112.67%)
Mutual labels:  crop, resize
Flyimg
Dockerized PHP7 application runs as a Microservice to resize and crop images on the fly. Get optimised images with MozJPEG, WebP or PNG using ImageMagick. Includes face detection, cropping, face blurring, image rotation and many other options. Abstract storage based on FlySystem in order to store images on any provider (local, AWS S3...).
Stars: ✭ 762 (-20.21%)
Mutual labels:  crop
Chordious
Fretboard diagram generator for fretted stringed instruments.
Stars: ✭ 22 (-97.7%)
Mutual labels:  scale

php-image-resize

PHP library to resize, scale and crop images.

Build Status Latest Stable Version Monthly Downloads Coverage Status

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.


Setup

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

If using Composer, in your composer.json file add:

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

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.

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 \Gumlet\ImageResize();

Or alias it:

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):

$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

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, 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.

$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.

$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:

$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:

$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

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:

  • 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

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() and imagepng() 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

By default, image interlacing 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

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

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)

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.

$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.

$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 disable the gamma color correction enabled by default.

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

API Doc

https://gumlet.github.io/php-image-resize/index.html


Maintainer

This library is maintained by Gumlet.com

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].