2

I'm trying to merge two png images using the following code:

$imgl = "myFirstImage.png";
$img2 = "mySecondImage.png";

// Create image instances
$dest = imagecreatefrompng($imgl);
$src = imagecreatefrompng($img2);
imagecolortransparent($src, imagecolorat($src, 0, 0));

// Copy and merge
$src_x = imagesx($src);
$src_y = imagesy($src);
imagecopymerge($dest, $src, 0, 0, 0, 0, $src_x, $src_y, 100);

// Output and free from memory
header('Content-Type: image/png');
imagepng($dest);

imagedestroy($dest);
imagedestroy($src);

However, the output is total junk. It looks something like this:

‰PNG IHDR#5P IDATxœìÝÏ“dÕy7ø›ÕD¨xÛ¶d1~Ýt´»±(Gmýxß DH[-g´°ý‡X+ë‘´ðš­&mf4²ˆQlC»aÔ´°©±•]•³H”¤2³²nÞ¼÷žçœóù„ÃÑ¢»«NWeÝsóùÞç9“ù|Þtr”z@Æ$ @w—S/Ho2™œ÷[¬»M”¡;"„~¹’@µ$ PŽÑr…–\^ ’ÈXïÑ“O=ßí/¾õæ+þW(’¤2sxºÐ9Nèæ¼ÂÅÊ i€’.Œœ+´±™=¸@¾$ W·€!´°ÃZêàŠÙ‘4@8ûyE;¬¦.MIÒ>c(&]ØJä‘4@z†óˆ >I¤Ô2c¨-Ø´Œ\² I$ èl9¸p@’U›ŒAÀp!-‡¤FraÆØ—¼"4Ààvg†Ã½õæ+.eФ$c“¼’4@ÿJJÅyÑ0>IôlGÌ `ÇjsÃ÷¯Þü›ûï¤]”MÒ½‘1„²È¾õfÓ4ÂŽ¤z cˆéøá+ß¹÷Þâ......[goes on for pages]

I thought it might be a header problem, but that's set as a png in the code. How do I fix this so it renders an image?

user2755541
  • 536
  • 2
  • 10
  • 25
  • Try with full path of the image like `$imgl = "http://www.example.com/myFirstImage.png";` – prava Jul 16 '14 at 18:10
  • Do you see the correct header when looking at the response? – Bart Jul 16 '14 at 18:12
  • try adding a `@` to `imagecreatefrompng`. Like this `@imagecreatefrompng($img1);` http://php.net/manual/en/function.imagecreatefrompng.php – rob Jul 16 '14 at 18:13
  • Never use `@` unless absolutely necessary. Turn on your error reporting: http://php.net/manual/en/function.error-reporting.php and you should see where the errors occur. – Aziz Saleh Jul 16 '14 at 18:15
  • The code works fine for me. Is there output above the code in question? – labue Jul 16 '14 at 18:18
  • 4
    Are you sure you don't have any whitespace at the beginning of the file? – Maury Jul 16 '14 at 18:21
  • I tried the full path and it didn't change anything. Bart, what do you mean when you say the correct header when looking at the response? – user2755541 Jul 16 '14 at 18:38
  • @Maury can you elaborate? That may be a problem. – user2755541 Jul 16 '14 at 18:38
  • @Maury - that worked!! I guess I didn't realize it had to be the very first thing in the file. – user2755541 Jul 16 '14 at 18:40
  • It doesn't have to be the very first thing in the file. You just can't have any space before an opening tag. Like ` – Maury Jul 16 '14 at 19:55
  • Also check this: http://stackoverflow.com/questions/17424611/header-content-type-charset-utf-8-with-bom If someone happens to have the same problem and their file does not start with a space, make sure your file is saved without BOM. Saving with a "Byte-Order-Mark", results in failing PHP headers. – JohannesB Aug 05 '14 at 01:50

1 Answers1

0

I use these headers for images:

header('Content-type: image/png');
header('Content-transfer-encoding: binary');

I can see you use the 'Content-type' header, but the 'Content-transfer-encoding' header is missing. Perhaps that's the problem? Otherwise close your browser (completely) and open it again, I have found that in some cases that helps. This is a cache problem because a 'file-date' is missing. You can add it, like this:

header('Last-Modified: '.gmdate('D, d M Y H:i:s',time().' GMT'); 

This will inform the cache of the browser, every time, that the file has changed.

KIKO Software
  • 15,283
  • 3
  • 18
  • 33