I use imagick to combine multiple layers consisting of both text and images resulting in a 72dpi 500x500 png image. The PHP script gets called via an ajax call then saved and the browser serves up the new image. After some benching I am experiencing a 0.7 second average lapse (local development machine) until the image is recompiled and served to browser. When I try on my shared hosting this can be anywhere from 1 - 15 seconds! Before I look at upgrading my hosting (££) I want to see if the procedure can be optimised in-script first. Here is a basic plan of the current process (I have simplified the script as it is in a class and includes unrelated info)
Code: Select all
$canvas = new Imagick();
$canvas->newImage(500, 500, new ImagickPixel('white'));
// loop through layers to add to canvas (usually 1 - 10 either images or text)
// If adding image layer
$layer = new Imagick( realpath('path/to/image.jpg') );
$layer->thumbnailImage( $width, 0, false ); // Only called if image needs to be resized before adding to canvas
$canvas->compositeImage($layer, imagick::COMPOSITE_OVER, $x, $y);
// else If adding text layer
$layer = new Imagick();
$draw = new ImagickDraw();
$draw->setFont( realpath('path/to/font.ttf') );
$draw->setFontSize( 12 );
$draw->setFillColor( ImagickPixel( '#000000' );
$draw->setGravity(Imagick::GRAVITY_CENTER);
$metrics = $layer->queryFontMetrics($draw, $text);
$layer->newImage($width, $metric['textHeight'], ImagickPixel('none'));
$layer->annotateImage( $draw, 0, 0, 0, $text );
$canvas->compositeImage($layer, imagick::COMPOSITE_OVER, $x, $y);
$canvas->writeImage( $this->userFile = 'user_img.png' );
BTW I am running Q16 IM6.7.1-7.
Thanks for any advice you can provide...