Home > database >  Convert image from png to jpg using PHP Intervention
Convert image from png to jpg using PHP Intervention

Time:01-16

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 here 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.

CodePudding user response:

Using pure intervention methods:

$png = 'folder1/image_src.png';
$jpg = 'folder2/image_dest.jpg';

$manager = new ImageManager();
$image = $manager->make($png);
$image->resize(300, 300);
$image->save($jpg);

CodePudding user response:

This can be done easily using the PHP GDImage class functions:

$png = 'folder1/image_src.png';
$jpg = 'folder2/image_dest.jpg';

$image = imagecreatefrompng($png);   //create image object from png
imagejpeg($image, $jpg, 100);        // save image as jpeg with Quality: 100
imagedestroy($image);
  • Related