Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ilya_user's avatar

Intervention image encode in Laravel

I want to encode image from png to jpg but for some reason it doesn't work. Although judging by the examples it should, what can be a problem? It does the resize part, but when it comes to changing the extension, it does nothing

$image_destination_path = 'images/';
$employee_image = date('YmdHis') . '.png';
$image->move($image_destination_path, $employee_image);
$img = Image::make('images/' . $employee_image)->resize(300, 300);

// save file as jpg with medium quality
$img->encode('jpg', 80)->save('images/'. $employee_image);

One person had the same problem and he solved it by changing this line:

$employee_image = date('YmdHis') . "." . $image->getClientOriginalExtension();

Like this:

$employee_image = date('YmdHis') . '.png';

But it did nothing for me.

0 likes
1 reply
tisuchi's avatar
tisuchi
Best Answer
Level 70

@ilya_user The issue you're experiencing is most likely caused by the fact that you're trying to save the image with the same filename but with a different extension. The save method overwrites the existing file, so if you're trying to save the image with the same filename, it will save the image with the same format, not the format you're trying to change it to.

One solution to this would be to change the filename when you're saving the image after encoding it. For example, you can change the filename to include the new extension.

$img->encode('jpg', 80)->save('images/'. date('YmdHis') . '.jpg');

Another solution is to remove the file before saving the new one with the correct format

unlink('images/'. $employee_image);
$img->encode('jpg', 80)->save('images/'. $employee_image);

Sidenote: Also, make sure that you have the permissions to write in the specified directory.

1 like

Please or to participate in this conversation.