Skip to content

Commit

Permalink
Add laravel storage support.
Browse files Browse the repository at this point in the history
  • Loading branch information
gzoran committed Jan 20, 2019
1 parent 1a284b5 commit c61870c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 36 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,7 +1,8 @@
.idea
.DS_Store
phpunit.phar
/vendor
composer.phar
composer.lock
*.project
.idea/
.idea/
16 changes: 16 additions & 0 deletions README.md
Expand Up @@ -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,服务端再转回图片保存的

Expand Down
93 changes: 58 additions & 35 deletions src/Crop.php
Expand Up @@ -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;
Expand All @@ -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 <zhengzhe94@gmail.com>
* @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 <zhengzhe94@gmail.com>
* @param $basename
* @return $this
*/
public function basename($basename)
{
if ($basename) {
$this->basename = $basename;
}

return $this;
}

/**
* @author Mike <zhengzhe94@gmail.com>
* @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 <zhengzhe94@gmail.com>
* @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;
Expand Down

0 comments on commit c61870c

Please sign in to comment.