2

Can anyone tell me why the following code doesn't work? It outputs images with ZERO bytes -- but here's the kicker: the below code works correctly (outputs images with a normal number of bytes) when I change "png" to "jpg" or "jpeg". The src file for the png code is bigpic.png, which contains a simple butterfly icon with a transparent background. I have an include that creates the transparency, however that's not causing the 0-bytes problem, since the script outputs 0-byte images when when I omit that particular include.)

Thanks for any ideas.

<?php

$x=$_REQUEST['x'];
 $y=$_REQUEST['y'];
 $x_change=$_REQUEST['x_change'];
 $y_change=$_REQUEST['y_change'];
 $numberofpics=$_REQUEST['numberofpics'];
$filename=$_REQUEST['filename'];
$c=0;


//SRC FILE IS A 1040x1040 PICTURE OF A BUTTERFLY ICON WITH TRANSPARENT BACKGROUND


while ($c<$numberofpics) {

        $src=$_REQUEST['src'];

        $src = imagecreatefrompng($src);

        $src_width=imagesx($src);

        $src_height=imagesy($src);

        $dest = imagecreatetruecolor(640, 360);

                $c=$c+1;



imagecopy($dest, $src, 0, 0, $x, $y, 640, 360);

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

$thisfilename=$filename.$c.".png";

imagepng($dest,$thisfilename,100);

imagepng($dest);

imagedestroy($dest);

imagedestroy($src);

$x=$x+$x_change;

$y=$y+$y_change;

                                }

?>
miken32
  • 42,008
  • 16
  • 111
  • 154
  • Could you post a second snippet with you change from `png` to `jpg`? There a few there just interested if you might have missed one. – redreddington Mar 08 '15 at 20:43

2 Answers2

0

Your problem is in the third parameter (quality) of imagepng, which allow you to set compression level from 0 (no compression) to 9.

You can try to comment header line to see the error.

Update

Since I found several bug in the code that you have, I have modified a bit:

<?php
$x = $_REQUEST['x'];
$y = $_REQUEST['y'];
$x_change = $_REQUEST['x_change'];
$y_change = $_REQUEST['y_change'];
$numberofpics = $_REQUEST['numberofpics'];
$filename = $_REQUEST['filename'];
$src = $_REQUEST['src'];
$src = imagecreatefrompng($src);

for ($c = 0; $c < $numberofpics; ++$c) {
  $dest = imagecreatetruecolor(640, 360);
  imagecopy($dest, $src, 0, 0, $x, $y, 640, 360);
  header('Content-Type: image/png');

  imagepng($dest, $filename . $c . '.png', 9);
  imagedestroy($dest);

  $x += $x_change;
  $y += $y_change;
}

imagedestroy($src);
Community
  • 1
  • 1
Fong-Wan Chau
  • 2,259
  • 4
  • 26
  • 42
  • That's it, Fong-Wan, thanks. I hadn't realized that the acceptable quality parameters for jpegs are different than the quality parameters for pngs. – user2116163 Mar 08 '15 at 22:10
0

You need to preserve transparency. Add those lines after creating $dest image by imagecreatetruecolor function:

imagealphablending($dest, false);
imagesavealpha($dest, true);
n-dru
  • 9,285
  • 2
  • 29
  • 42