Image.php 7.31 KB
Newer Older
庄钊鑫 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
<?php
namespace app\common\tools;

/**
 * ------------------------------------------
 * 图片处理类 (GD2库)
 * ------------------------------------------
 *
 * 根据实际需求设计的图片处理类, “居中剪裁”通常使用在用户头像、缩略图等场景, “等比缩放”通常用来压缩图片
 *
 * 1. 居中剪裁 (宽高自动)
 * 2. 等比缩放 (宽高自动, 源图小于宽高的不做缩放)
 * 3. 创建缩略图
 * 4. 如果操作网络图片, 会在根目录生成"tmp.jpg" (用于测试)
 *
 */
class Image
{
    protected $sourceImage = ''; // 源文件路径
    protected $width       = ''; // 要设置的宽度
    protected $height      = ''; // 要设置的高度
    protected $createThumb = false; // 是否创建缩略图
    protected $thumbMarker = '_thumb'; // 缩略图后缀

    /**
     * [__construct description] 
     * @param    array                    $props [description]
     */
    public function __construct($props = array())
    {
        if (count($props) > 0) {
            $this->initialize($props);
        }
    }

    /**
     * 初始化配置 
     * @param    [type]                   $props [description]
     * @return   [type]                          [description]
     */
    public function initialize($props)
    {
        $this->clear(); // 清除之前的配置
        $this->sourceImage = !isset($props['sourceImage']) ? $this->sourceImage : $props['sourceImage'];
        $this->width       = !isset($props['width']) ? $this->width : $props['width'];
        $this->height      = !isset($props['height']) ? $this->height : $props['height'];
        $this->createThumb = !isset($props['createThumb']) ? $this->createThumb : $props['createThumb'];
        $this->thumbMarker = !isset($props['thumbMarker']) ? $this->thumbMarker : $props['thumbMarker'];
    }

    /**
     * 清除配置 
     * @return   [type]                   [description]
     */
    protected function clear()
    {
        $this->sourceImage = '';
        $this->width       = '';
        $this->height      = '';
        $this->createThumb = false;
        $this->thumbMarker = '_thumb';
    }

    /**
     * 等比缩放 
     * @param    string                   $value [description]
     * @return   [type]                          [description]
     */
    public function resize($value = '')
    {
        $sourcePath   = $this->sourceImage;
        $targetWidth  = $this->width;
        $targetHeight = $this->height;
        $sourceInfo   = getimagesize($sourcePath);
        $sourceWidth  = $sourceInfo[0];
        $sourceHeight = $sourceInfo[1];
        $sourceMime   = $sourceInfo['mime'];
        switch ($sourceMime) {
            case 'image/gif':
                $sourceImage = imagecreatefromgif($sourcePath);
                break;
            case 'image/jpeg':
                $sourceImage = imagecreatefromjpeg($sourcePath);
                break;
            case 'image/png':
                $sourceImage = imagecreatefrompng($sourcePath);
                break;
            default:
                $sourceImage = imagecreatefromjpeg($sourcePath); // 兼容app, 许多app上传的图片无mime信息
                break;
        }

        $widthRatio  = $targetWidth / $sourceWidth;
        $heightRatio = $targetHeight / $sourceHeight;
        // 源图宽高均小于要设置的值
        if ($widthRatio >= 1 && $heightRatio >= 1) {
            $targetImage = $sourceImage;
        } else {
            // 根据缩放倍率小的宽或者高缩放
            if ($widthRatio < $heightRatio) {
                $zoom_width  = $targetWidth;
                $zoom_height = $sourceHeight * ($targetWidth / $sourceWidth);
            } else {
                $zoom_height = $targetHeight;
                $zoom_width  = $sourceWidth * ($targetHeight / $sourceHeight);
            }
            // 声明图片资源
            $targetImage = imagecreatetruecolor($zoom_width, $zoom_height);
            // 缩放
            imagecopyresampled($targetImage, $sourceImage, 0, 0, 0, 0, $zoom_width, $zoom_height, $sourceWidth, $sourceHeight);
        }
        // 图片地址为url
        if (strpos($sourcePath, 'http') !== false) {
            imagejpeg($targetImage, $_SERVER['DOCUMENT_ROOT'] . '/tmp.jpg');
        } else {
            if ($this->createThumb) {
                $sourcePath = str_replace('.', $this->thumbMarker . '.', $sourcePath);
            }
            imagejpeg($targetImage, $sourcePath);
        }

        //销毁资源
        imagedestroy($sourceImage);
        @imagedestroy($targetImage);
        $this->clear();
    }

    /**
     * 居中剪裁 
     * @return   [type]                   [description]
     */
    public function crop()
    {
        $sourcePath   = $this->sourceImage;
        $targetWidth  = $this->width;
        $targetHeight = $this->height;
        $sourceInfo   = getimagesize($sourcePath);
        $sourceWidth  = $sourceInfo[0];
        $sourceHeight = $sourceInfo[1];
        $sourceMime   = $sourceInfo['mime'];
        $sourceRatio  = $sourceHeight / $sourceWidth;
        $targetRatio  = $targetHeight / $targetWidth;
        if ($sourceRatio > $targetRatio) {
            // 源图过高
            $croppedWidth  = $sourceWidth;
            $croppedHeight = $sourceWidth * $targetRatio;
            $source_x      = 0;
            $source_y      = ($sourceHeight - $croppedHeight) / 2;
        } elseif ($sourceRatio < $targetRatio) {
            // 源图过宽
            $croppedWidth  = $sourceHeight / $targetRatio;
            $croppedHeight = $sourceHeight;
            $source_x      = ($sourceWidth - $croppedWidth) / 2;
            $source_y      = 0;
        } else {
            // 源图适中
            $croppedWidth  = $sourceWidth;
            $croppedHeight = $sourceHeight;
            $source_x      = 0;
            $source_y      = 0;
        }
        switch ($sourceMime) {
            case 'image/gif':
                $sourceImage = imagecreatefromgif($sourcePath);
                break;
            case 'image/jpeg':
                $sourceImage = imagecreatefromjpeg($sourcePath);
                break;
            case 'image/png':
                $sourceImage = imagecreatefrompng($sourcePath);
                break;
            default:
                $sourceImage = imagecreatefromjpeg($sourcePath); // 兼容app, 许多app上传的图片无mime信息
                break;
        }
        // 声明图片资源
        $targetImage   = imagecreatetruecolor($targetWidth, $targetHeight);
        $cropped_image = imagecreatetruecolor($croppedWidth, $croppedHeight);
        // 裁剪
        imagecopy($cropped_image, $sourceImage, 0, 0, $source_x, $source_y, $croppedWidth, $croppedHeight);
        // 缩放
        imagecopyresampled($targetImage, $cropped_image, 0, 0, 0, 0, $targetWidth, $targetHeight, $croppedWidth, $croppedHeight);
        // 图片地址为url
        if (strpos($sourcePath, 'http') !== false) {
            imagejpeg($targetImage, $_SERVER['DOCUMENT_ROOT'] . '/tmp.jpg');
        } else {
            if ($this->createThumb) {
                $sourcePath = str_replace('.', $this->thumbMarker . '.', $sourcePath);
            }
            imagejpeg($targetImage, $sourcePath);
        }
        // 销毁资源
        imagedestroy($sourceImage);
        imagedestroy($targetImage);
        imagedestroy($cropped_image);
        $this->clear();
    }
}