Dynamic image using PHP

Posted on

Problem

I need to create an image on the fly where one large image covers a small image. The large image is a mask which makes the underlying image visible. Here is a quick sketch of what I’m trying to accomplish:

enter image description here
It’s important that Y lies beneath X.

I have created to code for this and it works, but I was wondering how I can make the code more efficient. This is what I have:

    // Create a blank image with the underlying image correctly positioned
    $blank = imagecreatetruecolor(403, 403);
    $profile = imagecreatefromjpeg('img.jpg');
    $w = imagesx($profile);
    $h = imagesy($profile);
    imagecopy($blank, $profile, 0, 140, 0, 0, $w, $h);
    imagejpeg($blank, 'tmp.jpg', 100);

    // Frame overlay
    $frame = imagecreatefrompng('frame.png');
    $photo = imagecreatefromjpeg('tmp.jpg');
    imagecopy($photo, $frame, 0, 0, 0, 0, 403, 403);
    imagejpeg($photo, 'output.jpg', 100);

    // Garbage collection
    unlink('tmp.jpg');
    imagedestroy($blank);
    imagedestroy($profile);
    imagedestroy($frame);
    imagedestroy($photo);

I just think that creating 2 different images is just overkill, but I just can’t find a better solution.

Solution

First of all don’t save your temporary file as tmp.jpg, think about what happends if there are 2 requests for an image at the same time, the first tmp.jpg will be overwritten by the second one (very unlikely, but still possible and very hard to debug / reproduce bug)..

Second saving to the disk is expensive and useless, avoid it whenever possible.

Third why do you need the blank image? I think you should have the frame image and overlay the profile image on top of it.

Given this, I think the code should look like this:

// Create a blank image with the underlying image correctly positioned
$base = imagecreatefrompng('frame.png');
$profile = imagecreatefromjpeg('img.jpg');
$w = imagesx($profile);
$h = imagesy($profile);
imagecopy($base, $profile, 0, 140, 0, 0, $w, $h);

imagejpeg($base, 'output.jpg', 100);

// Garbage collection
imagedestroy($base);
imagedestroy($profile);

Unless I miss something this code should do exactly the same thing, but faster and safer.

Leave a Reply

Your email address will not be published. Required fields are marked *