Intervention을 이용한 이미지 포맷 변경
install
composer require intervention/image
사용예제
추천 드리고 싶은 사용법
use Intervention\Image\Facades\Image;
Image::make(storage_path('app/public/...'))
->resize(500, 300)->save(storage_path('app/public/...'));
Image::make(storage_path('app/public/...'))
->encode('png')->save(storage_path('app/public/...'));
use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Gd\Driver;
// create image manager with desired driver
$manager = new ImageManager(new Driver());
// read image from file system
$image = $manager->read('images/example.jpg');
// resize image proportionally to 300px width
$image->scale(width: 300);
// insert watermark
$image->place('images/watermark.png');
// save modified image in new format
$image->toPng()->save('images/foo.png');
$interventionImage = \Image::make($image)->stream("webp", 100); // in this case webp is the format and the quality is set to 100%
\Storage::disk('s3')->put('some-imge-name.webp', $interventionImage, 'public');
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
use Intervention\Image\ImageManagerStatic;
use Illuminate\Support\Str;
class ImageController extends Controller {
public function index(): View {
return view('image-upload');
}
public function store(Request $request): RedirectResponse {
$this->validate($request, [ 'image' => ['required', 'image', 'mimes:jpg,png,jpeg,gif,svg', 'max:2048'], ]);
$input = $request->all();
$image = ImageManagerStatic::make($request->file('image'))->encode('webp');
$imageName = Str::random().'.webp';
$image->save(public_path('images/'. $imageName));
$input['image_name'] = $imageName;
return back() ->with('success', 'Image converted and saved successfully!') ->with('imageName', $imageName);
}
}
적용 가능한 포맷
- jpg — return JPEG encoded image data
- png — return Portable Network Graphics (PNG) encoded image data
- gif — return Graphics Interchange Format (GIF) encoded image data
- tif — return Tagged Image File Format (TIFF) encoded image data
- bmp — return Bitmap (BMP) encoded image data
- data-url — encode current image data in data URI scheme (RFC 2397)