mart-ez 惻

May 1st, 2017 06:18 PM

Hello DevDojo community! Iā€™m currently trying to create a Laravel 5.4 image edit and upload app using Intervention Image. My question is, how to add Intervention Image filters to select box and apply filter based on selection?

devdojo 惻

May 4th, 2017 11:56 AM

Hey @Mart-ez :)

Hope you are doing well. You could always add the filter name to your select dropdown. So, in the select dropdown you would do something like the following:

<select name="filter">
    <option value="awesome">Awesome Filter</option>
		<option value="cool">Cool Filter</option>
</select>

Then, inside of you controller I might even do a switch statement like so:

public function applyFilter(Request $request){
		
		$img = Image::make('foo.jpg');
		
    switch($request->filter){
		
		    case 'awesome':
				    $img->filter(new AwesomeFilter(45));
            break;
						
			 case 'cool':
				    $img->filter(new CoolFilter(45));
            break;
						
				default:
				    break;
						
		}
		
		$img->save();
}

You can learn more about applying filters here on the Intervention documentation: http://image.intervention.io/api/filter

Hope that helps :)

Thanks.

devdojo 惻

May 4th, 2017 11:57 AM

Sorry the formatting might have gotten a little messed up in the last code block.

mart-ez 惻

May 5th, 2017 04:08 PM

It worked brilliantly. Thanks for your help :)