duanque2413 2015-01-27 04:13
浏览 39

PHP从上传的图像创建3个不同大小的缩略图图像

I'm trying to create 3 Thumbnail Images of different sizes, from the same uploaded Image. Presently, I use the code below to create 1 thumbnail that's 150px wide.

Is there an easy way to do this instead of repeating the same code thrice for each thumbnail I need?

I'm trying to create thumbnails in 3 sizes: 750px wide, 150px wide and 70px wide.

Here's the code I use to do the Thumbnail that's 150Px wide.

Does PHP have a function to do such a thing, or is repeating the code below for each size I need my only option.

if(file_exists($thisImage)) {

    $imageName = $thisImage;
    $imageInfo = finfo_open(FILEINFO_MIME_TYPE);
    $imageType = finfo_file($imageInfo, $imageName);
    finfo_close($imageInfo);


    if($imageType == 'image/pjeg' || $imageType == 'image/jpeg' || $imageType == 'image/jpg') {
        $imgSource = imagecreatefromjpeg($thisImage);
    } elseif ($imageType == 'image/png') {
        $imgSource = imagecreatefrompng($thisImage);
    } elseif ($imageType == 'image/gif') {
        $imgSource = imagecreatefromgif($thisImage);
    } else {
        $imgSource = false;
        return false;
    }

    if($imgSource) {
        list($width,$height)=getimagesize($thisImage);

        $thumbImageWidth = 150;
        $thumbImageHeight = ($height/$width)*$thumbImageWidth;
        $tempThumbImage = imagecreatetruecolor($thumbImageWidth,$thumbImageHeight);

        if(!imagecopyresampled($tempThumbImage,$imgSource,0,0,0,0,$thumbImageWidth,$thumbImageHeight,$width,$height)) return false;

        $thumbImageTarget = $thisPath.$thisName;

        if(!imagejpeg($tempThumbImage,$thumbImageTarget,100)) return false;

        if(!imagedestroy($imgSource)) return false;

        if(!imagedestroy($tempThumbImage)) return false;

        if(!unlink($thisImage)) return false;

        return true;
    }
} else {
    return false;
}
  • 写回答

2条回答 默认 最新

  • dongshanjin8947 2015-01-27 04:29
    关注

    PHP dont have that function. But you can create a function named resize($width, $heigh) and put your resize part in.

    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计