ImageManager.php 4 KB
<?php

namespace common\helpers;

/**
 * 极办公系统的图片资源管理器
 * 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';

    /**
     * 根据系统模块划分, 定义图片一级目录结构
     */
    private static $SMART_ROOT_PATH                = 'smart/';    // 激活库
    private static $CHECK_ROOT_PATH                = 'check/';    // 校验库
    private static $TEMP_ROOT_PATH                 = 'tmp/';     // 临时文件库


    /**
     * 文件名拼接随机串再md5,避免重复
     * @param $length
     * @return null|string
     */
    private static function rand($length)
    {
        $str = null;
        $strPol = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $max = strlen($strPol) - 1;
        for ($i = 0; $i < $length; $i++) {
            $str .= $strPol[mt_rand(0, $max)];
        }

        return $str;
    }

    /**
     * @param $path 数据库存储的图片相对路径
     * @param null $style 不传递此参数或者null表示读取原图, 否则读取指定样式规格的图片
     */
    public static function getUrl($path, $style = null)
    {
        if (empty($path)) return '';
        return ImageServiceHelper::getUrl($path, $style);
    }

    public static function getVoiceUrl($path)
    {
        if (empty($path)) return '';
        return ImageServiceHelper::getUrl($path);
    }

    /**
     * 添加上传图片
     * @param $tmpPath
     * @param $savePath
     */
    public static function add($tmpPath, $savePath)
    {
        return ImageServiceHelper::add($tmpPath, $savePath);
    }

    /**
     * 替换图片
     * @param $oldPath
     * @param $newPath
     * @param $tmpPath
     */
    public static function replace($oldPath, $newPath, $tmpPath)
    {
        return ImageServiceHelper::replace($oldPath, $newPath, $tmpPath);
    }

    /**
     * 移动图片存放位置
     * @param $oldPath
     * @param $newPath
     * @return mixed
     */
    public static function move($oldPath, $newPath)
    {
        ImageServiceHelper::move($oldPath, $newPath);
    }

    /**
     * 删除旧图片
     * @param $path
     */
    public static function delete($path)
    {
        ImageServiceHelper::delete($path);
    }

    /**
     * 获取[临时图片]的存储路径: tmp/20170509/[16位md5加密串].jpg
     * @param $suffix 图片后缀, 如'.jpg'
     * @return string
     */
    public static function getTempImgPath($suffix)
    {
        $imageName = md5(time() . self::rand(5)) . '.' . $suffix;
        $basePath = self::$TEMP_ROOT_PATH . date("Ymd");
        $savePath = $basePath . '/' . $imageName;
        return $savePath;
    }

    /**
     * 商品图片存储路径: smart/20170509/[userid]/[16位md5加密串].jpg
     * @param $userId
     * @param $suffix 图片后缀, 如'.jpg'
     * @param $fileName 图片另外的名称,商品图片存储路径
     * @return string
     */
    public static function getSmartImgPath($userId, $fileName = '', $suffix = 'jpg')
    {
        $imageName = md5(time() . $userId . $fileName . self::rand(5)) . '.' . $suffix;
        $basePath = self::$SMART_ROOT_PATH . date("Ymd") . '/' . $userId;
        $savePath = $basePath . '/' . $imageName;
        return $savePath;
    }

    /**
     * 商品图片存储路径:  IMG_防伪标签uuid_时间戳.jpg
     * @param $userId
     * @param $suffix 图片后缀, 如'.jpg'
     * @param $fileName 图片另外的名称,商品图片存储路径
     * @return string
     */
    public static function getSmartCheckImgPath($userId, $uuid = '', $suffix = 'jpg')
    {
        $imageName = "IMG_" . $uuid . "_" . time() . '.' . $suffix;
        $basePath = self::$CHECK_ROOT_PATH . date("Ymd") . '/' . $userId;
        $savePath = $basePath . '/' . $imageName;
        return $savePath;
    }
}