1

I'm using Creating image function to create an image with exact text message using the following code

<?PHP
header ("Content-type: image/gif");
$image=imagecreatefromgif("myimage.gif"); // will be background img
$black = imagecolorallocate($image, 0,0,0);
$message = "Hello Egypt";
imagestring($image, 4, 25, 10, $message, $black);
imagegif($image);
imagedestroy($image);
?>

The output should be like this

Hello Egypt

Now my question is there any way i can write image over it not only text so that if i've flag image at same path (flag.gif Flag) and i would like to write it just after my $message to be like this

Hello Egypt Flag

so is this possible and how could be ! ~ thanks a lot

Update based on @MarcB idea of using imagecopy function

<?PHP
header ("Content-type: image/gif");
$image=imagecreatefromgif("myimage.gif"); // will be background img


$src = imagecreatefromjpeg('flag.jpg');// new image will add
imagecopy($image, $src, 120, 10, 0, 0, 32, 20);

$black = imagecolorallocate($image, 0,0,0);
$message = "Hello Egypt";
imagestring($image, 4, 25, 10, $message, $black);

imagegif($image);
imagedestroy($image);
?>

the output is not true color WHY :(

enter image description here

ANY help about this new problem ~ thanks

5
  • 2
    php.net/imagecopy Commented Jun 12, 2012 at 21:34
  • thanks never knew about imagecopy before but i'll try to combine both code hope it works ~ thanks a lot Commented Jun 12, 2012 at 21:54
  • @MarcB i've updated the code please view it ! why i'm getting it not true color ! Commented Jun 12, 2012 at 22:19
  • 4
    Since the image is a .gif, the colors you need may not be in the gif palette. Can you make it a .jpg file instead? Commented Jun 12, 2012 at 22:20
  • @SurrealDreams thanks a lot that was very helpful too , i've changed to .jpg and replaced imagecreatefromgif to imagecreatefromjpeg and it works perfect ~ thanks Commented Jun 12, 2012 at 22:27

2 Answers 2

4

With the help of Surreal Dreams and Marc B This one works fine

<?PHP
header ("Content-type: image/gif");

$image=imagecreatefromjpeg("myimage.jpg"); // will be background img

$src = imagecreatefromjpeg('flag.jpg');// new image will add

imagecopy($image, $src, 120, 10, 0, 0, 32, 20);


$black = imagecolorallocate($image, 0,0,0);
$message = "Hello Egypt";

imagestring($image, 4, 25, 10, $message, $black);
imagegif($image);
imagedestroy($image);
?>

Output

enter image description here

I've should have learned the following functions

  1. imagecopy
  2. imagecreatefromjpeg
  3. imagecreatefromgif
Sign up to request clarification or add additional context in comments.

1 Comment

If this is the solution, don't forget to accept it. It's OK to accept your own solution as long as it the one that works best.
1

imagecopy() should properly deal with differences between images' palettes; however, the GIF format does not support more than 256 colors, and neither does GD when it works with palette-based images. If 256 palette entries already exist when GD tries to use a new color, GD will pick the closest match, which can produce the results you see.

To avoid this problem, you should use imagecreatetruecolor() to create a 24-bit true-color image in memory. You can then use imagecopy() to insert each GIF image (including the background) and imagepng() to generate PNG output, which is better for line art than JPEG, offers better compression than GIF, and can support more than 256 colors.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.