skip to Main Content

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.

3

Answers


  1. Chosen as BEST ANSWER

    I achieved it using this code:

    $image_destination_path = 'images/';
    $employee_image = date('YmdHis') . "." . $image->getClientOriginalExtension();
    $image->move($image_destination_path, $employee_image);
    $img = Image::make($image_destination_path . $employee_image)->resize(300, 300);
    
    // save file as jpg with medium quality
    $deleteImage = unlink(public_path(). '/images/' . $employee_image);
    $img->encode('jpg')->save('images/'. date('YmdHis') . '.jpg', 80);
    

  2. 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);
    
    Login or Signup to reply.
  3. 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);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search