PHP: imagecopyresampled returns false and URLs


PHP: imagecopyresampled returns false and URLs

我正在尝试编写一个简单的文件上传程序,可以根据需要调整图像的大小并将其复制到服务器的文件系统中。如果我直接传递文件,这是有效的,但我也希望能够通过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);

一切似乎都很好,直到imagecopyresample。我得到了正确的IMAGETYPE,同时,用imagecreatetruecolor创建了一些东西。但是imagecopyresample返回false。我很困惑,因为脚本似乎能够实际读取图像,并得到它的第一个比特,以确定它的类型。有人怀疑出了什么问题吗?

你的resize部分似乎遗漏了一些可能导致未定义的$newheight$newwidth的情况。

您确定imagecreatetruecolor返回图像吗?