After the command, the project/storage/app/public and project/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 is public, so please use asset('storage/(anySubdirectoryYouWant/fileName)')
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. $parameters = request()->all();
if (request()->hasFile('image')) { // The file exists, so we save it to project/storage/app/public, and get the URL. In case, we will get 'public/fileName' $imageURL = request()->file('image')->store('public'); // However, we only want to insert pure file name into the database, so we remove the 'public' and only leave 'fileName' $parameters['image'] = substr($imageURL, 7); }
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;
// Get the instance of the item I just inserted $item = Item::update($parameters);
// Set the driver Image::configure(array('driver' => 'gd'));
// If we simply dd(storage_path), we will get 'project/store/', but it's not what we want. // So we append 'app/public/', that's the internal file address // Note that when resizing images, the target is in internal directory // Also, after resizing, we save it with the same name in the same place. Image::make(storage_path('app/public/' . $item->image)) ->resize(300, 300) ->save(storage_path('app/public/' . $item->image));
Delete the image upon the user’s request
if ($request->imageDelete == true) { Storage::delete($item->images); $item->update(['images' => null]); }
Produce publicly accessible URL
// When returning publicly accessible URL, it will have to be external address. return asset('storage/' . $parameters['image']);
Comments