duangewu5234 2011-12-13 15:35
浏览 99
已采纳

PHP:imagecopyresampled返回false和URL

I'm trying to write a simple file uploader which can resize an image if needed and copy it into the server's file system. That works if I pass the file directly but I also want to be able to pass a URL via a GET parameter. And somehow imagecopyresampled seems to fail with the URL.

Here is my code:

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);

Everything seems to work fine until imagecopyresampled. I get the correct IMAGETYPE and also, something is created with imagecreatetruecolor. But imagecopyresampled returns false. I'm pretty confused since the script seems to be able to actually read the image and get it's first bits to determine the type of it. Any suspections what's going wrong?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • douyan2970 2011-12-13 15:41
    关注

    Your resize section seems to leave out some situations that could lead to an undefined $newheight and $newwidth.

    Are you sure about imagecreatetruecolor returning an image?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c