Resize the image using PHP vips library

1.7k Views Asked by At

I am using both PHP imagick and PHP vips library for image operations. I am working on image resize operation. For imagick I am using resizeImage() function and for vips I am using resize() function. But the output of both the image are different for same height and width. I want to same output for vips. Here I add my code which I am using for vips. I want same result for vips also, which I got into imagick

<!-- Imagick Code -->
$img = new Imagick($ipFile); 
$img->resizeImage(1000, 1000, Imagick::FILTER_LANCZOS, 1, true);
$img->writeimage($opFile);

<!-- VIPS Code -->
$im = Vips\Image::newFromFile($ipFile);
$im = $im->resize($ratio, ['kernel' => 'lanczos2']);
$im->writeToFile($opFile);

Vips Output file:
Vips Output file Imagick Output file:
Imagick Output file

1

There are 1 best solutions below

9
On BEST ANSWER

Don't use resize with php-vips unless you really have to. You'll get better quality, better speed and lower memory use with thumbnail.

The thumbnail operation combines load and resize in one, so it can exploit tricks like shrink-on-load. It knows about transparency, so it'll resize PNGs correctly. It also knows about vector formats like PDF and SVG and will size them in the best way.

Try:

<!-- VIPS Code -->
$im = Vips\Image::thumbnail($ipFile, 1000);
$im->writeToFile($opFile);

Benchmark:

$ /usr/bin/time -f %M:%e convert ~/pics/nina.jpg -resize 1000x1000 x.jpg
238836:0.77
$ /usr/bin/time -f %M:%e vips resize ~/pics/nina.jpg x.jpg .1653439153
60996:0.39
$ /usr/bin/time -f %M:%e vips thumbnail ~/pics/nina.jpg x.jpg 1000
49148:0.16

nina.jpg is a 6k x 4k RGB JPG image.

  • imagick resizes in 770ms and 240MB of memory
  • vips resize takes 390ms and 61MB of memory
  • vips thumbnail takes 160ms and 50MB of memory

Quality will be the same in this case.