PHP 图片缩放

 /**
     * # 缩放图片大小
     * @param $path
     * @param $max_width
     * @param $max_height
     * @param $img_name
     * @param $img_path
     * @param $img_type
     * @return void
     */
    function ResizeImage($path,$max_width,$max_height,$img_name,$img_path,$img_type)
    {

        # 获取图片信息
        $img = imagecreatefromstring(file_get_contents($path));

        # 获取当前图片信息
        list($pic_width, $pic_height) = getimagesize($path);

        # 获取长倍数
        $widthratio = $max_width/$pic_width;

        # 获取宽倍数
        $heightratio = $max_height/$pic_height;

        # 获取确定的缩放倍数
        $ratio = $widthratio > $heightratio ? $widthratio : $heightratio;

        # 获取新长宽
        $new_width = $pic_width * $ratio;
        $new_height = $pic_height * $ratio;
        if(function_exists("imagecopyresampled")){
            $new_img = imagecreatetruecolor($new_width,$new_height);
            imagecopyresampled($new_img,$img,0,0,0,0,$new_width,$new_height,$pic_width,$pic_height);
        }else{
            $new_img = imagecreate($new_width,$new_height);
            imagecopyresized($new_img,$img,0,0,0,0,$new_width,$new_height,$pic_width,$pic_height);
        }

        # 获取放大后的图片地址
        $path = "{$img_path}/{$img_name}.{$img_type}";

        # 判断图片类型调用不同图片方法
        switch($img_type){
            case 'png':
                imagepng($new_img,$path);
                break;
            case 'jpeg':
                imagejpeg($new_img,$path);
                break;
        }

        # 销毁新图
        imagedestroy($new_img);

    }

  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值