By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,645 Members | 1,048 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,645 IT Pros & Developers. It's quick & easy.

Having trouble with imagecopymerge() with gifs and imagettftext()

P: 6
Hello,

I'm trying to make a gif image out of a total of 5 (currently) images. One background image and 4 small icons.

This image also includes text of a specific font.

Here are my issues.

First... the background image is in color and looks correct. The icons that I merge onto the background image are in black and white. I'm not sure why this is happening. This happens on both my development environment and on my server.

Second... The text is working in my development environment, but when I move the file to the server, the text does not display.

Development Environment:
Win XP Pro SP2
PHP 4.4.4
GD 2.0.28

Server
Linux
PHP 5.2.0
GD 2.0.28

I should probably mention that when I use png for all of the images it works fine in both my development environment and on my server, however I'm trying to use gif files for transparency reasons since IE doesn't handle png transparencies.

[php]<?php
header ("Content-type: image/gif");
$id = $_GET['id'];
if ( strlen( $id) < 1 ) {
$id= "MissingID";
}

// Defining the background image.
$background = imagecreatefromgif("ps3gamertag-blankgif.gif");

// colors to possibly be used later
$white = imagecolorallocate($background, 255, 255, 255);
$almostWhite = imagecolorallocate( $background, 247, 247, 247 );

// Defining the icons to be added or combined.
$insert = imagecreatefromgif( "icon1.gif" );
$insert2 = imagecreatefromgif( "icon2.gif" );
$insert3 = imagecreatefromgif( "icon3.gif" );
$insert4 = imagecreatefromgif( "icon4.gif" );

// Get icon width and hight for later use
// all icons are the same size
$insert_x = imagesx($insert);
$insert_y = imagesy($insert);

// Combine the images into a single output image.
imagecopymerge($background,$insert4,312,45,0,0,$in sert_x,$insert_y,99);
imagecopymerge($background,$insert2,350,45,0,0,$in sert_x,$insert_y,99);
imagecopymerge($background,$insert3,388,45,0,0,$in sert_x,$insert_y,99);
imagecopymerge($background,$insert,426,45,0,0,$ins ert_x,$insert_y,99);

// Add id text
imagettftext( $background, 12, 0, 35, 76, $almostWhite, "Concf.ttf", $id);

// Output the results as a gif image, to be sent to users browser.
imagegif($background);
imagedestroy($background);
?>[/php]

Thoughts?
Jun 12 '07 #1
Share this Question
Share on Google+
9 Replies


Motoma
Expert 2.5K+
P: 3,235
The imagecreatefromgif may be imposing color restrictions on your original image. Just create an image with imagecreate and draw a colored rectangle for your background.
Jun 12 '07 #2

P: 6
The imagecreatefromgif may be imposing color restrictions on your original image. Just create an image with imagecreate and draw a colored rectangle for your background.
Before posting here, I tried this...

[php]
$tempimage = imagecreatetruecolor($insert_x, $insert_y);
// copy the 8-bit gif into the truecolor image
imagecopy($tempimage, $insert2, 0, 0, 0, 0,$insert_x, $insert_y);
// copy the source_id int
$insert2 = $tempimage;[/php]

That didn't work.
I also tried it using imagecreate() instead of imagecreatetruecolor().
Do I need to set a color to the image first beforehand?

Any thoughts on the text?
Jun 12 '07 #3

Motoma
Expert 2.5K+
P: 3,235
I think this line:
$background = imagecreatefromgif("ps3gamertag-blankgif.gif");
Is giving you the problem.
I believe that your target's color depth ($background's color depth) is set wrong. Try replacing that with createimagetruecolor.
Jun 12 '07 #4

P: 6
I think this line:
$background = imagecreatefromgif("ps3gamertag-blankgif.gif");
Is giving you the problem.
I believe that your target's color depth ($background's color depth) is set wrong. Try replacing that with createimagetruecolor.
Motoma... Thanks a lot!

This worked for me! Unfortunatly it brought up another problem.
My icons are now showing in color.
My text is now showing up too.
However, transparencies are no longer working.

Here's what I've tried so far.

[php] $background = imagecreatefromgif("blankgif.gif");
$background_x = imagesx( $background );
$background_y = imagesy( $background );

