1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- class ConversorWep {
- public static function generaImagenWebp($file, $compression_quality = 80) {
- $temp = explode('.', $file);
- $output_file = 'imgConvert/convertidas/' . $temp[0] . '.webp';
- $file = 'imgConvert/originales/' . $file;
- // check if file exists
- if (!file_exists($file)) {
- return false;
- }
- // If output file already exists return path
- if (file_exists($output_file)) {
- return $output_file;
- }
- $file_type = strtolower(pathinfo($file, PATHINFO_EXTENSION));
- if (function_exists('imagewebp')) {
- switch ($file_type) {
- case 'jpeg':
- case 'jpg':
- $image = imagecreatefromjpeg($file);
- break;
- case 'png':
- $image = imagecreatefrompng($file);
- imagepalettetotruecolor($image);
- imagealphablending($image, true);
- imagesavealpha($image, true);
- break;
- case 'gif':
- $image = imagecreatefromgif($file);
- break;
- default:
- return false;
- }
- // Save the image
- $result = imagewebp($image, $output_file, $compression_quality);
- if (false === $result) {
- return false;
- }
- // Free up memory
- imagedestroy($image);
- return $output_file;
- } elseif (class_exists('Imagick')) {
- $image = new Imagick();
- $image->readImage($file);
- if ($file_type === 'png') {
- $image->setImageFormat('webp');
- $image->setImageCompressionQuality($compression_quality);
- $image->setOption('webp:lossless', 'true');
- }
- $image->writeImage($output_file);
- return $output_file;
- }
- return false;
- }
- }
|