合并fecyo\services; fecbvc\services; fecalioos\services;项目下的image类,并更改了上传图片的路径带上传年月日

技术问题 · johocn · 于 3年前 发布 · 1697 次阅读
<?php
namespace 命名空间\services;
//名空间需要改动
use fec\helpers\CDir;
use Yii;
use yii\base\InvalidValueException;

/**
 * 合并 fecyo\services;  fecbvc\services;  fecalioos\services;
 * 
 * 
 */
class Image extends \fecshop\services\Image
{


    /**
     * get Image save file path, if floder is not exist, this function will create floder.
     * if image file is exsit , image file name will be change  to a not existed file name( by add radom string to file name ).
     * return image saved relative path , like /a/d/advert.jpg. 默认格式改为/product/年月日/advert.jpg.
     */
     protected function getImgSavedRelativePath($name,$first_str='product')
    {
        list($imgName, $imgType) = explode('.', $name);
        if (!$imgName || !$imgType) {
            throw new InvalidValueException('image file name and type is not correct');
        }
        if (strlen($imgName) < 2) {
            $imgName .= time(). mt_rand(100, 999);
        }
        //$first_str = substr($imgName, 0, 1);
        $two_str = date("Ymd");//substr($imgName, 1, 2);
        $imgSaveFloder = CDir::createFloder($this->GetCurrentBaseImgDir(), [$first_str, $two_str]);
        if ($imgSaveFloder) {
            $imgName = $this->getUniqueImgNameInPath($imgSaveFloder, $imgName, $imgType);
            $relative_floder = '/'.$first_str.'/'.$two_str.'/';

            return $relative_floder.$imgName;
        }

        return false;
    }


    /**
     * @param $imageBuffer 图片文件数据。二进制buffer
     * @param $imageName 图片名字
     * 保存微信小程序的分享二维码,到本地保存
     */
    public function saveWxMicroQrcodeImg($imageBuffer, $imageSaveFileName)
    {
        $saveFilePath = $this->getQrCodeImgAbsoluteFilePath($imageSaveFileName);
        $imageSaveFileName = '/'.$imageSaveFileName;
        if (!file_exists($saveFilePath)) {
            $isMoved = @file_put_contents($saveFilePath, $imageBuffer);
            if (!$isMoved) {
                
                return false;
            } 
        }
        $imgUrl = $this->getUrlByRelativePath($imageSaveFileName);
        $imgPath = $this->getDirByRelativePath($imageSaveFileName);

        return [$imageSaveFileName, $imgUrl, $imgPath];
    }
//fecyo\services;

   
    /**
     * @param $imageBuffer 图片文件数据。二进制buffer
     * @param $imageName 图片名字
     * 保存微信小程序的分享二维码,到本地保存
     */
    public function getCacheWxMicroQrcodeImg($imageSaveFileName)
    {
        $saveFilePath = $this->getQrCodeImgAbsoluteFilePath($imageSaveFileName);
        $imageSaveFileName = '/'.$imageSaveFileName;
        if (file_exists($saveFilePath)) {
            $imgUrl = $this->getUrlByRelativePath($imageSaveFileName);
            $imgPath = $this->getDirByRelativePath($imageSaveFileName);

            return [$imageSaveFileName, $imgUrl, $imgPath];
        }
        
    }
    
    /**
     * @param $imgName | string ,图片名称
     * 得到保存二维码图片的绝对路径。
     */
    public function getQrCodeImgAbsoluteFilePath($imgName)
    {
        $imgName = '/'.$imgName;
        $saveFilePath = $this->GetCurrentBaseImgDir() . $imgName;
        
        return $saveFilePath;
    }
    
    /**
     * @param $imgName | string ,图片名称
     * 得到保存二维码图片的url。
     */
    public function getQrCodeImgUrl($imgName)
    {
        $imgName = '/'.$imgName;
        $imgUrl = $this->getUrlByRelativePath($imgName);
        
        return $imgUrl;
    }

//fecbvc\services; 

 
    /**
     * @param $fileType | string,  图片的类型,譬如 png, jpg
     * 当pdf文件生成图片的时候,通过该函数,得到图片的保存路径
     */
    public function generatePdfImgPath($fileType)
    {
        Yii::$service->image->imageFloder = Yii::$service->product->image->imageFloder;
        $name = 'xxx.'.$fileType;
        $fileName = $this->generateImgName($name, 18);
        
        $imgSavedRelativePath = $this->getImgSavedRelativePath($fileName);
        
        $imgSavedAbsolutePath = $this->GetCurrentBaseImgDir().$imgSavedRelativePath;
        
        return [$imgSavedRelativePath, $imgSavedAbsolutePath];
    }
//fecalioos\services;


    /**
     * @param $param_img_file | Array .
     * 上传产品图片,
     * 如果成功,保存产品相对路径,譬如: '/b/i/big.jpg'
     * 如果失败,reutrn false;
     */
    public function saveUploadImg($FILE)
    {
        $size = $FILE['size'];
        $file = $FILE['tmp_name'];
        $name = $FILE['name'];
        $newName = $this->generateImgName($name);
        if (!$newName) {
            throw new InvalidValueException('generate img name fail');
        }
        
        if ($size > $this->getMaxUploadSize()) {
            throw new InvalidValueException('upload image is to max than'. $this->getMaxUploadSize().' MB');
        } elseif (!($img = getimagesize($file))) {
            throw new InvalidValueException('file type is empty.');
        } elseif ($img = getimagesize($file)) {
            $imgType = $img['mime'];
            if (!$this->isAllowImgType($file, $name)) {
                throw new InvalidValueException('image type is not allow for '.$imgType);
            }
        }
        // process image name.
        $imgSavedRelativePath = $this->getImgSavedRelativePath($newName);
        
        $aliossResult = Yii::$service->alioss->uploadFile(trim($imgSavedRelativePath, '/'), $file);
        if (isset($aliossResult['info']['url']) && $aliossResult['info']['url']) {
            
            return [$imgSavedRelativePath, $aliossResult['info']['url'], ''];
        }
        
        return false;

    }
    
}

共收到 7 条回复
Fecmall#13年前 0 个赞

多谢分享。

ffee#23年前 0 个赞

这个文件放那里吗?

johocn#33年前 0 个赞

namespace 命名空间\services; //名空间需要改动 放在优先运行的命名空间下,如walletyo\services;

Fecmall#43年前 0 个赞

后面看看找时间直接默认集成到fecyo中

linqqq5311#53年前 0 个赞

碰到一样的问题

Fecmall#63年前 0 个赞

@linqqq5311 #5楼 两个文件都对Image Services进行了重写,重写文件(Image Services)冲突了,需要合并一下文件

7楼 已删除.
8楼 已删除.
zzkclo3#93年前 0 个赞

遇到同样的问题,多谢大佬

添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics