[英]Save resized image with PHP GD in Storage:
如果您能幫助解釋如何將修改后的圖像保存在Storage::('users')中,將不勝感激。 imagejpeg($ imgResized $ image_path_name,90); 將其保存到公共根文件夾中,並以修改后的大小保存,但是我不希望在公共場所使用,我希望在存儲用戶中使用。 先感謝您。
$image_path = $request->file('image_path');
if ($image_path) {
$image_path_name = time().$image_path->getClientOriginalName();
$img = imagecreatefromjpeg($image_path);
$imgResized = imagescale($img, 100);
imagejpeg($imgResized, $image_path_name,90);
imagedestroy($img);
imagedestroy($imgResized);
Storage::disk('users')->put($image_path_name, File::get($image_path));
$user->image = $image_path_name;
}
$user->update();
如果您不想公開訪問圖像文件,請從磁盤配置中刪除公共可見性屬性和Web訪問URL。
config/filesystems.php
'users' => [
'driver' => 'local',
'root' => storage_path('app') . '/users',
],
在您的視圖中假設這樣的表單,例如: resources/views/welcome.blade.php
<form action="/file" method="POST" enctype="multipart/form-data">
@csrf
<input type="file" name="image_path">
<button type="submit">Submit</button>
</form>
在控制器或路由閉包中的存儲功能,例如: routes/web.php
編輯 :將存儲路徑作為第二個參數傳遞給imagejpeg
函數。
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
Route::get('/', function () {
return view('welcome');
});
Route::post('file', function (Request $request) {
$image_path = $request->file('image_path');
if ($image_path) {
$image_path_name = time() . $image_path->getClientOriginalName();
$img = imagecreatefromjpeg($image_path);
$imgResized = imagescale($img, 100);
// Specify storage directory
imagejpeg($imgResized, storage_path('app/users/') . $image_path_name, 90);
return view('test', compact('imgResized'));
imagedestroy($img);
imagedestroy($imgResized);
Storage::disk('users')->put($image_path_name, File::get($image_path));
}
});
然后,圖像將被私下存儲在storage/app/users/timestampfilename.extension
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.