PHP批量等比例缩放图片大小

京漂大叔 2020-12-16 PM 3061℃ 0条

QQ截图20201216144955.png
如图,将图中目录下面的图片批量等比例缩放

第一个方法,遍历目录下文件夹里面的图片。

function read_all ($dir,$dst_path=''){
    if(!is_dir($dir)) return false;

    $handle = opendir($dir);
    //$dst_path = '';
    if($handle){
        while(($fl = readdir($handle)) !== false){
            $temp = $dir.DIRECTORY_SEPARATOR.$fl;
           
            //如果不加  $fl!='.' && $fl != '..'  则会造成把$dir的父级目录也读取出来
            if(is_dir($temp) && $fl!='.' && $fl != '..'){
                $dstdir = $temp;
                $dst_path = substr($dstdir,0,-1);
                read_all($temp,$dst_path);
            }else{
                $dst_path = 'hong.jpg';
                if($fl!='.' && $fl != '..'){
                    compressedImage($temp,$dst_path);
                    
                }
            }
        }
    }
}

第二个方法,等比例批量缩放目录下面的图片,并存放到指定目录

//图片等比压缩
/**
   * desription 压缩图片
   * @param sting $imgsrc 图片路径
   * @param string $imgdst 压缩后保存路径
   * @param int $type 压缩后的图片保存格式
   * @param string $yswidth 要压缩的最大宽
   */
function compressedImage($imgsrc, $imgdst, $yswidth='240') {
    if(!is_dir($imgdst)){
        mkdir($imgdst,0777,true);
    }
    $hz = substr(strrchr($imgsrc, '.'), 1);
    //生成新图片名
    $imgdst = $imgdst.'/'.date("YmdHis").rand(1000,9999).".".$hz;
    //echo $imgdst;die;
    list($width, $height, $type) = getimagesize($imgsrc);
    $new_width = $width;//压缩后的图片宽
    $new_height = $height;//压缩后的图片高
    if($width >= $yswidth){
      $per = $yswidth / $width;//计算比例
      $new_width = $width * $per;
      $new_height = $height * $per;
    }

    switch ($type) {
      case 1:
        $giftype = check_gifcartoon($imgsrc);
        if ($giftype) {
          header('Content-Type:image/gif');
          $image_wp = imagecreatetruecolor($new_width, $new_height);
          $image = imagecreatefromgif($imgsrc);
          imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
          //90代表的是质量、压缩图片容量大小
          imagejpeg($image_wp, $imgdst, 90);
          imagedestroy($image_wp);
          imagedestroy($image);
        }
        break;
      case 2:
        header('Content-Type:image/jpeg');
        $image_wp = imagecreatetruecolor($new_width, $new_height);
        $image = imagecreatefromjpeg($imgsrc);
        imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
        //90代表的是质量、压缩图片容量大小
        imagejpeg($image_wp, $imgdst, 90);
        imagedestroy($image_wp);
        imagedestroy($image);
        break;
      case 3:
        //header('Content-Type:image/png');
        $image_wp = imagecreatetruecolor($new_width, $new_height);
        $image = imagecreatefrompng($imgsrc);
        //分配颜色 + alpha,将颜色填充到新图上
        $alpha = imagecolorallocatealpha($image_wp, 0, 0, 0, 127);
        imagefill($image_wp, 0, 0, $alpha);
        
        imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
        imagesavealpha($image_wp, true);
       
        imagepng($image_wp, $imgdst);
        imagedestroy($image_wp);
        imagedestroy($image);
        break;
    }
}

直接执行 read_all('E:png'); 途中的那些 ABC目录存放再E:png目录下,这样执行即可把E:png目录下文件夹里面的图片批量进行缩放

图片合并的方法,将read_all方法里面的opentow($dst_path,$temp);替换即可。

function opentow($dst_path,$src_path){
    /*if(!is_dir($imgdst)){
        mkdir($imgdst,0777,true);
    }*/
    $sp = explode('.',$dst_path);
    //print_r($sp);die;
    $p = explode("\\",$src_path);
    $newp = 'png';
    $Absolute_Path = $p[0].'\\'.$p[1].'\\'.$newp.'\\'.$p[3].'\\'.$sp[0].'\\';
    if(!is_dir($Absolute_Path)){
        mkdir($Absolute_Path,0777,true);
    }
    //echo  $Absolute_Path;die;
    //$Absolute_Path=dirname(__FILE__);
    //echo   $hz;die;
    //本地的绝对路径
    //$dst_path = '1.png';//背景图  
    //$src_path= '2.png'; //头像
    $hz = substr(strrchr($dst_path, '.'), 1);
    //$path = $Absolute_Path.'/data/';
    //生成新图片名
    $image = $Absolute_Path.date("YmdHis").rand(1000,9999).".".$hz;
    //echo $image;die;
    //创建图片的实例
    $dst = imagecreatefromstring(file_get_contents($dst_path));
    $src = imagecreatefromstring(file_get_contents($src_path));
    //获取水印图片的宽高
    $src_w =139;$src_h=58;
    list($src_w,$src_h) = getimagesize($src_path);
    //如果水印图片本身带透明色,则使用imagecopy方法
    imagecopy($dst, $src, 380,260, 0, 0, $src_w, $src_h);
    //输出图片
    list($src_w, $src_h, $dst_type) = getimagesize($dst_path);
    switch ($dst_type) {
        case 1://GIF
            imagegif($dst, $image);
            break;
        case 2://JPG
            imagejpeg($dst, $image);
            break;
        case 3://PNG
//              header('Content-Type: image/png');
            imagepng($dst, $image);
            break;
        default:
            break;
    }
    return $image;
}
标签: PHP, 等比例缩放

非特殊说明,文章均为原创。

评论啦~