php 缩略图 封装一个方法

来源:互联网 发布:淘宝客手机可以开通吗 编辑:程序博客网 时间:2024/06/05 14:46

在工作中我们可能要用到一些缩略图,这里我封装了一个方法,希望对大家有用

废话不多说了

<?phpsuolue('big.jpg',800,800);suolue('big.jpg',500,500);suolue('big.jpg',300,300);suolue('big.jpg',100,100);suolue('big.jpg',50,50);function suolue($resuce,$width,$height,$type = 'png',$isfileName=true,$path = "pl"){    //打开图片    $imageRes = open($resuce);    //获取图片信息    $imageInfo = getImageInfo($resuce);    //进行缩略    $newSize = getNewSize($width,$height,$imageInfo);    //解决gif黑色    $newRes = kidOfImage($imageRes,$newSize,$imageInfo);    //解决文件随机    if($isfileName){        $name = uniqid().'.'.$type;    }else{        $info = pathinfo($path);        $name = $info['filename'].'.'.$type;    }    $path = rtrim($path,'/').'/'.$name;    //输出    $func = 'image'.$type;    if(!function_exists($func)){        return false;    }    $func($newRes,$path);    //销毁    imagedestroy($newRes);    return $path;}function getImageInfo($path){    $info = getimagesize($path);    $data['width'] = $info[0];    $data['height'] = $info[1];    return $data;}function open($path){    //判断文件是否存在    if(!file_exists($path)){        return '不存在';    }    $info = getimagesize($path);    switch($info['mime']){        case 'image/jpg':        case 'image/jpeg':        case 'image/jpe':        case 'image/pjpeg':            $res = imagecreatefromjpeg($path);        break;        case 'image/png':            $res = imagecreatefrompng($path);            break;        case 'image/gif':            $res = imagecreatefromgif($path);            break;    }    return $res;}//等比缩放function getNewSize($width, $height,$imgInfo){        //将原图片的宽度给数组中的$size["width"]    $size["width"]=$imgInfo["width"];     //将原图片的高度给数组中的$size["height"]          $size["height"]=$imgInfo["height"];            if($width < $imgInfo["width"]){        //缩放的宽度如果比原图小才重新设置宽度        $size["width"]=$width;                 }    if($height < $imgInfo["height"]){        //缩放的高度如果比原图小才重新设置高度        $size["height"]=$height;                }    if($imgInfo["width"]*$size["width"] > $imgInfo["height"] * $size["height"]){        $size["height"]=round($imgInfo["height"]*$size["width"]/$imgInfo["width"]);    }else{        $size["width"]=round($imgInfo["width"]*$size["height"]/$imgInfo["height"]);    }    return $size;}//这两个方法不用会,直接复制就行//处理gif变黑function kidOfImage($srcImg,$size, $imgInfo){    //传入新的尺寸,创建一个指定尺寸的图片    $newImg = imagecreatetruecolor($size["width"], $size["height"]);         //定义透明色    $otsc = imagecolortransparent($srcImg);    if( $otsc >= 0 && $otsc < imagecolorstotal($srcImg)) {         //取得透明色         $transparentcolor = imagecolorsforindex( $srcImg, $otsc );             //创建透明色             $newtransparentcolor = imagecolorallocate(             $newImg,             $transparentcolor['red'],                 $transparentcolor['green'],             $transparentcolor['blue']         );        //背景填充透明         imagefill( $newImg, 0, 0, $newtransparentcolor );         imagecolortransparent( $newImg, $newtransparentcolor );    }    imagecopyresized( $newImg, $srcImg, 0, 0, 0, 0, $size["width"], $size["height"], $imgInfo["width"], $imgInfo["height"] );    imagedestroy($srcImg);    //最终新资就解决了透明色的题     return $newImg;}