ImageUtils.php 5.25 KB
<?php

namespace common\helpers;

/**
 * Class ImageUrl
 * @package app\wx\helpers
 */
class ImageUtils
{
    /**
     * 图像等比缩放处理函数
     * @param $im
     * @param $scaleWidth
     * @param $scaleHeight
     * @param $name
     * @param $filetype
     */
    public static function resizeImage($im, $scaleWidth, $scaleHeight, $savePath = '')
    {
        if (!$im) {
            return null;
        }

        $pic_width = imagesx($im);
        $pic_height = imagesy($im);

        // 尺寸不合法, 直接返回原图
        if ($scaleWidth <= 0 || $scaleHeight <= 0) {
            return $im;
        }

        // 计算缩放比例
        $widthRatio = $scaleWidth / $pic_width;
        $heightRatio = $scaleHeight / $pic_height;
        $scaleRatio = ($widthRatio < $heightRatio) ? $widthRatio : $heightRatio;

        // 计算新图像宽高
        $newWidth = $pic_width * $scaleRatio;
        $newHeight = $pic_height * $scaleRatio;

        // 缩放处理
        if(function_exists("imagecopyresampled")) {
            $newim = imagecreatetruecolor($newWidth, $newHeight);
            imagecopyresampled($newim, $im, 0, 0, 0, 0,$newWidth, $newHeight, $pic_width, $pic_height);
        } else {
            $newim = imagecreate($newWidth, $newHeight);
            imagecopyresized($newim, $im, 0, 0, 0, 0, $newWidth, $newHeight, $pic_width, $pic_height);
        }

        // 如果传入了保存路径, 则将缩放后的图片写入保存
        if (!empty($savePath)) {
            imagejpeg($newim, $savePath);
            imagedestroy($newim);
        }
        else { // 不写入文件直接使用, 记得使用完释放掉
            return $newim;
        }
    }

    /**
     * 按照指定的尺寸压缩图片
     * @param $source_path  原图路径
     * @param $target_path  保存路径
     * @param $imgWidth     目标宽度
     * @param $imgHeight    目标高度
     * @return bool|string
     */
    function resize_image($im, $scaleWidth, $scaleHeight, $savePath = '')
    {
        if (!$im) {
            return null;
        }

        $pic_width = imagesx($im);
        $pic_height = imagesy($im);

        // 尺寸不合法, 直接返回原图
        if ($scaleWidth <= 0 || $scaleHeight <= 0) {
            return $im;
        }

        // 计算新图像宽高
        $newWidth = $scaleWidth;
        $newHeight = $scaleHeight;

        // 缩放处理
        if(function_exists("imagecopyresampled")) {
            $newim = imagecreatetruecolor($newWidth, $newHeight);
            imagecopyresampled($newim, $im, 0, 0, 0, 0,$newWidth, $newHeight, $pic_width, $pic_height);
        } else {
            $newim = imagecreate($newWidth, $newHeight);
            imagecopyresized($newim, $im, 0, 0, 0, 0, $newWidth, $newHeight, $pic_width, $pic_height);
        }

        // 如果传入了保存路径, 则将缩放后的图片写入保存
        if (!empty($savePath)) {
            imagejpeg($newim, $savePath);
            imagedestroy($newim);
        }
        else { // 不写入文件直接使用, 记得使用完释放掉
            return $newim;
        }

    }

    /** 方法参数为图片名
     * @param $str_file
     * @param int $max
     * @return bool
     */
    static function changeSize($str_file, $max = 600)
    {
        $size = getimagesize($str_file);
        //这个方法会返回一个数组,第一个元素 (索引值 0) 是图片的高度,单位是像素 (pixel)。第二个元素 (索引值 1) 是图片的宽度。第三个元素 (索引值 2) 是图片的文件格式,其值 1 为 GIF 格式、 2 为 JPEG/JPG 格式、3 为 PNG 格式。第四个元素 (索引值 3) 为图片的高与宽字符串,height=xxx width=yyy。

        //因为PHP只能对资源进行操作,所以要对需要进行缩放的图片进行拷贝,创建为新的资源
        //根据图片的格式使用不同的方法
        if($size[2]==1)
            $src=imagecreatefromgif($str_file);
        if($size[2]==2)
            $src=imagecreatefromjpeg($str_file);
        if($size[2]==3)
            $src=imagecreatefrompng($str_file);

        //取得源图片的宽度和高度
        $w = $size['0'];
        $h = $size['1'];

        //根据最大值为300,算出另一个边的长度,得到缩放后的图片宽度和高度
        if ($w > $h) {
            $w = $max;
            $h = $h * ($max / $size['0']);
        } else {
            $h = $max;
            $w = $w * ($max / $size['1']);
        }

        //声明一个$w宽,$h高的真彩图片资源,此时只是一个有宽高的黑白图片。
        $image = imagecreatetruecolor($w, $h);

        //关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)返回值为bool
        imagecopyresampled($image, $src, 0, 0, 0, 0, $w, $h, $size['0'], $size['1']);

        //将图片塞进去,也是根据图片不同的格式选用不同的方法,返回值为bool。
        if($size[2]==1)
            $result = imagegif($image,$str_file);
        if($size[2]==2)
            $result = imagejpeg($image,$str_file);
        if($size[2]==3)
            $result = imagepng($image,$str_file);

        //销毁资源
        imagedestroy($image);

        return $result ;
    }

}