php按照指定的尺寸压缩图片

复制代码
    /**     
     * * 按照指定的尺寸压缩图片     
     * * @param $source_path  原图路径
     * * @param $imgWidth     目标宽度
     * * @param $imgHeight    目标高度
     * * @return bool|string     
    */    
    function resize_image($source_path, $imgWidth, $imgHeight)
    {        
        $source_info = getimagesize($source_path);        
        $source_mime = $source_info['mime'];        
        switch ($source_mime) {            
            case 'image/gif':
                $source_image = imagecreatefromgif($source_path);                
                break;
            case 'image/jpeg':
                $source_image = imagecreatefromjpeg($source_path);                
                break;
            case 'image/png':
                $source_image = imagecreatefrompng($source_path);                
                break;
            default:
                return false;
                break;        
        }
        $target_image  = imagecreatetruecolor($imgWidth, $imgHeight); //创建一个彩色的底图
        imagecopyresampled($target_image, $source_image, 0, 0, 0, 0, $imgWidth, $imgHeight, $source_info[0], $source_info[1]);
        if(!imagejpeg($target_image, $source_path)){
            $source_path = '';
        }
        imagedestroy($target_image);
        return $source_path;
    }
复制代码

 

本文作者:潘潘潘的博客

本文链接:https://www.cnblogs.com/seanpan/p/17373694.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   潘潘潘的博客  阅读(137)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.