image merging

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
a-man
Forum Newbie
Posts: 3
Joined: Tue Jul 25, 2006 2:21 am

image merging

Post by a-man »

hi,

i have the following problem: I've got a picture, a jpg photo with variabel size. What i want to do is put the jpg over a new image (this one is bigger) and save it to a png.

The reason for this is because i need to create images with the same width & height but a user can upload the photos so they have different width & height. Like this: user uploads photo -> create new transparent image -> put photo over it -> save the image to png
Result is a photo (bigger then the original) with a transparent background.

My problem lies here: when i use imagecreate for the new image the transparency is ok, but the colors of my photo are wierd. Then i used imagecreatetruecolor but then the transparency was lost (i got a black square with my photo over it)

a bit of code to verify

Code: Select all

<?php
//create the new image
$image = imagecreate(600,600);
//set the color to white
$back = imagecolorallocate($image, 255, 255, 255);
//make white transparent
imagecolortransparent($image,$col);

//create an image from the photo
$photo = imagecreatefromjpeg("test.jpg");

//copy the two together
imagecopymerge($image,$photo,50,50,0,0,400,500,75);

//output a correct header
header('Content-type: image/png');

// and finally, output the result
imagepng($image);

imagedestroy($image);
imagedestroy($foto);
?>
thanks in advance

ps: sorry for the language errors
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Moved to PHPGD.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Try this: http://www.phpgd.com/scripts.php?script=27 .. it's a function for creating transparent true color images.
a-man
Forum Newbie
Posts: 3
Joined: Tue Jul 25, 2006 2:21 am

Post by a-man »

works great, thanks

my apologies for posting in the wrong forum
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

This is a terrible waste of bandwidth. A much better way would be grab the image width and height with getimagesize() and then write margin values to your <img> element.
a-man
Forum Newbie
Posts: 3
Joined: Tue Jul 25, 2006 2:21 am

Post by a-man »

wouldn't that stretch the photo? if i understand you correctly, you would do this if the eventual image has to be 800x600:

<img width="300" height="500"> => 800x600

or even worse

<img width="50" height="800"> => 800x600

the eventual width and height have to be equal for all photos because they will serve in a carousel

or do you mean something else?
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

a-man wrote:or do you mean something else?
I mean something else. The code below should be fine although due to roundup error the margin (not the image) could be 1 pixel off what it should be when the image dimensions are odd. This could be cured by specifying all four margins (if you are fanatical).

Code: Select all

<?php

function img_element($image, $space_width  = 500, $space_height = 500)
{
	// $image is a relative path accessible from the web and the file system.
	list($width, $height, , $details) = @getimagesize($image) or die('problem with path or file type.');
	$vertical_margin   = round(($space_height - $height) / 2);
	$horizontal_margin = round(($space_width  - $width)  / 2);
	return '<img src="'.$image.'" '.$details.' style="margin:'.$vertical_margin.'px '.$horizontal_margin.'px">';
}

// test it... This worked fine for me...
echo img_element('./1.jpg');

?>
Post Reply