$tempimage = imagecreatetruecolor($background_x, $background_y);
imagesavealpha($tempimage, true);
$trans_colour = imagecolorallocatealpha($tempimage, 255, 0, 0, 127);
imagefill($tempimage, 0, 0, $trans_colour);
imagecopymerge($tempimage, $background, 0, 0, 0, 0,$background_x, $background_y,99);
$background = $tempimage;[/php]

I also tried this...

[php] $background = imagecreatefromgif("blankgif.gif");
$background_x = imagesx( $background );
$background_y = imagesy( $background );

$tempimage = imagecreatetruecolor($background_x, $background_y);
$tempcolor = imagecolorallocate($tempimage, 255, 0, 0);
imagefill($tempimage, 0, 0, $tempcolor);
imagecolortransparent($tempimage, $tempcolor);
imagecopymerge($tempimage, $background, 0, 0, 0, 0,$background_x, $background_y,99);
$background = $tempimage; [/php]

I'm still looking for other stuff to try, but if you have any suggestions... I'd be very appreciative.

I'm sorry to keep bothering you with this stuff, but I'm still trying to figure out all the little quirks of GD.
Jun 12 '07 #5

Motoma
Expert 2.5K+
P: 3,235
However, transparencies are no longer working.

I'm still looking for other stuff to try, but if you have any suggestions... I'd be very appreciative.

I'm sorry to keep bothering you with this stuff, but I'm still trying to figure out all the little quirks of GD.
When you say transparencies are no longer working, do you mean for the base image ($background), or the images you are placing on top? You may need to enable transparency on all images.

You don't have to apologize for asking questions, that's what we are here for!
Jun 12 '07 #6

P: 6
When you say transparencies are no longer working, do you mean for the base image ($background), or the images you are placing on top? You may need to enable transparency on all images.

You don't have to apologize for asking questions, that's what we are here for!
Thanks a lot! :)

I mean the resulting image. The $background image is built on a transparency. the icons are placed on top of the $background. The createimage() is what I want to now be transparent and the two methods I posted did not work.

Any ideas?
Jun 12 '07 #7

Motoma
Expert 2.5K+
P: 3,235
Thanks a lot! :)

I mean the resulting image. The $background image is built on a transparency. the icons are placed on top of the $background. The createimage() is what I want to now be transparent and the two methods I posted did not work.

Any ideas?
Taking a look at your second example from the last code you posted:
You may either need to use imagealpha() on $background, or use imagecolortransparent() on a color from $background. You see, you are grabbing a color index from one image, and referring to that color index in a different image. This may be the source of your problem.
Jun 12 '07 #8

P: 6
Taking a look at your second example from the last code you posted:
You may either need to use imagealpha() on $background, or use imagecolortransparent() on a color from $background. You see, you are grabbing a color index from one image, and referring to that color index in a different image. This may be the source of your problem.
*sigh* This was frustrating Motoma

You were absolutely right.
I was using the temp image when defining the color to make the transparency. When I changed that... it worked... ... ... in my dev env.

As soon as I transfered it over to the server. The background color returned.
So I did some more research and got it going.

For some reason, when I filled the createimage() with a statically defined color, and then tried to transparent it, it wouldn't work. But if I went and got the color at 0,0 on the back ground image, set that to the temp color, filled the createimage with that, then transparent it, it works.

Anyway... here's the result...

[php] $background = imagecreatefromgif("blankgif.gif");
$background_x = imagesx( $background );
$background_y = imagesy( $background );

$tempimage = imagecreatetruecolor($background_x, $background_y);
$tempcolor = imagecolorat($background, 0, 0);
imagefill($tempimage, 0, 0, $tempcolor);
imagecopymerge($tempimage, $background, 0, 0, 0, 0,$background_x, $background_y, 99);
$background = $tempimage;
imagecolortransparent($background, $tempcolor); [/php]

Thanks a lot for all your help. I now know where to come for advice when I'm having issues!

Chris
Jun 12 '07 #9

Motoma
Expert 2.5K+
P: 3,235
Boy, I am glad you were able to get your solution. Come back any time you have a question.
Jun 12 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.