Pages

Friday, December 7, 2012

PHP resize an image without losing quality - [resolved]

PHP resize an image without losing quality, we will explain how we can resize an image through PHP, with only 3 lines of code. And most importantly, without the result is a pixelated image.

We begin:

To do this we will use the Imagick PHP Library (Image magic), which is included in PHP itself from version 5.1.3. As a second step we must ensure that the hosting site where we stored the library have this enabled, some do not bring it and this causes many errors. If we are running with WAMP or XAMP the web, it would not hurt to look for a tutorial on how to activate services and libraries for PHP in these local systems.

* A requirement before proceeding with the process is that the image should be resized before being uploaded to our server before.

We start by instantiating the class constructor, which receives as parameter the full path to the image hosted on our server (including extension):

$image = new Imagick('Imagename');

In the variable $image keep our Imagick object for treatment, after this, simply call the method cropThumbnailImage, whose parameters are width and height (in order):

$image->cropThumbnailImage(width[type int],high[type int]); //this function is used for croping image

OR

$image->resizeImage('500','691', imagick::FILTER_LANCZOS, 0.3); // this function is used for imageResize


As a last step we can only save the image you just cut, for it has the method Imagick writeImage whose expected parameter is the path where you want to save the image but the name of this. (please add the suffix _thumb the name of the image):

$image->writeImage( 'target_folder' );

This done, get our cropped image with PHP, without loss of quality, and without the result is a pixelated Thumbnail

For more information about this library, you can visit the PHP API: Image Magic PHP

No comments: