Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge images in PHP - GIF and JPG

Tags:

php

jpeg

gif

gd

I am trying to merge two images - a GIF image with a smaller JPG image. The output should be GIF.

The issue is that GIF image colors remain correct, but the colors of the JPG image are altered.

The GIF image has only 256 colors (8-bit), but is there a way to make the merged image to be a true-color resource which later can be converted to a 8-bit GIF for output?


Issue solved.

I updated the code. Here is the solution which works fine:

<?php

header('Content-Type: image/gif');

$gif_address = 'file.gif';
$jpg_address = 'file.jpg';

$image1 = imagecreatefromgif($gif_address);
$image2 = imagecreatefromjpeg($jpg_address);

$merged_image = imagecreatetruecolor(800, 800);
imagecopymerge($merged_image, $image1, 0, 0, 0, 0, 800, 800, 100);
imagecopymerge($merged_image, $image2, 0, 0, 0, 0, 500, 500, 100);

imagegif($merged_image);

imagedestroy($image1);
imagedestroy($image2);
imagedestroy($merged_image);

?>
like image 425
acoder Avatar asked Oct 22 '11 20:10

acoder


1 Answers

From your explanation (some code would help), i would hazard a guess you are merging the jpeg onto the gif. Id say the easiest way is to use imageCreateTrueColor to create a new image the size you need then use imagecopy to copy the GIF into this new image. Merge the jpg onto this and then at a later date you can covert the true colour image to a gif.

If im missing something a bit of example code of what you are currently doing might help.

like image 133
Adrian Brown Avatar answered Sep 29 '22 07:09

Adrian Brown