laravel-image
Basic image saver for Laravel 5., (*1)
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., (*2)
So this package will do it for you., (*3)
, (*4)
Installation
Laravel Image is distributed as a composer package:, (*5)
composer require unglud/laravel-image
If you want to change configs later, please publish config file first:, (*6)
php artisan vendor:publish
Afterward, you can edit the file config/lavaimage.php., (*7)
Saving Image
Use LavaImage::save() to save image to public/uploads, this method generate unique 8 char filename and put file to deep tree folder structure., (*8)
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();
File structure
You can specify another folder structure, like any depth or folder name length in the config file, (*9)
for 203bad62 it can be
/2/0/203bad62.jpg
/2/0/3/b/203bad62.jpg
/20/203bad62.jpg
/20/3b/203bad62.jpg
etc....
Crop and save
You can specify size as second argument for center fit cropping, (*10)
LavaImage::save('http://lorempixel.com/300/300/', [100,100]);
As the first argument, you can pass any data, what Intervention/image make method support, (*11)
// 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(), (*12)
Getting Image
Then you need to get an image, use hash you know, (*13)
$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
License
Laravel Image is released under the MIT Licence. See the bundled LICENSE file for details., (*14)