laravel - 调整图像大小并保存到 Amazon S3
laravel - Resize images and save to Amazon S3
当用户上传他的个人资料照片时,我想创建此图片的 3 个不同尺寸的版本,然后将所有内容上传到 amazon s3。
我使用图像干预包来调整图像大小,这是我目前的代码。
public function store(Request $request){
if($request->has('avatar')){
$avatar = $request->file('avatar');
$filename = md5(time()).'_'.$avatar->getClientOriginalName();
$normal = Image::make($avatar)->resize(160, 160);
$medium = Image::make($avatar)->resize(80, 80);
$small = Image::make($avatar)->resize(40, 40);
Storage::disk('s3')->put('/users/'.Auth::user()->uuid.'/avatars/normal/'.$filename, fopen($normal, 'r+'), 'public');
Storage::disk('s3')->put('/users/'.Auth::user()->uuid.'/avatars/medium/'.$filename, fopen($medium, 'r+'), 'public');
Storage::disk('s3')->put('/users/'.Auth::user()->uuid.'/avatars/small/'.$filename, fopen($small, 'r+'), 'public');
$user = User::findorFail(Auth::user()->id);
$user->avatar = $filename;
$user->save();
return redirect()->back();
}
}
当我尝试提交文件时出现此错误。
fopen(): Filename cannot be empty
感谢任何帮助
使用 Intervention 时,请务必提供文件在您的文件系统中的路径。
我能看到的是你给出了一个字符串(文件名,而不是路径)。
因此,干预可以创建图像。
因此您可以先将文件保存在本地文件系统中,然后在调用时提供完整路径:
$normal = Image::make($full_path)->resize(160, 160);
更新 我是这样工作的,如果有人有同样的问题我希望他会发现这段代码有用。
public function store(Request $request){
if($request->has('avatar')){
$avatar = $request->file('avatar');
$extension = $request->file('avatar')->getClientOriginalExtension();
$filename = md5(time()).'_'.$avatar->getClientOriginalName();
$normal = Image::make($avatar)->resize(160, 160)->encode($extension);
$medium = Image::make($avatar)->resize(80, 80)->encode($extension);
$small = Image::make($avatar)->resize(40, 40)->encode($extension);
//$path = '/users/'.Auth::user()->uuid.'/avatar/normal/'.$filename;
//dd($normal);
Storage::disk('s3')->put('/users/'.Auth::user()->uuid.'/avatar/normal/'.$filename, (string)$normal, 'public');
Storage::disk('s3')->put('/users/'.Auth::user()->uuid.'/avatar/medium/'.$filename, (string)$medium, 'public');
Storage::disk('s3')->put('/users/'.Auth::user()->uuid.'/avatar/small/'.$filename, (string)$small, 'public');
$user = User::findorFail(Auth::user()->id);
$user->avatar = $filename;
$user->save();
return redirect()->back();
}
}
我有一个辅助控制器,你可以试试。
https://github.com/RashiqulRony/Help_Content/blob/master/MediaController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Image;
class MediaController
{
public $basePath = '';
public $originalPath = '';
public $file = '';
public $name = '';
public $thumbPath = '';
public $thumb = false;
public $storageFolder = 'storage/';
public $imageResize = [];
public $thumbResize = [300, 300];
//Common File Upload Function...
private function upload()
{
$file = $this->file;
if ($this->name) {
$fileName = Str::slug($this->name, '-').'.'.$file->getClientOriginalExtension();
} else {
$newName = str_replace('.'.$file->getClientOriginalExtension(), '', $file->getClientOriginalName());
$fileName = time().'-'.Str::slug($newName, '-').'.'.$file->getClientOriginalExtension();
}
$data['name'] = $fileName;
$data['originalName'] = $file->getClientOriginalName();
$data['size'] = $file->getSize();
$data['mime_type'] = $file->getMimeType();
$data['ext'] = $file->getClientOriginalExtension();
$data['url'] = url($this->storageFolder.$this->originalPath.$data['name']);
//If real image need to resize...
if (!empty($this->imageResize)) {
$file = Image::make($file)->resize($this->imageResize[0], $this->imageResize[1]);
Storage::put($this->originalPath.'/'.$fileName, (string) $file->encode());
} else {
Storage::putFileAs($this->originalPath, $file, $data['name']);
}
if ($this->thumb) {
Image::make($this->storageFolder.$this->originalPath.$data['name'])
->resize($this->thumbResize[0], $this->thumbResize[1])
->save($this->storageFolder.$this->thumbPath.'/'.$data['name']);
}
return $data;
}
//Upload Image ("$definePath" and "$definePath/thumb") folder....
public function imageUpload($requestFile, $path, $thumb = false, $name = null, $imageResize = [], $thumbResize = [300, 300])
{
//Path Create...
$realPath = $this->basePath.$path.'/';
if (!Storage::exists($realPath)) {
Storage::makeDirectory($realPath);
}
if (!Storage::exists($realPath.'thumb') && $thumb) {
Storage::makeDirectory($realPath.'thumb');
}
$this->file = $requestFile;
$this->originalPath = $realPath;
$this->thumbPath = $realPath.'thumb';
$this->thumb = $thumb;
$this->name = $name;
$this->imageResize = $imageResize;
$this->thumbResize = $thumbResize;
return $this->upload();
}
//Upload Video in "$definePath" folder....
public function videoUpload($requestFile, $path, $name = null)
{
//Path Create...
$realPath = $this->basePath.$path.'/';
if (!Storage::exists($realPath)) {
Storage::makeDirectory($realPath);
}
$this->file = $requestFile;
$this->originalPath = $realPath;
$this->name = $name;
return $this->upload();
}
//Upload AnyFile in "$definePath" folder....
public function anyUpload($requestFile, $path, $name = null)
{
//Path Create...
$realPath = $this->basePath.$path.'/';
if (!Storage::exists($realPath)) {
Storage::makeDirectory($realPath);
}
$this->file = $requestFile;
$this->originalPath = $realPath;
$this->name = $name;
return $this->upload();
}
//Upload Content in "$definePath" folder....
public function contentUpload($content, $path, $name)
{
//Path Create...
$realPath = $this->basePath.$path.'/';
if (!Storage::exists($realPath)) {
Storage::makeDirectory($realPath);
}
Storage::put($name, $content);
$data['name'] = $name;
$data['url'] = $path.'/'.$name;
return $data;
}
//Only thumb image create in "$definePath/thumb" folder....
public function thumb($path, $file, $thumbPath = false, $thumbWidth = 300, $thumbHeight = 300)
{
$realPath = $this->basePath.$path;
if (!$thumbPath) {
$thumbPath = $this->basePath.$path.'/thumb';
}
if (!Storage::exists($thumbPath)) {
Storage::makeDirectory($thumbPath);
}
$img = Image::make($this->storageFolder.$realPath.'/'.$file)
->resize($thumbWidth, $thumbHeight)
->save($this->storageFolder.$thumbPath.'/'.$file);
if (isset($img->filename)) {
return true;
} else {
return false;
}
}
//Delete file "$definePath" folder....
public function delete($path, $file, $thumb = false)
{
$path = $this->basePath.$path.'/';
if (Storage::exists($path.'/'.$file)) {
Storage::delete($path.'/'.$file);
if ($thumb) {
Storage::delete($path.'/thumb/'.$file);
}
return true;
}
return false;
}
}
当用户上传他的个人资料照片时,我想创建此图片的 3 个不同尺寸的版本,然后将所有内容上传到 amazon s3。
我使用图像干预包来调整图像大小,这是我目前的代码。
public function store(Request $request){
if($request->has('avatar')){
$avatar = $request->file('avatar');
$filename = md5(time()).'_'.$avatar->getClientOriginalName();
$normal = Image::make($avatar)->resize(160, 160);
$medium = Image::make($avatar)->resize(80, 80);
$small = Image::make($avatar)->resize(40, 40);
Storage::disk('s3')->put('/users/'.Auth::user()->uuid.'/avatars/normal/'.$filename, fopen($normal, 'r+'), 'public');
Storage::disk('s3')->put('/users/'.Auth::user()->uuid.'/avatars/medium/'.$filename, fopen($medium, 'r+'), 'public');
Storage::disk('s3')->put('/users/'.Auth::user()->uuid.'/avatars/small/'.$filename, fopen($small, 'r+'), 'public');
$user = User::findorFail(Auth::user()->id);
$user->avatar = $filename;
$user->save();
return redirect()->back();
}
}
当我尝试提交文件时出现此错误。
fopen(): Filename cannot be empty
感谢任何帮助
使用 Intervention 时,请务必提供文件在您的文件系统中的路径。 我能看到的是你给出了一个字符串(文件名,而不是路径)。 因此,干预可以创建图像。
因此您可以先将文件保存在本地文件系统中,然后在调用时提供完整路径:
$normal = Image::make($full_path)->resize(160, 160);
更新 我是这样工作的,如果有人有同样的问题我希望他会发现这段代码有用。
public function store(Request $request){
if($request->has('avatar')){
$avatar = $request->file('avatar');
$extension = $request->file('avatar')->getClientOriginalExtension();
$filename = md5(time()).'_'.$avatar->getClientOriginalName();
$normal = Image::make($avatar)->resize(160, 160)->encode($extension);
$medium = Image::make($avatar)->resize(80, 80)->encode($extension);
$small = Image::make($avatar)->resize(40, 40)->encode($extension);
//$path = '/users/'.Auth::user()->uuid.'/avatar/normal/'.$filename;
//dd($normal);
Storage::disk('s3')->put('/users/'.Auth::user()->uuid.'/avatar/normal/'.$filename, (string)$normal, 'public');
Storage::disk('s3')->put('/users/'.Auth::user()->uuid.'/avatar/medium/'.$filename, (string)$medium, 'public');
Storage::disk('s3')->put('/users/'.Auth::user()->uuid.'/avatar/small/'.$filename, (string)$small, 'public');
$user = User::findorFail(Auth::user()->id);
$user->avatar = $filename;
$user->save();
return redirect()->back();
}
}
我有一个辅助控制器,你可以试试。 https://github.com/RashiqulRony/Help_Content/blob/master/MediaController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Image;
class MediaController
{
public $basePath = '';
public $originalPath = '';
public $file = '';
public $name = '';
public $thumbPath = '';
public $thumb = false;
public $storageFolder = 'storage/';
public $imageResize = [];
public $thumbResize = [300, 300];
//Common File Upload Function...
private function upload()
{
$file = $this->file;
if ($this->name) {
$fileName = Str::slug($this->name, '-').'.'.$file->getClientOriginalExtension();
} else {
$newName = str_replace('.'.$file->getClientOriginalExtension(), '', $file->getClientOriginalName());
$fileName = time().'-'.Str::slug($newName, '-').'.'.$file->getClientOriginalExtension();
}
$data['name'] = $fileName;
$data['originalName'] = $file->getClientOriginalName();
$data['size'] = $file->getSize();
$data['mime_type'] = $file->getMimeType();
$data['ext'] = $file->getClientOriginalExtension();
$data['url'] = url($this->storageFolder.$this->originalPath.$data['name']);
//If real image need to resize...
if (!empty($this->imageResize)) {
$file = Image::make($file)->resize($this->imageResize[0], $this->imageResize[1]);
Storage::put($this->originalPath.'/'.$fileName, (string) $file->encode());
} else {
Storage::putFileAs($this->originalPath, $file, $data['name']);
}
if ($this->thumb) {
Image::make($this->storageFolder.$this->originalPath.$data['name'])
->resize($this->thumbResize[0], $this->thumbResize[1])
->save($this->storageFolder.$this->thumbPath.'/'.$data['name']);
}
return $data;
}
//Upload Image ("$definePath" and "$definePath/thumb") folder....
public function imageUpload($requestFile, $path, $thumb = false, $name = null, $imageResize = [], $thumbResize = [300, 300])
{
//Path Create...
$realPath = $this->basePath.$path.'/';
if (!Storage::exists($realPath)) {
Storage::makeDirectory($realPath);
}
if (!Storage::exists($realPath.'thumb') && $thumb) {
Storage::makeDirectory($realPath.'thumb');
}
$this->file = $requestFile;
$this->originalPath = $realPath;
$this->thumbPath = $realPath.'thumb';
$this->thumb = $thumb;
$this->name = $name;
$this->imageResize = $imageResize;
$this->thumbResize = $thumbResize;
return $this->upload();
}
//Upload Video in "$definePath" folder....
public function videoUpload($requestFile, $path, $name = null)
{
//Path Create...
$realPath = $this->basePath.$path.'/';
if (!Storage::exists($realPath)) {
Storage::makeDirectory($realPath);
}
$this->file = $requestFile;
$this->originalPath = $realPath;
$this->name = $name;
return $this->upload();
}
//Upload AnyFile in "$definePath" folder....
public function anyUpload($requestFile, $path, $name = null)
{
//Path Create...
$realPath = $this->basePath.$path.'/';
if (!Storage::exists($realPath)) {
Storage::makeDirectory($realPath);
}
$this->file = $requestFile;
$this->originalPath = $realPath;
$this->name = $name;
return $this->upload();
}
//Upload Content in "$definePath" folder....
public function contentUpload($content, $path, $name)
{
//Path Create...
$realPath = $this->basePath.$path.'/';
if (!Storage::exists($realPath)) {
Storage::makeDirectory($realPath);
}
Storage::put($name, $content);
$data['name'] = $name;
$data['url'] = $path.'/'.$name;
return $data;
}
//Only thumb image create in "$definePath/thumb" folder....
public function thumb($path, $file, $thumbPath = false, $thumbWidth = 300, $thumbHeight = 300)
{
$realPath = $this->basePath.$path;
if (!$thumbPath) {
$thumbPath = $this->basePath.$path.'/thumb';
}
if (!Storage::exists($thumbPath)) {
Storage::makeDirectory($thumbPath);
}
$img = Image::make($this->storageFolder.$realPath.'/'.$file)
->resize($thumbWidth, $thumbHeight)
->save($this->storageFolder.$thumbPath.'/'.$file);
if (isset($img->filename)) {
return true;
} else {
return false;
}
}
//Delete file "$definePath" folder....
public function delete($path, $file, $thumb = false)
{
$path = $this->basePath.$path.'/';
if (Storage::exists($path.'/'.$file)) {
Storage::delete($path.'/'.$file);
if ($thumb) {
Storage::delete($path.'/thumb/'.$file);
}
return true;
}
return false;
}
}