imagecopyresampled似乎没有执行

时间:2018-08-07 13:16:09

标签: php ajax image image-manipulation pixabay

我被这个问题困扰了几天了

我有此代码,AJAX会查询此代码,并提供一个有效链接,可从其API链接到“ Pixel图片”。然后将其大小调整为640x420,以适合站点周围的图像容器。该站点将调整大小,保存大小,然后将UUID返回给AJAX。问题似乎是由于重新执行imageresize采样而导致的。创建新图像并将其保存到要保存的变量中,但是不会被调整大小的副本覆盖。

<?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);
  }
?>

据我所知,$ dest_image已创建,但从未被imagecopyresampled覆盖,因此它仅返回黑色的640x420框。没有PHP错误返回,尽我所能告诉服务器应该支持它。

1 个答案:

答案 0 :(得分:0)

这是我的问题所在:我正在调用不存在的变量。我不知道这种情况如何持续几天让我回避,但事实就是如此,它现在可以正常运行。

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

蜜饯

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

其中$ source_imagex和$ sourceimagey变为$ origx和$ origy