Skip to content

Instantly share code, notes, and snippets.

@crossmaya
Created June 8, 2019 16:06
Show Gist options
  • Save crossmaya/01bfe083c7a3f480801c1278db015844 to your computer and use it in GitHub Desktop.
Save crossmaya/01bfe083c7a3f480801c1278db015844 to your computer and use it in GitHub Desktop.
laravel image uploader
<?php
use Illuminate\Support\Facades\Validator;
class ImageHelper
{
// 自定义验证函数
protected $validator;
// 上传图片字段
protected $field = 'image';
// 根路径
protected $basePath = 'app'.DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR;
// 上传文件路径
protected $uploadPath;
// 图片上传目标路径。
protected $destination;
// 图片上传信息
protected $uploadedFileInfo;
// 图片验证规则.
protected $validationRules;
// 图片验证错误信息
protected $validationErrors;
public function __construct($rules=[])
{
$this->setRules($rules);
}
public function upload($file)
{
if (!$this->validate($file) )
{
return false;
}
$originalFileName = $file->getClientOriginalName();
$encryptedFileName = $this->getUniqueFilename($originalFileName);
$mimeType = $file->getMimeType();
$size = $file->getSize();
if ($file->move($this->getDestination(), $encryptedFileName)) {
$this->uploadedFileInfo = [
'orignal_image_name' => $originalFileName,
'file' => $encryptedFileName,
'upload_path' => $this->getUploadPath(),
'size' => $size,
'extension' => $file->getClientOriginalExtension(),
'mime_type' => $mimeType,
];
return true;
}
return false;
}
protected function validate($file)
{
if ( !$file->isValid() )
{
return false;
}
if ( $this->validator )
{
return call_user_func_array($this->validator,[$this, $file]);
} else
{
// 失败了 把错误信息存储到errors属性中。
$validator = Validator::make(['image'=>$file], $this->validationRules);
if ($validator->fails())
{
$this->validationErrors = '';
return false;
}
}
return true;
}
public function setUploadFolder($folder, $public=false)
{
$this->uploadPath = $this->basePath . $folder . DIRECTORY_SEPARATOR;
$this->destination = $public ? public_path($this->uploadPath) : storage_path($this->uploadPath);
}
// 清空目录下所有文件。
public function clean($folder)
{
if ( !is_dir($folder) )
{
throw new \Exception($folder. "is not dir");
}
array_map('unlink', glob($folder. DIRECTORY_SEPARATOR . '*'));
}
public function getUploadedFileInfo()
{
return $this->uploadedFileInfo;
}
public function getUniqueFilename($filename)
{
$uniqueName = uniqid();
$fileExt = explode('.', $filename);
$mimeType = end($fileExt);
$filename = $uniqueName . '.' . $mimeType;
return $filename;
}
public function setValidator($validator)
{
$this->validator = $validator;
return $this;
}
public function getField()
{
return $this->field;
}
public function setField($field)
{
$this->field = $field;
return $this;
}
public function setRules($rules)
{
$this->validationRules = $rules;
return $this;
}
public function getRules()
{
return $this->rules;
}
public function getErrors()
{
//
}
public function setErrors($errors)
{
//
}
public function setBasePath($path)
{
$this->basePath = $path;
return $this;
}
public function getBasePath()
{
return $this->basePath;
}
public function getUploadPath()
{
return $this->uploadPath;
}
public function getDestination()
{
return $this->destination;
}
public function getUniqueFolderName()
{
return sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 65535),
mt_rand(0, 65535),
mt_rand(0, 65535),
mt_rand(0, 4095) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 65535),
mt_rand(0, 65535),
mt_rand(0, 65535)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment