Convert image to webp after adding watermark on image - laravel

I am currently working on a project on which I have to add watermark to image and then convert the image into webp format.I come up with two libraries to do so but they both require uploaded image as parameters, but after adding watermark the input for the webp library becomes object of Image Intervention.
This is my Laravel Controller Code
use Illuminate\Http\Request;
use File;
use Webp;
use Intervention\Image\ImageManagerStatic as Image;
public function store(Request $request)
{
foreach ($request->image as $img) {
$destinationPath = 'uploads/auction';
$watermark = 'uploads/auction/logo.png';
$originalFile = $img->getClientOriginalName();
$filename= uniqid().'-'.time().'-image.jpg';
$name = $destinationPath.'/'.$filename;
$img = Image::make($img);
$img = $img->insert($watermark, 'bottom-right', 1, 1);
$img->save($name);
WebP::make($img)->save($name);
}
}
Can anyone provide me a solution to achieve this??

Related

How to choose were Image Intervention should store the images?

Im trying to save a resized copy of an image that has been uploaded, but Image Interventions doesent seem to know in what root directory to begin in, or maybe it is just me that dosent know how to configure it propertly.
This is my code in AvatarImageController#store
public function store(Request $request, Game $game)
{
if ($request->hasFile('game_image')) {
$request->validate([
'game_image' => 'mimes:jpeg,png|max:1014'
]);
$file = $request->file('game_image');
$filename_thumbnail = "thumb_". $file->hashName();
$path_full = $request->file('game_image')->store('images/test_folder');
$path_thumb = $request->file('game_image')->storeAs('images/test_folder/thumbs', $filename_thumbnail);
// resize image
$path_thumb = Intervention::make("./storage/app/public/" . $path_thumb)->resize(300, 200);
$path_thumb->save();
$image = new Image;
$image->full = basename($path_full);
$image->thumb = $filename_thumbnail;
$game->images()->save($image);
return back()->with('success', "Success!! Image uploaded.");
}else{
return back()->with('success', 'Ooops.. something went wrong.');
}
abort(500, 'Could not upload image :(');
}
The error i receive:
Intervention\Image\Exception\NotReadableException
Image source not readable
I suspect this is because im not in the correct path, because of these lines from above function:
// resize image
$path_thumb = Intervention::make("./storage/app/public/" . $path_thumb)->resize(300, 200);
$path_thumb->save();
And it is a real hassel working with explicit paths when using git as deployment.
You can make image directly like this:
$uploadedFile = $request->file('game_image');
$image = Image::make($uploadedFile);
Resize it:
$image = $image->resize(300, 200);
and save it like this:
$image_data = $image->encode('jpg')->__toString();
$relative_file_path = 'images/test_folder/';
Storage::put($relative_file_path, $image_data)
use the storage() helper?
Intervention::make(storage_path('app/public/") . $path_thumb)

Storing an HTMLCanvasElement as a JPG in Laravel

I have integrated a cropping script that returns either an image or an HTMLCanvasElement in my Vue Component. In my component's crop method, I then make an axios post call to my upload function. Here is the crop method:
crop() {
var croppedCanvas = this.cropper.getCroppedCanvas()
var img = croppedCanvas.toDataURL('image/jpeg', 1.0);
var formData = new FormData();
formData.append('image', img) // I can send croppedCanvas here too if I want
axios.post('/api/upload', formData)
}
Now the problem. I can't seem to save the HTMLCanvasElement or the Image as a JPG file. I tried so many different approaches (toDataURL, toBlob, etc.) and have failed in every single one. My latest implementation was as follows:
public function upload(Request $request) {
$input = $request->all();
$img = $input['image'];
Storage::put('picture.jpg', $img);
return response(Response::HTTP_OK);
}
This saves a 26 byte file in the storage/public folder which is unaccessible.
Can someone please help with how to go about this.
I simply want to save the HTMLCanvasElement as a JPG file in my storage/public folder.
Thanks.
Actually as mentioned in the docs over here : https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL
toDataURL method returns back a base64 encoded data so you have to decode it on the server side
you can use the base64_decode method in laravel like this
public function upload(Request $request) {
$img = $request->image;
$img = str_replace('data:image/jpeg;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
Storage::put('picture.jpg', $data);
return response(Response::HTTP_OK);
}

Converting uploaded image to grey scale and downloading it

I have made an app where user adds two images from back end one is black & white and the other colored which definitely takes more space on the server now I am writing a function which can convert colored image to gray scale this way the load on the server will reduce now I am stuck in a situation when I use intervention library it always give error that file is empty can anyone help me resolve what I am doing wrong here is my code that I am currently using.
public function download(Request $request) {
$input = Input::all();
$sheet = Sheet::find($request->id);
if ($input['color-type'] == 'color') {
$file = public_path() . "/large/s/" . $sheet->sheet_f_id . '-s.jpg';
return Response::download($file);
} else {
$file = public_path() . "/large/s/" . $sheet->sheet_f_id . '-s.jpg';
$image = Image::make($file);
$grayScale = $image->greyscale();
return Response::download($grayScale);
}
}
What is it that I am doing wrong here.
You need to do
$image = Image::make(public_path($file));

Laravel - Convert Intervention Image Model(\Intervention\Image\Image) to UploadedFile model (\Illuminate\Http\UploadedFile)

The second parameter of the Storage::put method accept only \Illuminate\Http\UploadedFile(or File) model. How can i convert \Intervention\Image\Image to UploadedFile without save to storage?
if($request->hasFile('image')) {
$image = $request->file('image');
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(300, 300);
$image_resize->encode();
$imagePath = Storage::put(public_path(), new File($image_resize), 'public');
}
I think you will need to change your code to something like...
$image_resize->encode() to $image_resize->stream()->__toString(); as i think the encode() would be redundant.
Then Storage::put(public_path(), new File($image_resize), 'public') to Storage::disk('local')->put($image->getClientOriginalName(), $image->stream()->__toString(), 'public');
Note that the filename is being set by the original name... i'd recommend changing that to a unique name, if you need help with that i'd start at https://laracasts.com/discuss/channels/general-discussion/how-to-generate-long-unique-name-for-filename?page=1

Laravel intervention/image doesn't resize image

I am trying to use laravel intervention plugin. I installed it without problem but can't use it.
I am trying to make a test function which returns resized image, but without success;
I think the problem may be in image path, please help me fix my code.
function test($img)
{
/* $img = Image::make('public/image1.jpg');
$img->resize(300, 200);
return $img; */
$image = Image::make('http://localhost/cms/digital-cms/public/image1.jpg')->resize(200, 200, function ($c) {
$c->aspectRatio();
$c->upsize();
});
return $image;
//$h=200; $w=200;
//return Image::make(public_path('public/image1.jpg')->resize($h, $w)->response('jpg'));
}
//get image
$image=$request->file('image');
//rename image
$input = time().'.'.$image->getClientOriginalExtension();
//your directory to upload
$destinationPath = 'main/images/company';
//save and resize image
$img = Image::make($image->getRealPath());
$img->resize(20,20, function ($constraint) {
$constraint->aspectRatio();
})->save($destinationPath.'/'.$input);
You should use response function on Image class to return the image
$image = Image::make('http://localhost/cms/digital-cms/public/image1.jpg')->resize(200, 200, function ($c) {
$c->aspectRatio();
$c->upsize();
});
return $image->response();
In my case, I was resizing, cropping (fit) but the final image is still the same as original. Found out that I had to add function encode, to produce the manipulated image
return $image->encode('jpg', 80);

Resources