Optimisation help
Posted: 2011-11-25T04:09:29-07:00
Hello
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)
I'm thinking of outputting the raw image straight to the browser for one optimisation, but can anyone make any other suggestions? Are there any imagick / imagemagick alternative functions I could be using instead?
BTW I am running Q16 IM6.7.1-7.
Thanks for any advice you can provide...
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...