2

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

dippas
  • 58,591
  • 15
  • 114
  • 126
Amv
  • 59
  • 8

1 Answers1

3

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.

jcupitt
  • 10,213
  • 2
  • 23
  • 39
  • Thanks for the help. Need one more help imagick provide Imagick::FILTER_LANCZOS for apply filter and also there is one blur option in resize option how can i achieve that using vips? – Amv Aug 31 '20 at 13:30
  • 1
    libvips `thumbnail` uses lanczos3. – jcupitt Aug 31 '20 at 13:40
  • Is there any way for change lanczos3? – Amv Aug 31 '20 at 14:01
  • 1
    `thumbnail` is supposed to implement best practice, so there are very few options. Why do you need a different kernel? – jcupitt Aug 31 '20 at 14:23
  • Little bit confused about filters now got the idea and i don't need to change kernel. Thank you for the help – Amv Aug 31 '20 at 14:36
  • One more thing, Imagick::getImageBlob() function is an inbuilt function in PHP which is used to get the image sequence as a blob. I want to do same for vips any similar method for that? – Amv Aug 31 '20 at 15:08
  • Yes, `$str = $image->writeToBuffer('.jpg');`, see https://libvips.github.io/php-vips/docs/classes/Jcupitt-Vips-Image.html#method_writeToBuffer – jcupitt Aug 31 '20 at 15:43
  • I am crooping the image by using below php vips method $im = $im->crop(87, 98, 530, 352); I am getting an error bad extract area, don'r know why that error occurred. Is there any way to prevent this? – Amv Sep 02 '20 at 06:03
  • It's `crop(left, top, width, height)`. You are asking for an area outside the image edges. – jcupitt Sep 02 '20 at 10:58