Intervention Image not detecting image - php

I'm using Intervention Image in my laravel app.
I upload a base64 string image through an ajax request, and then I convert that into a regular image.
$this->validate($request, [
'avatar' => 'required',
]);
$image_file = $request->get('avatar');
list($type, $image_file) = explode(';', $image_file);
list(, $image_file) = explode(',', $image_file);
$avatar = base64_decode($image_file);
Now, this works fine. In fact, if I go to save the image, it opens up just fine on my local like any other image. Great.
Storage::disk('public')->put($avatarPath.'/avatars/avatar.png', $avatar);
The problem is that when I try to pass the image into intervention image, like so
$image = Image::make($avatar)->fit(150, 150);
it just returns an image with all the properties being null. If I save the image, it just saves as a 0KB image with no data.
This does not return any errors.
Why is this happening?

Related

Laravel : Upload 64base image instead of normal

I have an application using React and Laravel where I'm uploading images.
I'm using Laravel Image Intervention package.
When testing my API, the upload for a normal image works perfectly.
Using React, the user can select an image from his pc and crop it, then he sees the preview as a generated cropped 64base image.
I would want to upload the generated 64base image to my database instead of a normal one, how to do so ?
Fileupload controller
public function Store(Request $request)
{
$this->validate($request, [
'filename' => 'image|required|mimes:jpeg,png,jpg,gif,svg'
]);
$originalImage= $request->file('filename');
$thumbnailImage = Image::make($originalImage);
// Define upload path
$originalPath = public_path('/images/');
// Save Orginal Image
$thumbnailImage->save($originalPath.time().$originalImage->getClientOriginalName());
// Save In Database
$imagemodel= new Photo();
$imagemodel->photo_name=time().$originalImage->getClientOriginalName();
$imagemodel->save();
return back()->with('success', 'Image Upload successful');
}
This one should work
$folderPath = "images/";
$image_parts = explode(";base64,", $request->file('file'));
$image_type_aux = explode("image/", $image_parts[0]);
$image_type = $image_type_aux[1];
$image_base64 = base64_decode($image_parts[1]);
$file = $folderPath . uniqid() . '. '.$image_type;
// file_put_contents($file, $image_base64);
Storage::put($file, $image_base64);
Possible alternative:
$image = Image::make(base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $request->file('file'))))->stream();
Storage::put($file, $image, 'public');

Encoding format (tmp) is not supported for image resize laravel

i want to resize uploaded image and store in folder.then show in web.
i used enctype="multipart/form-data" on form in blade.php.
file successfully show in web without resize.
when try to resize image i got error
controller.php
public function dili(Request $request)
{
$di = new diligent;
$di->jobtype = $request->jobtype;
$di->jobC = $request->jobC;
$di->details = $request->details;
$image = $request->file('image');
$path = $image->getClientOriginalName();
$destinationPath = public_path('img');
Image::make($image)->resize(300, 100)->save($image);
$a = $image->move($destinationPath, $path);
$di->image = $path;
$di->save();
$de = diligent::all();
return view('admin')->with('dw', $de);
}
Error Message
Encoding format (tmp) is not supported.
1) use getRealPath() inside Image::make()
2) save image in particular path. try like this.
if($request->hasFile('image')) {
$image = $request->file('image');
$filename = $image->getClientOriginalName();
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(300, 100);
$image_resize->save(public_path('img/' .$filename));
}
Make sure you installed Image intervention library.
The Intervention image save() method requires a filename so it knows what file format (jpg, png, etc..) to save your image in.
The reason you are getting the error is it does not know what encoding to save the temporary image object (tmp) in.
Here is an example
->save('my-image.jpg', 90)
There is also a optional second parameter that controls the quality output. The above outputs at 90% quality.
http://image.intervention.io/api/save

Unable to create thumbnail while image is uploading in Laravel

I'm trying to create thumbnail of image while it is uploading. The problem is that the thumbnail isn't created at all. Also is not saved in database.
This is what I have added in my function
$image = $request->file('image');
if( $image && $image->isValid()){
$imagename = str_random(20).'.'.$image->getClientOriginalExtension();
$destinationPath = public_path('/uploads');
$thumb_img = Image::make($image->getRealPath())->resize(100, 100);
$thumb_img->save($destinationPath.'/'.$imagename,80);
$image->move($destinationPath, $imagename);
}
$item->image = $filename;
$item->image_thumb = $thumb_img;
It's saves only the original image both places - uploads dir and in database but nothing regarding the thumbnail.
I'm using Intervention package.
Nothing is saving because you override the same image twice. Look what you have:
First, you creating thumbnail and saves it into the /uploads
After this, you save the original into the same directory with same name e.g. overriding the thumb.
You just need to make different name for the thumbnail:
$thumb_img = Image::make($image)->resize(100, 100)->save($destinationPath.'/thumb_'.$imagename, 80);
Notice the prefix for the thumb thumb_...

Resizing an image with Intervention

Im trying to use Intervention with AWS S3, but the resize method is not working for me.
$img = Image::make($file)->rotate($rotate)->crop($width, $width, $x, $y);
$img->backup();
foreach(Config::get('img.image-sizes') as $_k => $_v){
$img->resize($_v['w'], $_v['h']);
$s3->queue($img, $name);
$img->reset();
}
The images upload fine to S3, but resize fails, I get all images the size of the original image
If I call save() on the image, the resize works, but I do not wish to save the image as I am uploading via S3, putting the $img as the body:
$this->commands[] = $this->s3->getCommand('PutObject', [
'Bucket' => env('AWS_BUCKET'),
'Key' => Config::get('img.image-path').$name,
'Body' => $img,
'ContentType' => $img->mime(),
'ACL' => 'public-read'
]);
To get this to work will I have to call save on each image first? If so is there a way to get this to play nice with S3, ideally I do not want to save them to my server first before sending them off to S3.
This is the code I used to get my uploaded images resized and saved to Amazon S3 using Laravel 5 and Intervention.
$imageFile = \Image::make($imageUpload)->resize(600, 600)->stream();
$imageFile = $imageFile->__toString();
$filename = 'myFileName.png';
$s3 = \Storage::disk('s3');
$s3_response = $s3->put('/'.$filename, $imageFile, 'public');

Blank Image - Image intervention Laravel

When I save a image using the Image Intervention class my file saves but shows as blank. I send it a base64 string.
Currently My Code.
$file = Input::file($_POST['data']);
$img = Image::make($file);
$fileName = "profiles/".md5(time()).'.png';
$path = public_path($fileName);
if($img->save($path)){
$user = User::find(Auth::user()->id);
$user->location->start_image = $fileName;
$user->push();
}
print_r($_POST['data']) -> http://pastebin.com/BGbUeZhr
Thanks
I have tried $file = Input::file(base64_decode($_POST['data'])); This does not work
Maybe I'm missing something but using Input::file when you have the image as base64 looks just wrong to me. Input::file is for retrieving a file that has been upload with an html form.
Assuming $_POST['data'] is a base64 encoded image, try this:
$img = Image::make($_POST['data']);
Or even better:
$img = Image::make(Input::get('data'));

Categories