php - 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 错误,并且尽我所能告诉服务器应该支持它。
解决方案
这是我的问题:我正在调用不存在的变量。我不知道这几天是如何逃避我的,但事实就是如此,现在它可以完美运行。
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
推荐阅读
- python - pySpark 根据列表检查列是否存在
- vue.js - Nuxt.js:是否可以在预渲染选项中使用自定义页面名称?
- typescript - TS2322:类型不可分配给带有箭头功能的类型
- amazon-web-services - AWS Elastic Beanstalk 正确配置安全组无法连接到上游
- ios - CHHapticEngine 未运行
- javascript - 用三个 js 意外排序反应 mapbox-gl 自定义层
- c# - 使用 BitConverter.GetBytes() 在 C# 中将 Int16 转换为 UByte[] 不起作用
- python - 如何在 Windows 中将凭据设置为环境变量?
- javascript - 尝试使用 Node.js 传递键值对的 EJS 参考错误
- intellij-idea - IntelliJ IDEA 2020.1(终极版)挂了我的Ubuntu 18.04 LTS