Laravel 5.2: create image thumbnails in using intervention - php

I am trying to create image thumbnails using intervention here is my controller
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
Image::make($image)->resize(600, 390)->save(public_path('images/blog/' . $filename));
$image->fit(240, 157)->save(public_path('images/blog/' . $filename . '-thumbs.jpg'));
$add->image = $filename;
}
Got
Method fit does not exist.
What I am doing wrong here?

You have to update this line:
Image::make($image)->resize(600, 390)->save(public_path('images/blog/' . $filename));
to
$image = Image::make($image)->resize(600, 390)->save(public_path('images/blog/' . $filename));
because fit is a method of InterventionImage object.

Related

Image uploaded in Laravel 5.8 keep saving as private image

I am trying upload a single image into a Post and it keeps saving it as a private image--heck, I don't even know where to find that file.
PostController.php
...
if ($request->hasFile('photo')) {
$photo = $request->photo;
$ext = $photo->getClientOriginalExtension();
$filename = uniqid() . '.' . $ext;
$photo->storeAs('public/posts/' . $request->user()->id, $filename);
}
...
storeAs() function takes three parameters.
storeAs(path,name,options)
'options' is where you specify file visibility. In your case :
if ($request->hasFile('photo')) {
$photo = $request->photo;
$ext = $photo->getClientOriginalExtension();
$filename = uniqid() . '.' . $ext;
$photo->storeAs('posts/' . $request->user()->id, $filename,'public');
}

How do I use Image Intervention with Laravel Storage and digital ocean

I am using the Laravel storage feature to store images on DigitalOcean which works fine but I can't seem to add image intervention because I want to use the orientate feature. This is so that images created through the phone doesn't rotate sideways. How do I add image intervention to my existing code?
if ( $request->hasFile('image')) {
$extension = $request->file('image')->extension();
$mimeType = $request->file('image')->getMimeType();
$path = Storage::disk('do')->putFileAs('public/images', $request->file('image'), time() . '.' . $extension);
Storage::disk('do')->setVisibility($path, 'public');
$product->image = $path;
}
So i tried different ways of implementing image intervention haven't had much success but here my updated code. An with that i got a error saying Command (GetRealPath) is not available for driver (Gd).
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('/images/' . $filename);
$extension = $request->file('image')->extension();
//$mimeType = $request->file('image')->getMimeType();
$orientate = Image::make($image)->orientate();
$path = Storage::disk('do')->putFileAs('public/images', $orientate, $filename);
Storage::disk('do')->setVisibility($path, 'public');
$product->image = $path;

Laravel can't upload file in storage folder

I used to store my upload files (images) in my public folder but this time I want to store them in my storage folder and I get this error
Can't write image data to path (C:\laragon\www\mynewsite\storage\images/aboutimage-1522481830.png)
Error comes from this line (where I use intervention/image package)
Image::make($image)->resize(1200, 600)->save($location);
My function:
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = storage_path('images/' . $filename);
Image::make($image)->resize(1200, 600)->save($location);
$oldFilename = $indexabout->image;
$indexabout->image = $filename;
Storage::delete($oldFilename);
}
any idea?
UPDATE
My files will upload in root/storage folder instead of root/storage/app/public/images
why is that?
Update 2
I changed my function to code below and it's uploading where it suppose to upload but it does not delete the old image
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = '/aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = storage_path('app/public/images' . $filename);
Image::make($image)->resize(1200, 600)->save($location);
$oldFilename = $indexabout->image;
$indexabout->image = $filename;
Storage::delete($oldFilename);
}
SOLVED
here is final code:
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = storage_path('app/public/images/' . $filename); // root storage path
Image::make($image)->resize(1200, 600)->save($location);
Storage::delete('images/' . $indexabout->image); //public storage path
$indexabout->image = $filename;
}
Basically I gave Root/Storage path to store data and Public/Storage path for deleting them in update method.

Intervention \ Image \ Exception \ NotReadableException Unable to find file ()

I have this code to save my post images and it returns error of:
Intervention \ Image \ Exception \ NotReadableException
Unable to find file ().
My code:
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'food' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/');
Image::make($image)->resize(800, 400)->save($location);
$food->image = $filename;
}
I've got this code from Intervention \ Image \ Exception \ NotReadableException using laravel 4 but before that i had this code
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'food' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/');
$request->file('image')->move($location, $filename);
$food->image = $filename;
}
And it worked just fine, the reason I changed my code was to be able to resize images that's all.
Thanks.
You have to change the permissions on Windows/Temp file. Enable read permisson to the users group.
I have tested your code and fixed error. its working fine now.
$file = $request->file('image');
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'food' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/'. $filename);
Image::make($image->getRealPath())->resize(800, 400)->save($location);
}
In my case, i've managed to get it work by adding this entry in php.ini
upload_tmp_dir = "C:\Users\<NAME>\AppData\Local\Temp"
and reload the server
try this
$image = $request->file('image');
$location = public_path('images/');
$filename = $location. ''.'food' . '-' . time() . '.' . $image->getClientOriginalExtension();
Image::make($image->getRealPath())->resize('800','400')->save($filename);
UPDATE
$image = $request->file('image');
$location = public_path('images/');
Image::make($image)->resize('800','400')->save($location.$filename);
Also check this so post here it
$originalImage = 'images/1.jpg';
Image::make($originalImage);
I try many more, finally i got it.I find out if index.php file use in outside public folder then this problem show. Following this way to solve the problem.
$image = $request->file('image');
$input['imagename'] = hexdec(uniqid()).$image->getClientOriginalName();
$location = public_path("upload/image");
$imgs = Image::make($image->getPathname());
$imgs->resize(300 , 300, function ($constraint) { $constraint->aspectRatio(); })->save($location.'/'.$input['imagename']);
I try many more, finally I got it. Following this code I think solved your problem
$location = "assets/images/brand";`
$file = $request->image;
$filename =uniqid().time().'.'.$file->getClientOriginalExtension();
$image = Image::make(file_get_contents($file))->resize('600','600')->save($location.'/'.$filename);

Saving a remote image from facebook graph api

Any idea what's wrong?
I end up with 1bytes files and they are wrong. Using Intervention Image & PHP. Tried many ways to get it but nothing... If I just want to display it works but I can't save the picture...
$avatar_url = 'http://graph.facebook.com/' . $id . '/picture?type=square&width=140&height=140&redirect=false';
$avatar_pic_url = json_decode(file_get_contents($avatar_url), true)['data']['url'];
dd($avatar_pic_url);
$avatar = Image::make($avatar_pic_url);
$extension = 'jpg';
// save it
$destinationPath = 'uploads/avatars/';
$filename = uniqid(). '.' . $extension;
$path_to_temp_image = $avatar->dirname . '/' . $avatar->filename;
$key = $destinationPath . $filename;
// Upload avatar to remote storage
$uploadSuccess = Storage::put($key, $path_to_temp_image);

Categories