Basic image saver for Laravel 5.
If you need save uploaded image to some place and to a database, the best way to do that is to save an image in public folder with the unique name and then save that name to the database.
So this package will do it for you.
Laravel Image is distributed as a composer package:
composer require unglud/laravel-image
If you want to change configs later, please publish config file first:
php artisan vendor:publish
Afterward, you can edit the file config/lavaimage.php
.
Use LavaImage::save()
to save image to public/uploads
, this method generate unique 8 char filename and put file to deep tree folder structure.
use Unglued\LavaImage\Facades\LavaImage;
$fileHash = LavaImage::save('http://lorempixel.com/300/300/');
// $fileHash == 203bad62
// and file stored in /public/uploads/2/0/203bad62.jpg
// now you can save hash to file in your storage
$myModel = new MyModel();
$myModel->image = $fileHash;
$myModel->save();
You can specify another folder structure, like any depth or folder name length in the config file
for 203bad62 it can be
/2/0/203bad62.jpg
/2/0/3/b/203bad62.jpg
/20/203bad62.jpg
/20/3b/203bad62.jpg
etc....
You can specify size as second argument for center fit cropping
LavaImage::save('http://lorempixel.com/300/300/', [100,100]);
As the first argument, you can pass any data, what Intervention/image make method support
// save image from file
LavaImage::save('public/foo.jpg');
// or save image from binary data
LavaImage::save(file_get_contents('public/foo.jpg'));
// save image from gd resource
LavaImage::save(imagecreatefromjpeg('public/foo.jpg'));
// save image directly from an url
LavaImage::save('http://example.com/example.jpg');
// save image directly from Laravel file upload
LavaImage::save(Input::file('photo'));
Any time after saving you can retrieve generated hash by LavaImage::getImageCode()
Then you need to get an image, use hash you know
$hash = '203bad62'
LavaImage::getImage($hash);
// will return http://example.com/uploads/2/0/203bad62.jpg
LavaImage::getImage($hash, true);
// will return absolute path /home/var/laravel/public/uploads/2/0/203bad62.jpg
Laravel Image is released under the MIT Licence. See the bundled LICENSE file for details.