diff --git a/.gitignore b/.gitignore index 9d4b362..9bf9c4c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,8 @@ +.idea .DS_Store phpunit.phar /vendor composer.phar composer.lock *.project -.idea/ \ No newline at end of file +.idea/ diff --git a/README.md b/README.md index 1ed6b59..f0a20cc 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,22 @@ $form->cropper('content','label'); ```php $form->cropper('content','label')->cRatio($width,$height); ``` + +自定义文件名称(使用 basename 方法,原 name 方法在此插件无效) +```php +$form->cropper('content','label') + ->basename(function () { + return time() . '_' . str_random(10); + }); +``` + +> 使用 basename 方法请返回一个不包含拓展名的自定义文件名称 + +自定义存储路径 +```php +$form->cropper('content','label')->move('images/users/avatars'); +``` + ## PS (特性预读) 1、图片并不是预上传的,而是前端转base64之后填入input,服务端再转回图片保存的 diff --git a/src/Crop.php b/src/Crop.php index 9b4819a..4d9e7c2 100644 --- a/src/Crop.php +++ b/src/Crop.php @@ -5,12 +5,15 @@ use Encore\Admin\Form\Field\ImageField; use Encore\Admin\Form\Field\File; use Encore\Admin\Admin; +use Illuminate\Support\Facades\Storage; class Crop extends File { //use Field\UploadField; use ImageField; + protected $basename = null; + private $ratioW = 100; private $ratioH = 100; @@ -32,52 +35,72 @@ protected function preview() } /** - * [将Base64图片转换为本地图片并保存] - * @E-mial wuliqiang_aa@163.com - * @TIME 2017-04-07 - * @WEB http://blog.iinu.com.cn - * @param [Base64] $base64_image_content [要保存的Base64] - * @param [目录] $path [要保存的路径] + * @author Mike + * @param $base64ImageContent + * @return bool */ - private function base64_image_content($base64_image_content, $path) + private function storeBase64Image($base64ImageContent) { //匹配出图片的格式 - if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) { - $type = $result[2]; - $new_file = $path . "/" . date('Ymd', time()) . "/"; - if (!file_exists($new_file)) { - //检查是否有该文件夹,如果没有就创建,并给予最高权限 - mkdir($new_file, 0755, true); - } - $new_file = $new_file . md5(microtime()) . ".{$type}"; - if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))) { - return $new_file; - } else { - return false; - } - } else { + if (! preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64ImageContent, $result)) { return false; } + + $extension = $result[2]; + $directory = ltrim($this->getDirectory(), '/'); + $file_name = $this->getStoreBasename() . '.' . $extension; + $file_content = base64_decode(str_replace($result[1], '', $base64ImageContent)); + $file_path = $directory . '/' . $file_name; + + Storage::disk(config('admin.upload.disk'))->put($file_path , $file_content); + + return $file_path ; + } + + /** + * @author Mike + * @param $basename + * @return $this + */ + public function basename($basename) + { + if ($basename) { + $this->basename = $basename; + } + + return $this; + } + + /** + * @author Mike + * @return mixed|null|string + */ + protected function getStoreBasename() + { + if ($this->basename instanceof \Closure) { + return $this->basename->call($this); + } + + if (is_string($this->basename)) { + return $this->basename; + } + + return md5(uniqid()); } + /** + * @author Mike + * @param array|\Symfony\Component\HttpFoundation\File\UploadedFile $base64 + * @return array|bool|mixed|string|\Symfony\Component\HttpFoundation\File\UploadedFile + */ public function prepare($base64) { //检查是否是base64编码 if (preg_match('/data:image\/.*?;base64/is',$base64)) { - //base64转图片 返回的是绝对路径 - $imagePath = $this->base64_image_content($base64,public_path('uploads/base64img')); - if ($imagePath !== false) { - //删除旧图片 - @unlink(public_path('uploads/').$this->original); - //处理图片地址 - preg_match('/base64img\/.*/is',$imagePath,$matches); - - $this->callInterventionMethods($imagePath); - - return $matches[0]; - } else { - return 'lost'; - } + $imagePath = $this->storeBase64Image($base64); + $this->destroy(); + $this->callInterventionMethods($imagePath); + return $imagePath; } else { preg_match('/base64img\/.*/is',$base64,$matches); return isset($matches[0]) ? $matches[0] : $base64;