Resize Image in Laravel 5.2

Resize image in Laravel 5.2

Laravel does not have a default resize of image. But most Laravel developers use 'Image intervention' in handling the image. It is easy to use.

To install (Image intervention):

STEP 1 Run

composer require intervention/image

STEP 2 On your config/app.php:

In the $providers array, add the following:

Intervention\Image\ImageServiceProvider::class

In the $aliases array,add the following:

'Image' => Intervention\Image\Facades\Image::class

If you have problems your GD library is missing, install it

  • PHP5: sudo apt-get install php5-gd
  • PHP7: sudo apt-get install php7.0-gd

To use on your controller.

STEP 3

On top of your controller

use Intervention\Image\ImageManagerStatic as Image;

STEP 4

On your method (there are several ways but this will give you an idea)

if($request->hasFile('image')) {

$image = $request->file('image');
$filename = $image->getClientOriginalName();

$image_resize = Image::make($image->getRealPath());
$image_resize->resize(300, 300);
$image_resize->save(public_path('images/ServiceImages/' .$filename));

}

Reference here.

Resize and replace image in Laravel request

I just read that Laravel uses PSR-7 requests.

https://laravel.com/docs/5.7/requests#psr7-requests

These are immutable. In other words, you can't change the data once set. What you can do however, is get it to create a new request with your new parameters.

Looking at the PSR-7 interface, we see there is a method which looks like exactly what you need:

https://github.com/php-fig/http-message/blob/master/src/ServerRequestInterface.php#L150

/**
* Create a new instance with the specified uploaded files.
*
* This method MUST be implemented in such a way as to retain the
* immutability of the message, and MUST return an instance that has the
* updated body parameters.
*
* @param array $uploadedFiles An array tree of UploadedFileInterface instances.
* @return static
* @throws \InvalidArgumentException if an invalid structure is provided.
*/
public function withUploadedFiles(array $uploadedFiles);

So, do your thing, create your array, and once it's ready, replace your request like this:

$request = $request->withUploadedFiles($yourNewArray);

laravel 5.6 image intervention upload with rename and resize

step 2 is not needed if you are working on laravel 5.6 composer require intervention/image will install in your vendor folder and laravel package discovery will do the rest for you

Re-size image using laravel 5.2

Try this:

$img = Image::make(public_path('ddd.jpg'))->resize(320, 240)->insert(public_path('watermark.jpg'));

Assuming the files ddd.jpg and watermark.jpg are in your public folder. public_path() is a helper function built in Laravel. See: https://laravel.com/docs/5.2/helpers#method-public-path.



Related Topics



Leave a reply



Submit