Overlay an image on an image (background on a graph)

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
travdes
Forum Newbie
Posts: 5
Joined: Fri Dec 13, 2002 12:47 am
Contact:

Overlay an image on an image (background on a graph)

Post by travdes »

How do you overlay a background image on a graph? I'm creating a bar graph, and would like to have a background image, but havent found anything in the API to create an image, on top of another image.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you might use imagecreatefrompng() (or similar) and then draw on your background-image.

setting imagecolortransparent() is regarded by imagecopymerge() if you have to merge two images

Code: Select all

<?php
header('Content-type: image/png');
// a solid-blue image
$back = imagecreate(30,30);
$col1 = imagecolorallocate($back, 0,0,255);
// a solid-black image
$add = imagecreate(30,30);
$col1 = imagecolorallocate($add, 0,0,0);
// with some white text on it
$col2 = imagecolorallocate($add, 255,255,255);
imagestring($add, 1, 5, 10, "test", $col2);
// merge both
imagecolortransparent($add,  imagecolorat($add, 0,0));
imagecopymerge($back, $add, 0, 0, 0, 0, imagesx($add), imagesy($add), 99);
// output
imagepng($back);
?>
travdes
Forum Newbie
Posts: 5
Joined: Fri Dec 13, 2002 12:47 am
Contact:

Sweet...

Post by travdes »

Thanks for your help. I'll have to remember not to use phpbuilder to look up the functions, because that one doesnt exist on phpbuilder but does on php.net.
travdes
Forum Newbie
Posts: 5
Joined: Fri Dec 13, 2002 12:47 am
Contact:

Redraw problems

Post by travdes »

For some reason sometimes it draws 'imagefilledrectangle' properly and other times, not. For example, I've attached an image, the backdrop is an example of it not working (the bars are almost the same colour as the background, and the image on the side is an example of it working.

I first create 'image A', the merge 'image B' (background image', then I create all the plot lines, bars etc on top of 'image A'. This doesnt work at all if I set transition to 99, I reset it to the 75 range, and it works some of the times.

As you can see from the images, the bars are getting drawn in both, the colours just arent correct all the time. Does anyone have any idea what might be causing this?

Image
Post Reply