|
|
Ok!
|
|
|
Ok!
|
|---|---|---|---|---|
| 96 | function resizeAndConvertImageWebP(
$width,
$height,
$density,
$originalFilepath,
$resizedFilepath) {
$newWidth = $width * $density;
$newHeight = $height * $density;
$image = new Imagick($originalFilepath);
$origImageDimens = $image->getImageGeometry();
$origImgWidth = $origImageDimens['width'];
$origImgHeight = $origImageDimens['height'];
if($newWidth == 0) {
$ratioOfHeight = $newHeight / $origImgHeight;
$newWidth = $origImgWidth * $ratioOfHeight;
}
if($newHeight == 0) {
$ratioOfWidth = $newWidth / $origImgWidth;
$newHeight = $origImgHeight * $ratioOfWidth;
}
$widthRatios = $origImgWidth / $newWidth;
$heightRatios = $origImgHeight / $newHeight;
if($widthRatios <= $heightRatios) {
$cropWidth = $origImgWidth;
$cropHeight = $newWidth * $widthRatios;
} else {
$cropWidth = $newHeight * $heightRatios;
$cropHeight = $origImgHeight;
}
$cropX = ($origImgWidth - $cropWidth) / 2;
$cropY = ($origImgHeight - $cropHeight) / 2;
$image->stripImage();
$image->cropImage($cropWidth, $cropHeight, $cropX, $cropY);
$image->resizeImage($newWidth, $newHeight, imagick::FILTER_LANCZOS, 0.9);
$image->setImageFormat('webp');
$image->setImageAlphaChannel(imagick::ALPHACHANNEL_ACTIVATE);
$image->setBackgroundColor(new ImagickPixel('transparent'));
$image->writeImage($resizedFilepath);
} | webp,support,imagemagic | 140 | Конвертация картинки в webp в случае, если imageMagic поддрживает |