Search code examples
phplaravel-5grayscaleintervention

Converting images to greyscale using intervention


I am trying to convert my images into greyscale and then downloading it in Laravel but each and every time I am getting this error

The file "" does not exist

Dont' know why it giving this error here is my code.

$file = public_path() . "/large/s/" . $sheet[0]->sheet_f_id . '-s.jpg';
$image = Image::make($file);
$grayScale = $image->greyscale();
return Response::download($grayScale);

When I dump my $file variable I got the response something like this.

"D:\xampp\htdocs\wikistaging\public/large/s/03-02-05-025-s.jpg"

But still it is giving me the sam error why is that happening. Any help would be great.


Solution

  • If you want to download then first you have to save the created file and need to give the path to download. Here is working example

    $img_name=$sheet[0]->sheet_f_id . '-s.jpg';
    $destination_path=public_path() . "/large/s/";
    
    $file = $destination_path.$img_name;
    $image = Image::make($file);
    
    $image->greyscale()->save($destination_path.'gray-'.$img_name);
    return Response::download($destination_path.'gray_'.$img_name);
    

    And if you don't want to keep the file you can delete, replace the last line with below line.

    return Response::download($destination_path.'gray_'.$img_name)->deleteFileAfterSend(true);
    

    Hope it will work for you.