I am manipulating large hi-res jpeg/webp images (2457 x 4032) to apply $imagick->distortImage(Imagick::DISTORTION_PERSPECTIVE, $coords, false);
using PHP8 to fix perspectives – but I am finding this runs very slowly (+10 seconds in a localhost centos7 linux and +15 seconds on a shared host linux). (This is being seen over most images so this is not just an outlier).
I am also getting poor performance from $imagick->rotateImage(new ImagickPixel(), $angleDeg);
calls on the same images.
I cannot lose the resolution as the content is critical – but is there anything else that can be done? (Are there any alternatives to Imagick perhaps?)
2
Answers
While Imagick’s DISTORTION_PERSPECTIVE and rotateImage methods offer powerful image manipulation capabilities, they can struggle with large, high-resolution images due to the computational complexity involved. If you’re experiencing sluggish performance on sizeable JPEG/WebP files, a few potential solutions could help. First, consider using a lower-level library like MagickWand or the ImageMagick command-line tools, which may offer better performance for resource-intensive operations. Alternatively, you could downscale the image before processing and then upscale it back to the desired resolution, albeit with some potential quality loss. Setting up a dedicated image processing server, optimizing memory usage, exploring alternative libraries like GD or Gmagick, or implementing parallelization or background processing could also potentially improve performance, depending on your specific use case and infrastructure.
I tried this benchmark with php-vips:
I made a test image and timed it:
So it needed 210ms and about 120mb of memory on this PC (ubuntu 24.04, php 8.3, libvips 8.15).
I tried imagick:
I see:
2.7s and 225mb of memory.
Perspective distort is a bit awkward on php-vips — performance is OK, but you need to enter the formula yourself using
mapim
.