Skip to content

nishantshah977/compress-to-webp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

compress-to-webp

Compress your jpg, jpeg, png and convert into webp without losing quality ( depends upon level )in PHP.

  • Cup.jpg is original
  • test/filename_to_convert.webp is compressed
<?php
require('ImgProcess.php');

// Directory to store Processed Image
$dir = 'test';

// Name you want to store file with
$file = 'filename_to_save_with.webp';

$setting = array(
   'directory' => $dir, 
   'file_type' => array(
     'image/jpeg',
     'image/png',
     'image/gif'
   )
);

$ImgCompressor = new ImgCompressor($setting);
$jw = new ImageToWebp();

  /* Arguments: 'cup.jpg' - Original image path, 'jpg' - Output format, 5 - Compression level (0-9)
   */
  
$result = $ImgCompressor->run('cup.jpg', 'jpg', 5);

if ($result['status'] === 'success') {
    $compressedImageName = $result['data']['compressed']['name'];
    
    /* 
    Arguments: Compressed image path, Output WebP image path, Quality level (95 in this case) 
    */
    $jw->convert($dir . '/' . $compressedImageName, $dir.'/'.$file,95);
       unlink($dir.'/'.$compressedImageName);
       echo "Done";
} else {
    echo 'Error: ' . $result['message'];
}

?>

Thanks to

  1. PHP-Image-Compressor-Class
  2. PHP-ImageToWebp

Stay tuned for full readme.md or take reference from index.php comment

About

Compress your jpg, jpeg, png and convert into webp without losing quality ( depends upon level )in PHP.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages