skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. I tried this benchmark with php-vips:

    #!/usr/bin/env php
    <?php
        
    require __DIR__ . '/vendor/autoload.php';
    use JcupittVips;
            
    $image = VipsImage::newFromFile($argv[1]);
    $image = $image->rotate(12);
    $image->writeToFile($argv[2]);
    

    I made a test image and timed it:

    $ vips crop ~/pics/st-francis.jpg x.jpg 0 0 2457 4032
    $ /usr/bin/time -f %M:%e ./rotate-libvips.php x.jpg y.jpg
    120684:0.21
    

    So it needed 210ms and about 120mb of memory on this PC (ubuntu 24.04, php 8.3, libvips 8.15).

    I tried imagick:

    #!/usr/bin/env php
    <?php   
        
    $imagick = new Imagick($argv[1]);
    $imagick->rotateImage(new ImagickPixel(), 12);
    $imagick->writeImage($argv[2]);
    

    I see:

    $ /usr/bin/time -f %M:%e ./rotate-imagick.php x.jpg y.jpg
    224824:2.65
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search