Laravel 5 image resize - laravel

I use Laravel 5 and have a form to upload a image. When saving the file I have in my controller methods to get the image and put it in a directory:
if ($request->hasFile('picture')) {
$destinationPath = 'uploads';
$filename = $image->getClientOriginalName();
$extension = $image->getClientOriginalExtension(); // add
$picture = sha1($filename . time()) . '.' . $extension; //add
$offer->image = $picture;
$image->move($destinationPath, $picture);
}
$offer->save();
Before I save the file I would like to resize the file with a max. width of 800px. Is there a image resize or compression function in Laravel available?
What would be the best idea to do this?

You can install and use package, similar to intervention.
Example from official website:
$img = Image::make('public/foo.jpg');
$img->resize(320, 240);
$img->insert('public/watermark.png');
$img->save('public/bar.jpg');

Image::make($request->file('image'))->resize(462, 462)->save('upload_path/filename.jpg'));
try this code

Related

intervention/image error when capture with mobile camera

When i uploaded image to my website from capture image from mobile camera no from the gallery
and it say " Image source not readable"
this is my code
$fileName = create_random_name() . '.' . $image->getClientOriginalExtension();
$img = Image::make($image->getRealPath());
$img->stream();
Storage::disk('local')->put("public/images/{$directory}/".$fileName,$img,'public');
return $fileName;
Maybe this code will help you:
Storage::disk('local')->put($newFilename, file_get_contents($file));

Image pixelate on resize browser( responsive design ) after upload on laravel

After upload the image, on resize browser, or enter the project on the mobile browser, quality of image is very weak, how can i improve that ?
if($request->hasFile('image')){
$image = $request->file('image');
$filename = $request->name;
$foldername = $request->name;
$imagename = $filename .'.' . $request->image->extension();
$path = public_path('images/produse/'. $filename .'/');
if(!File::exists($path)){
File::makeDirectory($path, 0777, true, true);
}
Image::make($image)->resize(200, 200)->save( public_path('images/produse/' . $foldername . '/' . $imagename ));
}
You should try with a larger resize resolution. For example (800, 600)

Laravel resize and upload to storage

I am trying to resize, rename and upload the image using laravel Storage, intervention on laravel5.
Here is my upload code:
if( $request->file('logo_image') ){
$logo_path = $request->file('logo_image')->store('university/'.Auth::User()->id);
if ( $logo_path ){
Storage::delete($university->logo_image);
}
$university->logo_image = $logo_path;
}
$university->update();
It is storing the image on folder like: storage/app/public/university/1/fjkdhjkfdh.jpg
Now I want to rename image name like university-name.jpg and also resize and upload to sam directory.
So I should have two images one is storage/app/public/university/1/university-name.jpg.jpg
and other is storage/app/public/university/1/thumbnail/university-name.jpg
I play around with this for the whole day but no success.
if(Input::file())
{
$image = Input::file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$path = public_path('profilepics/' . $filename);
Image::make($image->getRealPath())->resize(200, 200)->save($path);
$user->image = $filename;
$user->save();
}
Please someone can help me?
Thanks in advance.

How to convert base64 to image in Laravel 5.4?

I am developing api in Laravel 5.4. I will receive the image in base64 format. How can I convert the base64 to image in Laravel?
this solution will deal with all image types
$image = $request->input('image'); // image base64 encoded
preg_match("/data:image\/(.*?);/",$image,$image_extension); // extract the image extension
$image = preg_replace('/data:image\/(.*?);base64,/','',$image); // remove the type part
$image = str_replace(' ', '+', $image);
$imageName = 'image_' . time() . '.' . $image_extension[1]; //generating unique file name;
Storage::disk('public')->put($imageName,base64_decode($image));
I found the solution:
use Illuminate\Support\Facades\Storage;
function createImageFromBase64(Request $request)
{
$file_data = $request->input('image_file');
$file_name = 'image_' . time() . '.png'; //generating unique file name;
if ($file_data != "") { // storing image in storage/app/public Folder
Storage::disk('public')->put($file_name, base64_decode($file_data));
}
}
1- use package intervention image
use Illuminate\Support\Facades\Storage;
use Intervention\Image\ImageManagerStatic;
use Illuminate\Support\Str;
$logo = ImageManagerStatic::make($request->logo)->encode('png');
$logo_name = Str::random(40) . '.png';
Storage::disk('s3')->put('products_seller/' . $logo_name, $logo);

Image resize error using intervention in laravel 5

I am using intervention image to resize image and then save it to a destination folder. But its not working for me. I am getting error like, "Image source not readable".
Pls see the below code:
$image_name = $file->getClientOriginalName();
$thumbName = 'thumb_'. $image_name;
$destinationPath = public_path() . '/uploads/';
$thumbdestinationPath = public_path() . '/uploads/thumbnails/';
$imgUrl = URL::to('/').'/public/uploads/'.$image_name;
$thumbUrl = URL::to('/').'/public/uploads/thumbnails/'.$image_name;
$upload_success = $file->move( $destinationPath, $image_name);
if ($upload_success) {
Image::make($file->getRealPath())->fit('120','120')->save($thumbdestinationPath );
}
You need to give the name of image:
if ($upload_success) {
Image::make($file->getRealPath())->fit('120','120')->save($thumbdestinationPath . $image_name );
}

Resources