jeremybrammer/laravelimagetos3package

dev-master 2020-10-25 17:07 UTC

This package is auto-updated.

Last update: 2024-04-26 00:30:46 UTC


README

Latest Version on Packagist Total Downloads Build Status

A simple Laravel package, to handle image uploads. This package will create a database migration for images, and upload them, resize them, store them on s3, and pre-sign CloudFront URLs!

Links:

Installation Steps for this repository:

Install the composer project:

composer install

Require the package.

composer require jeremybrammer/laravelimagetos3package

Publish the package's config files. It publishes a config file for a dependency.

php artisan vendor:publish --provider="jeremybrammer\laravelimagetos3package\laravelimagetos3packageServiceProvider"

Migrate the database to get the new image uploads database table going.

php artisan migrate

Change/Add the following lines in the .env file:

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=
AWS_BUCKET=
AWS_URL=
CLOUDFRONT_PRIVATE_KEY_PATH=keys/my_key.pem
CLOUDFRONT_KEY_PAIR_ID=

(Add your CloudFront key to /storage/keys/my_key.pem. This should be .gitignored already).

Depending on your server configuration, increase nginx.conf and php.ini settings as needed to allow larger image uploads, and memory limits.

Package Usage in Controllers:

Include Laravel's Request class, and the following classes, models, and facades from my package.

use Illuminate\Http\Request;
use jeremybrammer\laravelimagetos3package\laravelimagetos3package;
use jeremybrammer\laravelimagetos3package\Models\ImageUpload;
use jeremybrammer\laravelimagetos3package\Facades\LaravelImageToS3PackageFacade;

Gets all previously uploaded images and pre-signs the CloudFront URLs for the thumbnails.

LaravelImageToS3PackageFacade::getAllUploadedImages(); 

Optionally override the image size settings in the upload service.

LaravelImageToS3PackageFacade::setWidthByImageType("thumbnail", 100);
LaravelImageToS3PackageFacade::setWidthByImageType("small", 200);

Call the upload handler with the request, html image field name attribute, and folder in s3 to store them.

LaravelImageToS3PackageFacade::handUploadRequest($request, "image-upload-field", "victorycto/images");

A controller example to view individual images that uses my eloquent model with route-model-binding:

public function view(ImageUpload $imageUpload, $imagetype){
    //Use route-model binding for the image object, and an image type to get the proper size.
    switch($imagetype){
        case "thumbnail": $url = $imageUpload->thumbnail_image_url; break;
        case "small": $url = $imageUpload->small_image_url; break;
        case "original": $url = $imageUpload->original_image_url; break;
        default: return; break;
    }
    // $imageURL = $this->imagetos3->preSignS3Url($imageUpload->original_image_url); //Sign s3 URL.
    $imageURL = LaravelImageToS3PackageFacade::preSignCloudFrontUrl($url); //Sign CloudFront URL.
    return view("imageuploads.view", ["imageURL" => $imageURL]);
}

Enjoy!

Change log

Please see the changelog for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email author email instead of using the issue tracker.

Credits

License

license. Please see the license file for more information.