使用 intervention/image 调整图像大小
Resize image with intervention/image
我有一个保存图片的功能,我需要它把图片缩小到指定的尺寸。我决定使用包 intervention/image
。
我用 composer require intervention/image
安装了这个包。然后我对我的功能进行了更改。最后变成了这样:
控制器:
use Intervention\Image\Facades\Image;
if ($request->hasFile('preview_image')) {
$file = $request->file('preview_image');
$namewithextension = $file->getClientOriginalName();
$name = explode('.', $namewithextension)[0];
$extension = $file->getClientOriginalExtension();
$uploadname = $name. '-' .DateTime::createFromFormat('U.u', microtime(TRUE))->format('U-u') . '.' .$extension;
$img = Image::make($file)->resize(240, 120);
$article_block_image->preview_image = $img->save(public_path('public/article-block-image/preview' , $uploadname));
}
型号:
public function getOriginPreviewImageUrl() {
return $this->attributes['preview_image'];
}
public function getPreviewImageAttribute($value) {
return Storage::exists($value) ? Storage::url($value) : null;
}
当我尝试上传图片时出现此错误。
"message": "Can't write image data to path (D:\wamp64\www\test\public\public/article-block-image/preview)",
"exception": "Intervention\Image\Exception\NotWritableException",
"file": "D:\wamp64\www\test\vendor\intervention\image\src\Intervention\Image\Image.php",`
$path = public_path('public/article-block-image/preview' . $uploadname);
Image::make($image->getRealPath())->resize(468, 249)->save($path);
运行 命令:php artisan storage:link
我有一个保存图片的功能,我需要它把图片缩小到指定的尺寸。我决定使用包 intervention/image
。
我用 composer require intervention/image
安装了这个包。然后我对我的功能进行了更改。最后变成了这样:
控制器:
use Intervention\Image\Facades\Image;
if ($request->hasFile('preview_image')) {
$file = $request->file('preview_image');
$namewithextension = $file->getClientOriginalName();
$name = explode('.', $namewithextension)[0];
$extension = $file->getClientOriginalExtension();
$uploadname = $name. '-' .DateTime::createFromFormat('U.u', microtime(TRUE))->format('U-u') . '.' .$extension;
$img = Image::make($file)->resize(240, 120);
$article_block_image->preview_image = $img->save(public_path('public/article-block-image/preview' , $uploadname));
}
型号:
public function getOriginPreviewImageUrl() {
return $this->attributes['preview_image'];
}
public function getPreviewImageAttribute($value) {
return Storage::exists($value) ? Storage::url($value) : null;
}
当我尝试上传图片时出现此错误。
"message": "Can't write image data to path (D:\wamp64\www\test\public\public/article-block-image/preview)", "exception": "Intervention\Image\Exception\NotWritableException", "file": "D:\wamp64\www\test\vendor\intervention\image\src\Intervention\Image\Image.php",`
$path = public_path('public/article-block-image/preview' . $uploadname);
Image::make($image->getRealPath())->resize(468, 249)->save($path);
运行 命令:php artisan storage:link