PHP:imagecopyresampled返回false和URL

j0k*_*ker 2 php

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

在imagecopyresampled之前,一切似乎都能正常工作.我得到了正确的IMAGETYPE,并且使用imagecreatetruecolor创建了一些东西.但imagecopyresampled返回false.我很困惑,因为脚本似乎能够实际读取图像并获得它的第一位以确定它的类型.什么出问题?

提前致谢.

jer*_*oen 5

您的调整大小部分似乎遗漏了一些可能导致未定义$newheight$newwidth.

你确定要imagecreatetruecolor回复一张图片吗?