1

This is my code:

$data = json_decode($_REQUEST['data'], true);

for ($x = 0; $x <= count($data); $x++) {    

    $img = imagecreate($data[$x][0],$data[$x][1]);
    $img2 =  imagecreatefromjpeg("apple.jpg");

    $background_color = imagecolorallocate($img, 0, 0, 0);

    imagecopymerge($img, $img2, $data[$x][2],$data[$x][3],0,0,$data[$x][4],$data[$x][4],100);

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

    imagepng($img, "icon".$x."png");
    imagedestroy($img);
    imagedestroy($img2);

}

$data gives back the correct data so no problems there, but the outcome are black squares with blank squares(instead of the image) in them. Does someone know how I can make the smaller square the actual image I'm trying to use?

Martin
  • 22,212
  • 11
  • 70
  • 132
  • I think that the path to your file `apple.jpg` might be in a different place than the script. Are they in the same folder? If you use a direct link to a random image on the internet, is the result the same? – Virginia Dec 07 '18 at 14:37
  • What does your [PHP Error log](https://stackoverflow.com/questions/5127838/where-does-php-store-the-error-log-php5-apache-fastcgi-cpanel) tell you? – Martin Dec 07 '18 at 14:38
  • You realise your `imagepng($img, "icon".$x."png");` will NOT output to screen but instead try and save the data to the file? – Martin Dec 07 '18 at 14:40
  • And why are you not using [`imagecreatetruecolor()`](http://php.net/manual/en/function.imagecreatetruecolor.php)? – Martin Dec 07 '18 at 14:54
  • @Martin apparently "imagecreate" returns a "boolean" instead of a "resource".. any ideas on why it does this?.. (imagecreatetruecolor() gives the same result) – gerben schipper Dec 07 '18 at 15:01
  • what are the values of `$data` that it's using? – Martin Dec 07 '18 at 15:16
  • @Martin the values of data before decoding are a bunch of arrays containing numbers like this:[200,200,44.99999999999999,44.99999999999999,110.00000000000001], so this is $data[0] – gerben schipper Dec 07 '18 at 15:35
  • Yes... so what is `$data[1][0]`, `$data[1][1]` etc. These numbers should be integers. so use `imagecreate((int)$data[$x][0], (int)$data[$x][1]);` to force the floating numbers to be integers – Martin Dec 07 '18 at 15:44
  • @Martin I figured out what the problem is, I am giving the image a height and width but the image doest resize, it just takes the amount of pixels you entered and cuts a piece out of the top left corner... how do I fix this?? – gerben schipper Dec 10 '18 at 08:40
  • @gerbenschipper use `imagecopyresample` rather than `-merge` – Martin Dec 10 '18 at 09:41
  • @Martin Now the image does't resize at all, even though I give it a width and height – gerben schipper Dec 10 '18 at 09:56

0 Answers0