php 缩略图 封装一个方法

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

废话不多说了

<?php
suolue('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;
}       
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值