<?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;
}
}