Choose PHP Resizing Algorithm

The imagecopyresampled function is useful for creating thumbnails or resizing images while maintaining aspect ratio:

$fn = $_FILES['data']['tmp_name']; $size = getimagesize($fn); $width = $size[0]; $height = $size[1]; $ratio = $width / $height; if ($ratio > 1 && $size[0] > 500) { $width = 500; $height = 500 / $ratio; } else { if ($ratio <= 1 && $size[1] > 500) { $width = 500 * $ratio; $height = 500; }} $src = imagecreatefromstring(file_get_contents($fn)); $dst = imagecreatetruecolor($width, $height); imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $size[0], $size[1]); imagedestroy($src); imagejpeg($dst, 'test.jpg'); imagedestroy($dst); 

How to choose the resizing algorithm used by PHP?
Note: as indicated in this question , setting imagesetinterpolation($dst, IMG_BILINEAR_FIXED); or such things do not work.


According to the tests I did (in another language), "bilinear resizing" sometimes gives a better result than bicubic, and sometimes vice versa (it depends on its decrease or increase).

+7
php image-processing image-resizing
source share
4 answers

An alternative is the imagescale() function, which allows you to specify the interpolation algorithm as a parameter:

 imagescale($image, $new_width, $new_height, $algorithm); 

According to the documentation $algorithm can be:

One of IMG_NEAREST_NEIGHBOUR , IMG_BILINEAR_FIXED , IMG_BICUBIC , IMG_BICUBIC_FIXED or something else (will use two passes).

A test in PHP 7.0.15 (file hash comparison) shows that BICUBIC and BICUBIC_FIXED lead to a different image, while BILINEAR_FIXED and NEAREST_NEIGHBOUR lead to the same image.

+8
source share

imagecopyresampled is based / partially LibGD, looking at the source code of LibGD, you can clearly see its implementation , also the documentation is not ambiguous regarding the algorithm used, since it stated that:

If the source and destination areas are different in size, the area will be resized using bilinear interpolation for truecolor images and nearest neighbor interpolation for palette images. p>

So how can you choose the resizing algorithm used by PHP?

If you use to insist / should use the functions of LibGD, you cannot (unless you recompile PHP with the LibGD plugin, you are the code for this question).

However, if you can use a different image manipulation library, you can simply use one that uses a different algorithm for resizing, for example Imagick seems to offer a wide range of interpolations, but since the documentation is pretty inattentive about this, here are the constants needed to use Imagick::setImageInterpolateMethod(int $) method:

 const INTERPOLATE_UNDEFINED = 0; const INTERPOLATE_AVERAGE = 1; const INTERPOLATE_BICUBIC = 2; const INTERPOLATE_BILINEAR = 3; const INTERPOLATE_FILTER = 4; const INTERPOLATE_INTEGER = 5; const INTERPOLATE_MESH = 6; const INTERPOLATE_NEARESTNEIGHBOR = 7; const INTERPOLATE_SPLINE = 8; 
+4
source share

Why aren't you using the library? I think that if you use the php library it will be easier. Try this one . Hope this helps you.

0
source share

Well, you can download PHP Source, add a filter function and compile php.

here you can find the filters https://github.com/php/php-src/blob/master/ext/gd/libgd/gd_interpolation.c#L481

here is a switch case where you should apply the method https://github.com/php/php-src/blob/master/ext/gd/libgd/gd_interpolation.c#L2530

here you can define the constants https://github.com/php/php-src/blob/master/ext/gd/libgd/gd.h#L137

happy hack: D

0
source share

All Articles