PHP 生成图片缩略图函数

各位小盆友使用前记得打开 GD 库的支持哦,附上代码。

    1. <?php  
    2. /** 
    3.  * 生成缩略图函数(支持图片格式:gif、jpeg、png和bmp) 
    4.  * @author ruxing.li 
    5.  * @param  string $src      源图片路径 
    6.  * @param  int    $width    缩略图宽度(只指定高度时进行等比缩放) 
    7.  * @param  int    $width    缩略图高度(只指定宽度时进行等比缩放) 
    8.  * @param  string $filename 保存路径(不指定时直接输出到浏览器) 
    9.  * @return bool 
    10.  */  
    11. function mkThumbnail($src, $width = null, $height = null, $filename = null) {  
    12.     if (!isset($width) && !isset($height))  
    13.         return false;  
    14.     if (isset($width) && $width <= 0)  
    15.         return false;  
    16.     if (isset($height) && $height <= 0)  
    17.         return false;  
    18.   
    19.     $size = getimagesize($src);  
    20.     if (!$size)  
    21.         return false;  
    22.   
    23.     list($src_w, $src_h, $src_type) = $size;  
    24.     $src_mime = $size['mime'];  
    25.     switch($src_type) {  
    26.         case 1 :  
    27.             $img_type = 'gif';  
    28.             break;  
    29.         case 2 :  
    30.             $img_type = 'jpeg';  
    31.             break;  
    32.         case 3 :  
    33.             $img_type = 'png';  
    34.             break;  
    35.         case 15 :  
    36.             $img_type = 'wbmp';  
    37.             break;  
    38.         default :  
    39.             return false;  
    40.     }  
    41.   
    42.     if (!isset($width))  
    43.         $width = $src_w * ($height / $src_h);  
    44.     if (!isset($height))  
    45.         $height = $src_h * ($width / $src_w);  
    46.   
    47.     $imagecreatefunc = 'imagecreatefrom' . $img_type;  
    48.     $src_img = $imagecreatefunc($src);  
    49.     $dest_img = imagecreatetruecolor($width, $height);  
    50.     imagecopyresampled($dest_img, $src_img, 0, 0, 0, 0, $width, $height, $src_w, $src_h);  
    51.   
    52.     $imagefunc = 'image' . $img_type;  
    53.     if ($filename) {  
    54.         $imagefunc($dest_img, $filename);  
    55.     } else {  
    56.         header('Content-Type: ' . $src_mime);  
    57.         $imagefunc($dest_img);  
    58.     }  
    59.     imagedestroy($src_img);  
    60.     imagedestroy($dest_img);  
    61.     return true;  
    62. }  
    63.   
    64. $result = mkThumbnail('./IMG_3324.JPG', 147, 147); 

转载于:https://www.cnblogs.com/taozi32/p/5364143.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值