Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:imagecopyresampled返回false和URL_Php - Fatal编程技术网

PHP:imagecopyresampled返回false和URL

PHP:imagecopyresampled返回false和URL,php,Php,我正在尝试编写一个简单的文件上传程序,它可以根据需要调整图像的大小,并将其复制到服务器的文件系统中。如果我直接传递文件,这是可行的,但我也希望能够通过GET参数传递URL。不知何故,imagecopyresampled似乎在URL中失败了 这是我的密码: if(isset($_FILES["file"])) $fn = $_FILES['file']['tmp_name']; else $fn = urldecode($_GET['url']); $fileMD5 = md5_f

我正在尝试编写一个简单的文件上传程序,它可以根据需要调整图像的大小,并将其复制到服务器的文件系统中。如果我直接传递文件,这是可行的,但我也希望能够通过GET参数传递URL。不知何故,imagecopyresampled似乎在URL中失败了

这是我的密码:

if(isset($_FILES["file"]))
    $fn = $_FILES['file']['tmp_name'];
else
    $fn = urldecode($_GET['url']);
$fileMD5 = md5_file($fn);
$target = $basedir . $fileMD5 . ".png";
list($width, $height, $imgtype) = getimagesize($fn);
if ($imgtype == IMAGETYPE_PNG)
    $img = imagecreatefrompng($fn);
else if ($imgtype == IMAGETYPE_JPEG)
    $img = imagecreatefromjpeg($fn);
else if ($imgtype == IMAGETYPE_GIF)
    $img = imagecreatefromgif($fn);
else if ($imgtype == IMAGETYPE_BMP)
    $img = imagecreatefromwbmp($fn);
else {
    echo "unsupported file format";
        return;
}
// image resize
if($width > $maxwidth && $width >= $height) {
    $newwidth = $maxwidth;
    $newheight = ($height / $width) * $newwidth;
} else if($height > $maxheight) {
    $newheight = $maxheight;
    $newwidth = ($width / $height) * $newheight;
}
$tmp = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tmp, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagepng($tmp, $target);
在ImageCopy重新采样之前,一切似乎都正常。我得到了正确的IMAGETYPE,并且使用ImageCreateTureColor创建了一些东西。但是imagecopyresampled返回false。我很困惑,因为脚本似乎能够真正读取图像,并获取它的第一位来确定它的类型。 有人怀疑出了什么事吗


提前感谢。

您的调整大小部分似乎忽略了一些可能导致未定义$newheight和$newwidth的情况


您确定ImageCreateTureColor会返回图像吗?

不需要$fn=urldecode$\u GET['url'];-PHP在初始化过程中自动解码$\u GET和$\u POST以及$\u REQUEST中的值,您正在对其进行双重解码,这几乎肯定不是您想要的。此外,您将远程文件传输3次—一次是在调用md5_文件时,一次是在调用getimagesize时,一次是在调用imagecreatefrompng时。您应该先下载到临时文件,然后使用本地副本,完成后删除临时文件。当然,这就是问题所在。我没想到。非常感谢。