0

I've been held back by this problem for a few days now

I have this code which is queried by AJAX with a valid link to a Pixabay Image from their API. This is then resized to 640x420 to fit in image containers around the site. The site resizes it, saves it, then returns the UUID to AJAX. The problem seems to be originating with the imageresizeresampled not executing. A new image is created and saved to a variable to be saved, but is not overwritten with a resized copy.

<?php
  //user authentication would go here
  //loading shared API would go here
  $q = $_REQUEST['q'];
  
  //other use cases for this API would go here
  if($q=="getImage") {
    $pixabay = $_REQUEST['link'];
    $url = gen_uuid();
    $suffix = $_SERVER['DOCUMENT_ROOT']."/temp/";
    $filename = $suffix.$url.".jpg";

    $image = file_get_contents($pixabay);
    file_put_contents($filename, $image);

    $source_image_tmp = imagecreatefromjpeg($filename);
    $source_image = imagecreatetruecolor(imagesx($source_image_tmp),imagesy($source_image_tmp));
    imagecopy($source_image,$source_image_tmp,0,0,0,0,imagesx($source_image_tmp),imagesy($source_image_tmp));

    $origx = imagesx($source_image);
    $origy = imagesy($source_image);

    $dest_imagex = 640;
    $dest_imagey = 420;

    $dest_image = imagecreatetruecolor($dest_imagex, $dest_imagey);

    imagecopyresampled($dest_image, $source_image, 0, 0, 0, 0, $dest_imagex, $dest_imagey, $source_imagex, $source_imagey);

    imagejpeg($dest_image, $filename);  

    die($url);
  }
?>

As far as I can tell, the $dest_image is created, but never overwritten by imagecopyresampled, so it just returns a black 640x420 box. No PHP errors are returned, and as best as I can tell the server is supposed to support it.

DrYamok
  • 26
  • 1
  • 3

1 Answers1

0

Here is what my problem was: I was calling variables that did not exist. I have no idea how this evaded me for days on end, but that was the case, and it now works perfectly.

imagecopyresampled($dest_image, $source_image, 0, 0, 0, 0, $dest_imagex, $dest_imagey, $source_imagex, $source_imagey);

Became

imagecopyresampled($dest_image, $source_image, 0, 0, 0, 0, $dest_imagex, $dest_imagey, $origx, $origy);

Where $source_imagex and $sourceimagey become $origx and $origy

DrYamok
  • 26
  • 1
  • 3