Jump to content


Photo

GD library help


  • Please log in to reply
9 replies to this topic

#1 flash gordon

flash gordon
  • Members
  • PipPipPip
  • Advanced Member
  • 150 posts

Posted 29 March 2006 - 06:23 AM

Can anyone show me a tutorial or some code that opens two images and then makes them overlap at specific x & y values?

I have read the php freaks tutorials on GD, but it doesn't get very indepth at all :(

Any help would be much appreciated.

Cheers!

_me

#2 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 29 March 2006 - 08:05 AM

ok, without writing the code completely:

1, create an image large enough for both images (imagecreate)

2, open picture your first picture (imagecreatefromjpeg/gif, etc)

3, place picture on canvass created in step 1 (imagecopy/imagecopyresized, etc)

4, repeat steps 2 and 3 for second picture, or however many pictures you want

5, send the headers: header("Content-type: image/jpeg");

6, out the image created in step 1 (imagejpeg)


ok sod it, here's an example:

$canvass = imagecreate(800, 600);

$im1 = imagecreatefromjpeg('mypic_one.jpg');
imagecopy($canvass, $im1, 20,30,0,0,100,100);

$im2 = imagecreatefromjpeg('mypic_two.jpg');
imagecopy($canvass, $im2, 200,300,0,0,120,70);

imagedestroy($im1);
imagedestroy($im2);

header("Content-type: image/jpeg");
imagejpeg($canvass);

"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#3 flash gordon

flash gordon
  • Members
  • PipPipPip
  • Advanced Member
  • 150 posts

Posted 29 March 2006 - 07:55 PM

thanks for the help. it has gotten me started some, but I'm having a little trouble. Here is my current code:
<?php

$size1 = getimagesize('wolverine.jpg');
$canvas = imagecreate(($size1[0] + 20), ($size1[1]+20));

$im1 = imagecreatefromjpeg('wolverine.jpg');
imagecopy( $canvas, $im1, 0, 0, 0, 0, $size1[0], $size1[1] );

$size2 = getimagesize('box.jpg');
$im2 = imagecreatefromjpeg('box.jpg');
imagecopy($canvas, $im2, 200, 300, 0, 0, $size2[0], $size2[1] );

header("Content-type: image/gif");
imagegif($canvas);

imagedestroy($im1);
imagedestroy($im2);

?>
And here is the output: gone
and here is the source image: gone

My questions are: [ol type=\'1\'][*]Why is the canvas showing up gray when I didn't allocate any color vaules to it? [*]Why is the image of wolverine appear to have an alpha set of around 60-70? He looks grey-ed out.[*]How do I control the "Z" or depth index? It is simply a matter of the order of calling imagecopy()?[/ol]Thanks for your time and help thus far!

Cheers!

#4 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 29 March 2006 - 09:52 PM

1, because something is wrong with box.jpg (either its format/loading, etc), or box.jpg is a grey box. try loading it seperately just with:

$im = imagecreatefromjpeg('/full/path/to/box.jpg');

header("Content-type: image/jpeg");
imagejpeg($im);

2, no idea. possibly you may consider using imagecreatetruecolor instead of imagecreate, as they look like quite hi-res pictures.
3, everything done overwrites a previous action, just like a painters canvass. so yep - whatever you want on top should be the last thing you actually copy/write.
4, even tho you didn't have a 4th question - nice pictures.

hope that helps
Cheers
Mark

"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 29 March 2006 - 10:48 PM

You seem to have a surplus image. Try without the canvas and output as jpg

<?php
$im1 = imagecreatefromjpeg('wolverine.jpg');

$size2 = getimagesize('box.jpg');
$im2 = imagecreatefromjpeg('box.jpg');
imagecopy($im1, $im2, 200, 300, 0, 0, $size2[0], $size2[1] );

header("Content-type: image/jpg");
imagejpeg($im1);

imagedestroy($im1);
imagedestroy($im2);

?>
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 flash gordon

flash gordon
  • Members
  • PipPipPip
  • Advanced Member
  • 150 posts

Posted 30 March 2006 - 05:30 AM

[!--quoteo(post=359839:date=Mar 29 2006, 05:48 PM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Mar 29 2006, 05:48 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
You seem to have a surplus image. Try without the canvas and output as jpg
[/quote]
Yes! That looks much better. However, do you have a link or can describe by what you mean of: "You seem to have a surplus image"?

Also, before I read you post I made this little application: [a href=\"http://www.modernmusicians.com/help/test.swf\" target=\"_blank\"]HERE[/a]. Whenver you click on "generate image" the image qualitly isn't very good. Do you (anyone) have any idea why that is? The code I am using for that is as follows:

<?php

$canvas = imagecreatetruecolor( 300, 350 );

$size1 = getimagesize('red.jpg');
$im1 = imagecreatefromjpeg('red.jpg');
imagecopy( $canvas, $im1, $_POST['rX'], $_POST['rY'], 0, 0, $size1[0], $size1[1] );

$size2 = getimagesize('blue.jpg');
$im2 = imagecreatefromjpeg('blue.jpg');
imagecopy($canvas, $im2, $_POST['bX'], $_POST['bY'], 0, 0, $size2[0], $size2[1] );

header("Content-type: image/jpeg");
imagejpeg($canvas);

imagedestroy($im1);
imagedestroy($im2);
imagedestroy($canvas);

?>

Thanks for all the help guys!


#7 flash gordon

flash gordon
  • Members
  • PipPipPip
  • Advanced Member
  • 150 posts

Posted 31 March 2006 - 02:40 AM

anyone know why it is blurry?

#8 flash gordon

flash gordon
  • Members
  • PipPipPip
  • Advanced Member
  • 150 posts

Posted 02 April 2006 - 09:47 PM

last bump.

#9 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 02 April 2006 - 10:01 PM

[!--quoteo(post=360985:date=Apr 2 2006, 10:47 PM:name=flash gordon)--][div class=\'quotetop\']QUOTE(flash gordon @ Apr 2 2006, 10:47 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
last bump.
[/quote]

i think the quality output from 'imagejpeg' defaults to 75, although it shouldn't really mess things up that much.

try

imagejpeg($canvas, "", 100);

"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#10 flash gordon

flash gordon
  • Members
  • PipPipPip
  • Advanced Member
  • 150 posts

Posted 03 April 2006 - 02:34 AM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
quality is optional, and ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file). The default is the default IJG quality value (about 75).
[/quote]

thanks bully!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users