加入收藏 | 设为首页 | 会员中心 | 我要投稿 银川站长网 (https://www.0951zz.com/)- 云通信、基础存储、云上网络、机器学习、视觉智能!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php按指定大小等比缩放组成上传图片缩略图

发布时间:2023-05-31 13:01:20 所属栏目:PHP教程 来源:
导读:php按指定大小等比缩放组成上传图片缩略图:/** * * *等比缩放 * @param unknown_type $srcImage 源图片路径 * @param unknown_type $toFile 目标图片路径 * @param unknown_type $maxWidth 最大宽 *

php按指定大小等比缩放组成上传图片缩略图:

/** 

 * * 

 *等比缩放 

 * @param unknown_type $srcImage   源图片路径 

 * @param unknown_type $toFile     目标图片路径 

 * @param unknown_type $maxWidth   最大宽 

 * @param unknown_type $maxHeight  最大高 

 * @param unknown_type $imgQuality 图片质量 

 * @return unknown 

 */ 

function resize($srcImage,$toFile,$maxWidth = 100,$maxHeight = 100,$imgQuality=100) 

    list($width, $height, $type, $attr) = getimagesize($srcImage); 

    if($width < $maxWidth  || $height < $maxHeight) return ; 

    switch ($type) { 

    case 1: $img = imagecreatefromgif($srcImage); break; 

    case 2: $img = imagecreatefromjpeg($srcImage); break; 

    case 3: $img = imagecreatefrompng($srcImage); break; 

    } 

    $scale = min($maxWidth/$width, $maxHeight/$height); //求出绽放比例 

    if($scale < 1) { 

    $newWidth = floor($scale*$width); 

    $newHeight = floor($scale*$height); 

    $newImg = imagecreatetruecolor($newWidth, $newHeight); 

    imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); 

    $newName = ""; 

    $toFile = preg_replace("/(.gif|.jpg|.jpeg|.png)/i","",$toFile); 

    switch($type) { 

        case 1: if(imagegif($newImg, "$toFile$newName.gif", $imgQuality)) 

        return "$newName.gif"; break; 

        case 2: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality)) 

        return "$newName.jpg"; break; 

        case 3: if(imagepng($newImg, "$toFile$newName.png", $imgQuality)) 

        return "$newName.png"; break; 

        default: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))//开源代码Cuoxin.com 

        return "$newName.jpg"; break; 

    } 

    imagedestroy($newImg); 

    } 

    imagedestroy($img); 

    return false; 

(编辑:银川站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!