ImageManager.php 3.82 KB
<?php

namespace common\helpers;

use Yii;
/**
 * 系统的图片资源管理器
 * Class ImageManager
 * @package app\wx\helpers
 */
class ImageManager
{
    /**
     * 图片样式定义(不指定样式则读取原图)
     */
    public static $STYLE_90 = '90x90';
    public static $STYLE_180 = '180x180';
    public static $STYLE_280 = '280x280';
    public static $STYLE_480 = '480x480';

    /**
     * 根据系统模块划分, 定义图片一级目录结构
     */

    public static $MAINTENANCE_PATH                  = 'maintenance/';      // 维修厂用户注册的图片路径
    public static $ORDER_BROKEN_PATH                 = 'broken_order/';    // 订单里面车损坏的图片
    public static $ORDER_FINISH_PATH                 = 'finish_order/';    // 维修好车的时候的图片

    /**
     * @param $path 数据库存储的图片相对路径
     * @param null $style 不传递此参数或者null表示读取原图
     */
    public static function getUrl($path, $style = null)
    {
        $baseUrl = Yii::$app->params['frontEndBaseUrl'];
        $fullPath = $baseUrl.$path;
        if ('min' == $style) {
            $fileArr = explode('.', $path);
            $end = array_pop($fileArr);
            $newPath = implode('.', $fileArr);
            $fullPath = $baseUrl.$newPath.'_min.'.$end;
        }
        return $fullPath;
    }

    /**
     * @param $licensePic
     * @param $fileTypePath
     * @return string
     */
    public static function mvUploadImage($licensePic, $uuid, $fileTypePath = '')
    {
        $dir = Yii::getAlias('@site') . "/tmp";
        $tmpFilePath = $dir.'/'.$licensePic;
        $fileArr = explode('.', $licensePic);

        $minFileName = $fileArr[0].'_min'.'.'.end($fileArr);
        $tmpMinFilePath = $dir.'/'.$minFileName;

        $desPathFile = self::genImagePath($uuid, $fileTypePath);

        $aimPath = Yii::getAlias('@site')."/".$desPathFile .$licensePic;
        FileUtil::moveFile($tmpFilePath, $aimPath,true);

        $aimTPath = Yii::getAlias('@site')."/".$desPathFile .$minFileName;
        FileUtil::moveFile($tmpMinFilePath, $aimTPath,true);

        return [$desPathFile, $licensePic, $minFileName];
    }

    /**
     * @param $uuid
     * @param string $fileTypePath
     * @return string
     */
    public static function genImagePath($uuid, $fileTypePath = '')
    {
        if (empty($fileTypePath)) {
            $catPath = self::$MAINTENANCE_PATH;
        } else {
            $catPath = $fileTypePath;
        }
        $desPathFile = "upload/".$catPath.$uuid.'/';

        return $desPathFile;
    }

    private static function _imageCreateFromPath($imgPath)
    {
        list($width, $height, $type, $attr) = getimagesize($imgPath);
        switch ($type) {
            case 3: // png
                return imagecreatefrompng($imgPath);
            case 2: // jpeg
                return imagecreatefromjpeg($imgPath);
            default:
                return null;
        }
    }

    /**
     * 上传到服务器临时文件区
     * @param $fileInfo $_FILE['file']
     * @return array
     */
    public static function uploadImg($fileInfo)
    {
        $dir = Yii::getAlias('@site') . "/tmp";
        $fileArr = explode('.', $fileInfo['name']);
        $tt = time();
        $filename = 'auto_'.$tt.md5($fileInfo['name']).'.'.end($fileArr);
        $minFileName = 'auto_'.$tt.md5($fileInfo['name']).'_min'.'.'.end($fileArr);
        $saveFilePath = $dir.'/'.$filename;
        move_uploaded_file($fileInfo['tmp_name'], $saveFilePath);
        $tmpUrl = $tmpMinFile = 'tmp/'.$filename;
        $imgSource = self::_imageCreateFromPath($saveFilePath);
        if ($imgSource) {
            ImageUtils::resizeImage($imgSource, 100, 100, $dir.'/'.$minFileName);
            $tmpMinFile = 'tmp/'.$minFileName;
        }

        return [$filename, $minFileName, $tmpUrl, $tmpMinFile];
    }
}