尝试在上传文件保存到服务器时调整其大小
Trying to resize uploaded files as they are saved to server
我正在使用 Glide 从我的网站之一传送图像内容。这运行良好,我现在已经构建了一个文件上传,以便管理员可以将图像上传到站点以供后续下载。
管理员上传的一些图片会比我需要的大很多(或者想要存储在服务器上的开销),所以我想缩小它们的尺寸,最好是在上传过程中或者上传失败时,就在上传之后他们已保存到新位置 (storage/app/images)
因此,由于我对 getClientOriginalName/Extension 等
中可用的文件名和路径的理解很差,我一直在进行干预,但没有取得太大成功。
任何人都可以向我展示一个可以正常工作的模式。理想情况下,我很想包括一些我在其他人的例子中看到的东西,比如...
$img = Image::make('foo.jpg')->resize(300, 200);
...在我的代码中的正确位置
foreach($files as $file) {
$fileExtension = $file->getClientOriginalExtension();
$fileMimeType = $file->getMimeType();
if(in_array($fileExtension, $allowableExtensions)) {
if(in_array($fileMimeType, $allowableMimes)) {
array_push($dbFileList, $file->getClientOriginalName());
$newImage = '/images/' . $propertyCode . '/' . $file->getClientOriginalName();
Storage::put('/images/' . $propertyCode . '/' . $file->getClientOriginalName(), file_get_contents($file));
}else{
$errorMessage = 'At least one file was not an image, check your results...';
}
}else{
$errorMessage = 'At least one file was not an image, check your results...';
}
}
更新 1:
Storage::put('/images/' . $propertyCode . '/' . $file->getClientOriginalName(), file_get_contents($file));
$img = Image::make($file);
Storage::put('/images/new/' . $file->getClientOriginalName(), $img);
此更新后的代码将文件输出到 /new 目录,一切看起来都很好,但输出文件有 'zero bytes'。我错过了什么?
更新 2:最终代码
最终答案(在使用贡献者提供的正确代码之后)是:
- 我不得不将我的应用程序从虚拟机移到开发机器 (iMac) 上,以防止与路径产生额外的混淆
- 在执行 ->save() 之前,图像的路径必须存在
- 路径变量必须在 ->save() 之前设置
- 我根本不需要 Storage::put,所以更大的文件永远不会出现在服务器上。
然后这个最终代码开始工作。
$path = storage_path('app/smallpics/')."/".$file->getClientOriginalName();
$img = Image::make($file)->resize(300,200)->save($path);
非常感谢大家。你让我的 Laravel 学习曲线变得不那么可怕了!!
图片上传和调整大小的工作流程如下:
- 将图像从
tmp
上传到您的目录。
- 通过设置
height, width, quality
复制该图像并将其保存在相同或其他目录中。
- 删除原图。
所以根据您的代码流程:
Storage::put('/images/' . $propertyCode . '/' . $file->getClientOriginalName(), file_get_contents($file));
在这段代码之后,放上图片压缩代码,然后删除原图。
您可以使用 Intervention
来处理图像(调整大小等),如
$new_image = Image::make($file)->resize(300,200)->save('/path/to/save');
您可以使用 Intervention or just use imagemagick convert command line 命令调整大小或转换。
关注评论:
public function saveUploadPic(Request $request)
{
$pic = $request->file('<NAME_OF_FILE_INPUT_IN_HTML_FORM>');
#check for upload correctly
if(!$pic->isValid())
{
throw new Exception("IMAGE NOT UPLOADED CORRECTLY");
}
#check for mime type and extention
$ext = $pic->getClientOriginalExtension();
$mime = $pic->getMimeType();
if(!in_array($mime, $allowedMimeTypeArray) || !in_array($ext, $allowedExtArray))
{
throw new Exception("This Image Not Support");
}
#check for size
$size = $pic->getClientSize() / 1024 / 1024;
if($size > $allowedSize)
{
throw new Exception("Size Of Image Is More Than Support Size");
}
########################YOU HAVE TWO OPTION HERE###################
#1- save image in a temporary location with random hash for name if u need orginal image for other process
#below code save image in <LARAVEL_APP_PATH>/storage/app/tmp/pics/
$hash = md5(date("YmdHis").rand(1,10000));
$pic->storeAs('tmp/pics', $hash.'.'.$ext);
#Then resize or convert it
$img = Image::make(storage_path('app/tmp/pics/'.$hash.'.'.$ext))->resize(300, 200);
#save new image whatever u want
$img->save('<PATH_TO_SAVE_IMAGE>');
#after u finish with orginal image delete it
Storage::delete(storage_path('app/tmp/pics/'.$hash.'.'.$ext);
#2- Or just use below for resize and save image witout need to save in temporary location
$img = Image::make($pic->getRealPath())->resize(300,200);
$img->save('<PATH_TO_SAVE_IMAGE>');
}
如果您想使用转换,请参阅 this link。
我正在使用 Glide 从我的网站之一传送图像内容。这运行良好,我现在已经构建了一个文件上传,以便管理员可以将图像上传到站点以供后续下载。
管理员上传的一些图片会比我需要的大很多(或者想要存储在服务器上的开销),所以我想缩小它们的尺寸,最好是在上传过程中或者上传失败时,就在上传之后他们已保存到新位置 (storage/app/images)
因此,由于我对 getClientOriginalName/Extension 等
中可用的文件名和路径的理解很差,我一直在进行干预,但没有取得太大成功。任何人都可以向我展示一个可以正常工作的模式。理想情况下,我很想包括一些我在其他人的例子中看到的东西,比如...
$img = Image::make('foo.jpg')->resize(300, 200);
...在我的代码中的正确位置
foreach($files as $file) {
$fileExtension = $file->getClientOriginalExtension();
$fileMimeType = $file->getMimeType();
if(in_array($fileExtension, $allowableExtensions)) {
if(in_array($fileMimeType, $allowableMimes)) {
array_push($dbFileList, $file->getClientOriginalName());
$newImage = '/images/' . $propertyCode . '/' . $file->getClientOriginalName();
Storage::put('/images/' . $propertyCode . '/' . $file->getClientOriginalName(), file_get_contents($file));
}else{
$errorMessage = 'At least one file was not an image, check your results...';
}
}else{
$errorMessage = 'At least one file was not an image, check your results...';
}
}
更新 1:
Storage::put('/images/' . $propertyCode . '/' . $file->getClientOriginalName(), file_get_contents($file));
$img = Image::make($file);
Storage::put('/images/new/' . $file->getClientOriginalName(), $img);
此更新后的代码将文件输出到 /new 目录,一切看起来都很好,但输出文件有 'zero bytes'。我错过了什么?
更新 2:最终代码
最终答案(在使用贡献者提供的正确代码之后)是:
- 我不得不将我的应用程序从虚拟机移到开发机器 (iMac) 上,以防止与路径产生额外的混淆
- 在执行 ->save() 之前,图像的路径必须存在
- 路径变量必须在 ->save() 之前设置
- 我根本不需要 Storage::put,所以更大的文件永远不会出现在服务器上。
然后这个最终代码开始工作。
$path = storage_path('app/smallpics/')."/".$file->getClientOriginalName();
$img = Image::make($file)->resize(300,200)->save($path);
非常感谢大家。你让我的 Laravel 学习曲线变得不那么可怕了!!
图片上传和调整大小的工作流程如下:
- 将图像从
tmp
上传到您的目录。 - 通过设置
height, width, quality
复制该图像并将其保存在相同或其他目录中。 - 删除原图。
所以根据您的代码流程:
Storage::put('/images/' . $propertyCode . '/' . $file->getClientOriginalName(), file_get_contents($file));
在这段代码之后,放上图片压缩代码,然后删除原图。
您可以使用 Intervention
来处理图像(调整大小等),如
$new_image = Image::make($file)->resize(300,200)->save('/path/to/save');
您可以使用 Intervention or just use imagemagick convert command line 命令调整大小或转换。
关注评论:
public function saveUploadPic(Request $request)
{
$pic = $request->file('<NAME_OF_FILE_INPUT_IN_HTML_FORM>');
#check for upload correctly
if(!$pic->isValid())
{
throw new Exception("IMAGE NOT UPLOADED CORRECTLY");
}
#check for mime type and extention
$ext = $pic->getClientOriginalExtension();
$mime = $pic->getMimeType();
if(!in_array($mime, $allowedMimeTypeArray) || !in_array($ext, $allowedExtArray))
{
throw new Exception("This Image Not Support");
}
#check for size
$size = $pic->getClientSize() / 1024 / 1024;
if($size > $allowedSize)
{
throw new Exception("Size Of Image Is More Than Support Size");
}
########################YOU HAVE TWO OPTION HERE###################
#1- save image in a temporary location with random hash for name if u need orginal image for other process
#below code save image in <LARAVEL_APP_PATH>/storage/app/tmp/pics/
$hash = md5(date("YmdHis").rand(1,10000));
$pic->storeAs('tmp/pics', $hash.'.'.$ext);
#Then resize or convert it
$img = Image::make(storage_path('app/tmp/pics/'.$hash.'.'.$ext))->resize(300, 200);
#save new image whatever u want
$img->save('<PATH_TO_SAVE_IMAGE>');
#after u finish with orginal image delete it
Storage::delete(storage_path('app/tmp/pics/'.$hash.'.'.$ext);
#2- Or just use below for resize and save image witout need to save in temporary location
$img = Image::make($pic->getRealPath())->resize(300,200);
$img->save('<PATH_TO_SAVE_IMAGE>');
}
如果您想使用转换,请参阅 this link。