Introduction
This article is my learning log of saving images and resize it afterwards with Laravel and its package Internention
Install package Intervention
Please refer to the installing guide on its official GitHubcomposer require intervention/image
Find
config/app.php
, and addIntervention\Image\ImageServiceProvider::class
in array
$providers
, and add'Image' => Intervention\Image\Facades\Image::class
in array
aliases
Build the link between external folder and internal folder where we are going to save images in.
Following the official website and build the link
- Type the follows In terminal
php artisan storage:link
- Type the follows In terminal
After the command, the
project/storage/app/public
andproject/public/storage
will be linked and so shared with each other- When you save files, please save it to
project/storage/app/public/(anySubdirectoryYouWant)
- If you want to produce externally accessible URL, use
project/public/storage/(anySubdirectoryYouWant)/fileName
, because from external, the default accessible folder ispublic
, so please useasset('storage/(anySubdirectoryYouWant/fileName)')
- When you save files, please save it to
Validate if the image is brought
// Because we don't need a lot of stuff, so we could only take what's in $request array. |
Re-organise the size of the image
- We are going to do resizing, so we will need the package intervention.
- Below namespace, add
use Intervention\Image\ImageManagerStatic as Image;
- Below namespace, add
// Get the instance of the item I just inserted |
Delete the image upon the user’s request
if ($request->imageDelete == true) |
Produce publicly accessible URL
// When returning publicly accessible URL, it will have to be external address. |
Comments