码迷,mamicode.com
首页 > Web开发 > 详细

【PHP】图片操作类

时间:2014-07-16 19:55:41      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   width   io   cti   

刚学php不久,在做一个项目一步步的积累和学习,希望分享的东西能其他人带来帮助,少走弯路。

<?php
/**
 *    图片操作类
 * @author Harlan Song
 */

/**
 * 压缩图片,以宽度为基准,高度等比例压缩。
 * @param string $srcPath 源图片路径
 * @param string $newPath 压缩后图片路径
 *  @param string $newWidth 最大宽度
 */
function commpressImage($srcPath,$newPath,$newWidth){
    $info = getimagesize($srcPath);
    $width = $info[0];
    $height = $info[1];
    $mime = $info[‘mime‘];//得到图片类型
    if($width > $newWidth){
        $newHeight = $height / ($width / $newWidth);
        $newImg = imagecreatetruecolor($newWidth,$newHeight);
        switch($mime){
            case "image/jpeg":
                $img = @imagecreatefromjpeg($srcPath);
                imagecopyresampled($newImg,$img,0,0,0,0,$newWidth,$newHeight,$width,$height);
                imagejpeg($newImg,$newPath);
                break;
            case "image/png":
                $img = @imagecreatefrompng($srcPath);
                imagecopyresampled($newImg,$img,0,0,0,0,$width,$height,$newWidth,$newHeight);
                imagepng($newImg,$newPath);
                break;
            case "image/gif":
                $img = @imagecreatefromgif($srcPath);
                imagecopyresampled($newImg,$img,0,0,0,0,$width,$height,$newWidth,$newHeight);
                imagegif($newImg,$newPath);
                break;
            case "image/wbmp":
                $img = @imagecreatefromwbmp($srcPath);
                imagecopyresampled($newImg,$img,0,0,0,0,$width,$height,$newWidth,$newHeight);
                imagewbmp($newImg,$newPath);
                break;
        }
    }else{
        //源图片宽度比指定值小,不压缩直接复制。
        copy($srcPath,$newPath);
    }
}

?>

 

【PHP】图片操作类,布布扣,bubuko.com

【PHP】图片操作类

标签:style   blog   color   width   io   cti   

原文地址:http://www.cnblogs.com/markdev/p/3837243.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!