Skip to content

Commit

Permalink
use webp for image resizer format
Browse files Browse the repository at this point in the history
  • Loading branch information
brookgagnon committed Aug 11, 2024
1 parent 849e70e commit c741635
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions classes/core/obfhelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,23 +187,18 @@ public static function image_resize($src, $dst, $width, $height)
}

$im = new Imagick();
$imgdata = file_get_contents($src);
$im->readImageBlob($imgdata);

$source_width = $im->getImageWidth();
$source_height = $im->getImageHeight();
$source_ratio = $source_width / $source_height;
$ratio = $width / $height;

if ($ratio > $source_ratio) {
$width = $height * $source_ratio;
} else {
$height = $width / $source_ratio;
}

$im->setImageFormat("jpeg");
$im->adaptiveResizeImage($width, $height);

$im->readImage($src);
$im->setImageBackgroundColor('white'); // Set white background if necessary
$im->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE); // Remove alpha channel
$im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN); // Flatten image
$im->thumbnailImage($width, $height, true); // Adjust width and height as necessary (maintain aspect ratio)

// Set the image format and apply lossy compression
$im->setImageFormat('webp');
$im->setOption('webp:lossless', 'true'); // For lossless
$im->setOption('webp:quality', '85'); // Set quality (0-100) for lossy compression

// write image
$im->writeImage($dst);
$im->clear();
$im->destroy();
Expand Down

0 comments on commit c741635

Please sign in to comment.