codebuds/webp-converter 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

codebuds/webp-converter

Composer 安装命令:

composer require codebuds/webp-converter

包简介

A service that uses php GD to convert Jpeg, Png, Gif and Bmp into WebP

README 文档

README

Code Coverage Badge

Webp Converter

I had already found a great webP conversion package rosell-dk/webp-convert that seems to work in many cases.

However, most of my projects will now be running on PHP7.4 and Symfony5, so I created a very small converter for those situations.

Usage

You can use composer to get this package :

composer require codebuds/webp-converter

Then, inside a Symfony controller for example you can use it like the following :

use CodeBuds\WebPConverter\WebPConverter;

class PageController extends AbstractController
{
    /**
     * @Route("/", name="home")
     * @return Response
     */
    public function index()
    {
        $path  = "/var/www/symfony/public/images/test.png";
        try {
            $webp = WebPConverter::createWebpImage($path, ['saveFile' => true, 'quality' => 10]);
        } catch(Exception $e) {
            // Do something with the exception
        }
        return $this->render('page/home.html.twig');
    }

The WebPConverter::createWebpImage static function needs the resource for the image.

This can either just be the path to the image, in which case the function itself will try to create the Symfony\Component\HttpFoundation\File\File from the path.

You can also directly pass the File element if you want :

public function index()
{
    // ...
    $file  = new File("/var/www/symfony/public/images/test.png");
    $webp = WebPConverter::createWebpImage($file);
    // ...
}

Exceptions

Multiple exceptions can be thrown. First the Symfony File is used for the guessExtension() function. This allows us to check the type of file provided and will make sure whether the file is called .JPG, .jpeg, .jpg, ... or something else, but it ends up being a jpeg file this is what will be used. If the file is not one of the allowed types (jpeg, png, gif and bmp) an exception will be thrown.

If the provided image is already a webP image the exception will send that information.

If the saveFile or the force options are not booleans, or the quality option is not an integer between 1 and 100 the information will be in an exception.

The filename, filenameSuffix and savePath options need to be strings.

If saveFile is set to true and force to the default false value, and the webP image already exists an exception will be thrown to let the user know force needs to be set to true is you want to override an existing file.

Finally, if the file is one of the allowed types but something goes wrong during the conversion made by GD that exception will be forwarded.

Return values

If no exceptions are present the return will consist of the webP image ressource and what the path would be for the image if you want it saved in the same directory as the original one.

Options

You can also set options but this is not required.

The saveFile option is false by default. As mentioned before this will only return the ressource, and the possible path for the webP image. This means you then need to run the gd function to save the image.

However, if you set this function to true the image will be saved automatically by triggering the gd imagewebp function.

$saveFile = $options['saveFile'] ??= false;
if($saveFile)
{
    imagewebp($imageRessource, $webPPath, $quality);
}

If you want to save the file directly there are more options to customize the webP file and it's location :

$path = '/var/www/symfony/public/images/a_file.jpg';

WebPConverter::createWebpImage(
    $path,
    [
    'saveFile' => true,
    'force' => true,
    'filename' => 'a_new_file',
    'filenameSuffix' => '_q50',
    'quality' => 50,
    'savePath' => '/var/www/symfony/public/webp'
    ]
);

This example will create the webP image /var/www/symfony/public/webp/a_new_file_q50.webp

Default values :

  • saveFile => false
  • force => false
  • quality => 80
  • filename => the same as the file that is going to be converted.
  • savePath => the same as the path of the file that is going to be converted.
  • filenameSuffix => an empty string

As that is possible, the second option is quality which by default is 80. This is only useful if the saveFile option is set to true (as the image is not created and saved otherwise).

codebuds/webp-converter 适用场景与选型建议

codebuds/webp-converter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18.26k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2020 年 05 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 codebuds/webp-converter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 codebuds/webp-converter 我们能提供哪些服务?
定制开发 / 二次开发

基于 codebuds/webp-converter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 18.26k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 33
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-05-09