dongshengli6384 2018-08-07 13:16
浏览 19
已采纳

imagecopyresampled似乎没有执行

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.

</div>
  • 写回答

1条回答 默认 最新

  • duanouyong4228 2018-08-07 15:32
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集