3/30/2024 0 Comments Imagemagick convert webp to gif![]() This defines the final image format to WebP and then ensures that if you are converting a PNG, it keeps any alpha channels. The important lines of code for WebP conversion are: $image->setImageFormat('webp') $image->setBackgroundColor(new ImagickPixel('transparent')) $image->setImageAlphaChannel(imagick::ALPHACHANNEL_ACTIVATE) $image->resizeImage($newWidth, $newHeight, imagick::FILTER_LANCZOS, 0.9) $image->cropImage($cropWidth, $cropHeight, $cropX, $cropY) $heightRatios = $origImgHeight / $newHeight $widthRatios = $origImgWidth / $newWidth $newHeight = $origImgHeight * $ratioOfWidth ![]() $ratioOfWidth = $newWidth / $origImgWidth $newWidth = $origImgWidth * $ratioOfHeight $ratioOfHeight = $newHeight / $origImgHeight $origImageDimens = $image->getImageGeometry() This means I can decide what size image I send to the browser.įor that reason, I got ImageMagick to do a couple of extra things, other than just convert, it strips the images metadata, crops it and then resizes it. One thing I do on my site is format the file names in the URL in such a way that it includes the desired image dimensions as well as the density of the screen. WebPageTest Results After WebP How Do You Convert to WebP? You can see the difference in the results below. On the home page the image went from 141.5 KB to 66.8 KB and that results in a load time from 959 ms to 393 ms. I ran everything through and WebP has a positive impact on my site (surprise, surprise). ![]() What I ended up doing to get around this was run the following: sudo apt-get -f install Errors were encountered while processing: This all seemed to work, although I did get some weird/scary looking error messages after the final command. Sudo apt-get install graphicsmagick-imagemagick-compat Sudo apt-get install libwebp-dev devscripts Then it was a case of rebuilding ImageMagick. To support WebP conversion in ImageMagick I had to install libwebp-dev with: sudo apt-get install libwebp-dev When I get a newer version of ImageMagick or libwebp I will update everything and see if enabling it works or not. The code is sat on my server, but disabled by a boolean. You can check yours with: sudo aptitude versions libwebp-dev Debian (and possibly) Ubuntu ships with 0.1.3. The problem here, is that to compile newer versions of ImageMagick you need to have version 0.3.0 or higher of libwebp. This may be an option for you using the information on this ImageMagick post. The way to get around the problem is to install a version higher than or equal to 6.8.3-0 Beta. It turns out you can use ImageMagick if it can find libwebp-dev when it’s built.īy default, unfortunately, Ubuntu doesn’t come with ImageMagick built with WebP support and I’m assuming the same is true for Debian (I got everything working on Ubuntu and went through the same process on Debian - didn’t check Debian needed this, I just assumed).Īnyway, I stumbled on this post which outlines how you can build webp support into ImageMagick.Īfter landing WebP I ended up hitting a problem later on where the alpha channel was messed up somewhere in the WebP conversion and displayed a black background - Booooo. ![]() I’ve been wanting to get WebP set-up on my site for a little while now, the biggest barrier to entry has been getting it integrated into the flow already had, where I generate different sized images where I need to.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |