Image manipulation with Laravel makes image manipulation easier and more expressive. This article is a tutorial to demonstrate how to handle and to manipulate images in Laravel using the Intervention Image library.
Are you looking to create image thumbnails, watermarks, or image files? This library aids you to work smoothly without any fuss or interruptions. For this library, you have to use PHP version 5.4 that comes with Laravel Facades and Service Providers to simplify the optional framework integration.
Ultimate Image Manipulation with Laravel-Take the next step
Are you missing out on the best performance optimization for image manipulation? Well, now you have access to all the tips and tricks with the help of this elaborate and easy tutorial.
Intervention Image Requirements & Installation
For Intervention Image, you require the following components to work efficiently.
- PHP >= 5.4
- Fileinfo Extension
- GD Library (>=2.0) … or …
- Imagick PHP extension (>=6.5.7)
You can install and configure these, they are available here.
Meanwhile, we will explore quick and fundamental methods of the php library concerning image manipulation with Laravel in any particular project.
$img = Image::make('foo/bar/baz.jpg');
The method proves to be highly variable. It not only reads file paths but also the following input formats.
- Path of the image in the filesystem.
- URL of an image (allowurlfopen must be enabled).
- Binary image data.
- Data-URL encoded image data.
- Base64 encoded image data.
- PHP resource of type gd. (when using GD driver)
- Imagick instance (when using Imagick driver)
- Intervention\Image\Image instance
- SplFileInfo instance (To handle Laravel file uploads via
Upload Image on local disk/Amazon via Controller
Image upload on local disk
$imageName = time() . '-' . $image->getClientOriginalName(); $image->move(public_path('/uploads/'), $imageName);
Here’s how to upload image on Amazon s3 disk. If you have already completed completed Amazon s3 bucket configuration in Laravel project, then you can understand the code and method easily.
$image = $request->file('image'); $imageName = time() . '-' . $image->getClientOriginalName(); Image::make($image); $filePath = 'uploads/' . $imageName; Storage::disk('s3')->put($filePath, filegetcontents($image), 'public');
Resizing of the image
You can resize image with given width and height as you upload on Amazon or saving on local disk. These both parameters are optional. You can have a look below on how to do it in a better and quicker way:
// resize image with width & height
// resize only the width of the image
// resize only the height of the image
Optimization of the image
You can use last parameter to set image optimization of the fly
$img->save(public_path('uploads/' . $imageName), 60);
Parameters for image manipulation with Laravel
Path to the file where you can write the image data. If the image is created from an existing image in the file system and the parameter is not set, the method will try to overwrite the existing file.
You can optionally define the quality of the image. It is normalized for all file types to a range from 0 (poor quality, small file) to 100 (best quality, big file). Quality is only applied if you’re encoding JPG format since PNG compression is lossless and does not affect image quality. The default value is 90.
By default, the format of the saved image is defined by the file extension of the given path. Alternatively, it is possible to define the image format by passing one of the image format extension as a third parameter.
Here’s how you can conveniently define the encoding format from one of the following formats:
- 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
- ico — return ICO encoded image data
- psd — return Photoshop Document (PSD) encoded image data
- webp — return WebP encoded image data
- data-url — encode current image data in data URI scheme (RFC 2397)
By default the method will return data encoded in the type of the current image. If no image type is defined yet, data will be encoded as jpeg.
// encode png image as jpg
// encode png image as jpg
There is large number of list of useful functions related to image manipulation. e.g:
- Rotate image – rotate()
- Apply mask on image – mask()
- Image greyscale – greyscale()
- Set opacity of image – opacity()
- Image blur – blur()
Choose Efficient Image manipulation with Laravel
You can find solutions for all the challenges related to the code or image handling or image manipulation. If the existing method is causing any trouble, you can always replace it with better available options like image manipulation with Laravel.
Don’t limit your results to chances or what’s available. There are multiple outstanding methods and ways to get the best outcome from image manipulation and handling. Do you have any better suggestions or queries? Feel absolutely free to ask our experts! Share your valuable feedback related to the tutorial in the comment section below.
Find out more about image manipulation with Laravel, Contact vteams!