首页 > 解决方案 > imagecopyresampled 似乎没有执行

问题描述

我已经被这个问题拖了几天了

我有这个由 AJAX 查询的代码,其中包含从他们的 API 到 Pixabay 图像的有效链接。然后将其调整为 640x420 以适应站点周围的图像容器。站点调整它的大小,保存它,然后将 UUID 返回给 AJAX。问题似乎源于 imageresizeresampled 未执行。创建新图像并将其保存到要保存的变量中,但不会被调整大小的副本覆盖。

<?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 错误,并且尽我所能告诉服务器应该支持它。

标签: phpajaximageimage-manipulationpixabay

解决方案


这是我的问题:我正在调用不存在的变量。我不知道这几天是如何逃避我的,但事实就是如此,现在它可以完美运行。

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


推荐阅读