Layering external graphics to dynamically create a new image

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ExpertAlmost
Forum Commoner
Posts: 41
Joined: Mon Oct 20, 2008 12:26 am

Layering external graphics to dynamically create a new image

Post by ExpertAlmost »

Good morning!

I have been playing around with the PHP graphics functions in an attempt to build a graphic using several external PNG files and layering them, one on top of the other. All the external PNG files are the same size and saved with white as transparent. My plan is to use a base graphic (a speedometer face), overlay it with another graphic (an arrow pointing at a set angle), overlay those two with another graphic (a different arrow at a different angle), etc...with up to 5 layers. The base remains the same in each case, but the arrows use a subset of all the possible arrow directions.

First I tried just creating the 2 resourses and then outputting them (Code Sample 1, below). But that just outputs the first file. (The second test file is a wider-thinner red box to make sure I am not just covering one graphic with the other.)

Then I tried using imagecopy, both without and with imagelayereffect($image,IMG_EFFECT_OVERLAY), and that writes the second image on the first, but seems to make the first image all black (while keeping the original solid color of the second image). (Code Sample 2)

I am at a loss as to what to try next. Being very new to to PHP, any brilliant suggestions, code snippets, completed packages doing what I want and a dozen things I have not yet thought of, would all be greatly appreciated!

Thank you for your time, efforts and expertise!
aloha ah

CODE SAMPLE 1: Simple Create and Write

Code: Select all

$ImgBaseStr = "C:/AppServ/www/img/base.png";
$ImgTopStr = "C:/AppServ/www/img/test.png";
$ImgBase = imagecreatefrompng($ImgBaseStr) or die("Cannot Initialize $ImgBaseStr"); 
$ImgTop = imagecreatefrompng($ImgTopStr) or die("Cannot Initialize $ImgTopStr"); 
header("Content-Type: image/png");
imagepng($ImgBase);
imagepng($ImgTop);
imagedestroy($ImgBase);
imagedestroy($ImgTop);
CODE SAMPLE 2: Using imagecopy with and without imagelayereffect

Code: Select all

$ImgBaseStr = "C:/AppServ/www/img/base.png";
$ImgTopStr = "C:/AppServ/www/img/test.png";
$ImgBase = imagecreatefrompng($ImgBaseStr) or die("Cannot Initialize $ImgBaseStr"); 
$ImgTop = imagecreatefrompng($ImgTopStr) or die("Cannot Initialize $ImgTopStr"); 
$ImgX = imagesx($ImgBase); 
$ImgY = imagesy($ImgBase); 
imagelayereffect($ImgTop, IMG_EFFECT_OVERLAY);
imagecopy ($ImgBase, $ImgTop, 0, 0, 0, 0, $ImgX, $ImgY);
header("Content-Type: image/png");
imagepng($ImgBase);
imagedestroy($ImgBase);
imagedestroy($ImgTop);
 
Post Reply