• PHP学习笔记:等比例缩放图片


    直接上代码,imgzip($src,$newwid,$newhei)这个函数带进去的分别是原图片、缩放要求的宽度、缩放的长度。代码都备注了,不懂可以留言哈哈

    <?php 
    
    //压缩图片  缩略图
    
    $src= "xiezheng.jpg";
    $newwid=640;
    $newhei= 480;
    function imgzip($src,$newwid,$newhei){
        $imgInfo = getimagesize($src);
    
        $imgType = image_type_to_extension($imgInfo[2], false);
    
        $fun = "imagecreatefrom{$imgType}";
    
    //声明图片   打开图片 在内存中
        $image = $fun($src);
    //方便配置长度宽度、高度,设置框为变量wid,高度为hei
        $wid=$imgInfo[0];
        $hei=$imgInfo[1];
    //判断长度和宽度,以方便等比缩放,规格按照500, 320
        if($wid>$hei){
            $wid=$newwid;
            $hei=$newwid/($wid/$hei);
        }else {
            $wid = $newhei * ($wid / $hei);
            $hei = $newhei;
        }
    
    //在内存中建立一张图片
        $images2 = imagecreatetruecolor($newwid, $newhei); //建立一个500*320的图片
    
    //将原图复制到新建图片中
    //imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
    
        imagecopyresampled($images2, $image, 0, 0, 0, 0, $wid,$hei, $imgInfo[0],$imgInfo[1]);
    
    //销毁原始图片
        imagedestroy($image);
    
    
    //直接输出图片文件
    
        header("Content-type: ".$imgInfo['mime']);
        imagejpeg($images2);
    
    //保存图片 到新文件
        imagejpeg($images2, 'new.jpg', 100); //10代码输出图片的质量 0-100 100质量最高
    
    //销毁
        imagedestroy($images2);
    }
    imgzip($src,$newwid,$newhei);
    
    
    ?>

     效果

    压缩前

    压缩后

  • 相关阅读:
    html5+css3中的background: -moz-linear-gradient 用法 (转载)
    CentOS 安装Apache服务
    Linux 笔记
    CURL 笔记
    Spring Application Context文件没有提示功能解决方法
    LeetCode 389. Find the Difference
    LeetCode 104. Maximum Depth of Binary Tree
    LeetCode 520. Detect Capital
    LeetCode 448. Find All Numbers Disappeared in an Array
    LeetCode 136. Single Number
  • 原文地址:https://www.cnblogs.com/jianqingwang/p/5881327.html
Copyright © 2020-2023  润新知