Converting uploaded image to grey scale and downloading it - laravel

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));

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)

How to keep old image if a new one isn't uploaded on update. Laravel

I am hoping someone here will be able to help with this;
When a post is being updated in my project, I'd like for the image file that was uploaded during the initial post creation time, to be the same, if I don't want to upload a new one during an update.
**Also, if I do upload a new one during the update, I'd like for it to replace the old one. So, the old one gets deleted.
Here's what I currently have for the initial creation of posts;
if ($request->hasFile('post_avatar')){
$postimg = $request->file('post_avatar');
$postImgName = Str::slug($request->post_title) . '.' . $postimg->getClientOriginalExtension();
$destinationPath = public_path('/postImages');
$imagePath = $destinationPath. "/". $postImgName;
$postimg->move($destinationPath, $postImgName);
$post->post_avatar = $postImgName;
}
Thanks in advance!
I am assuming that you keep the name saved for the old image,
You can do like this,
$post_image = Str::slug($post->post_title); // taking from your old Post model instance lets say $post = Post::find(%id);
if ($request->hasFile('post_avatar')){
$image_path = public_path("/postImages/".$post_image);
if (File::exists($image_path)) {
File::delete($image_path);
}
$postimg = $request->file('post_avatar');
$postImgName = Str::slug($request->post_title) . '.' . $postimg->getClientOriginalExtension();
$destinationPath = public_path('/postImages');
$imagePath = $destinationPath. "/". $postImgName;
$postimg->move($destinationPath, $postImgName);
$post->post_avatar = $postImgName;
} else{
$post->post_avatar = $post_image;
}
$post->save();
Edit :
$post_image = Str::slug($post->post_title);
to
$post_image = $post->post_title;
Alright, below is the workflow for achieving what you have asked in your question. While, I'm not providing all the code, I believe this will be sufficient enough to guide you.
public function update(Request $request, $id)
{
$post = Post::findOrFail($id);
if ($request->hasFile('post_avatar')) {
$postimg = $request->file('post_avatar');
if ($post->post_avatar) {
// delete old image
// save new image
} else {
// save new image
}
}
// save post and redirect
}

Convert image to webp after adding watermark on image

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??

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.

Laravel 5 image resize

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

Resources