定制 rosell-dk/webp-convert-and-serve 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

rosell-dk/webp-convert-and-serve

Composer 安装命令:

composer require rosell-dk/webp-convert-and-serve

包简介

Convert and serve jpeg/png to webp with PHP (if at all possible)

README 文档

README

This library is now part of WebP Convert and thus obsolete!

Build Status

This library can be used for converting and serving WebP images instead of jpeg/png. It is based on WebPConvert, which takes care of the conversion. On top of that, it adds a method for serving the converted image with options on how to handle conversion failures.

The task of serving is in itself actually quite small. It can be implemented in a few lines, like this:

try {
    $success = WebPConvert::convert($source, $destination, $options);
    if ($success) {
        header('Content-type: image/webp');
        readfile($destination);        
    }
}

But then comes error handling.

If conversion fails, it will make sense to serve the source image instead (if that exists, that is). For that, we need to inspect the extension in order to provide the correct Content-type header. Also, we want to add headers that tells the browser not to cache it. And what if the source file isn't even available? This should be handled as well. Tedious stuff, that this library takes care of.

API

WebPConvertAndServe::convertAndServe($source, $destination, $options)

Note: This method was added in 0.4.0. The old one, convertAndServeImage() still works, but is depreciated.

Parameter Type Description
$source String Absolute path to source image (only forward slashes allowed)
$destination String Absolute path to converted image (only forward slashes allowed)
$options Array Array of conversion options. See below

The $options argument

The options argument is a named array. WebPConvertAndServe has just two available options (fail and critical-fail). However, the options will be handed over to WebPConvert. So Any option available in webp-convert are available here.

fail

Indicate what to serve, in case of normal conversion failure. Default value: "original"

Possible values Meaning
"original" Serve the original image.
"404" Serve 404 status (not found)
"report-as-image" Serve an image with text explaining the problem
"report" Serve a textual report explaining the problem

Instead of the string values (ie "original"), you can also use the following constants: WebPConvertAndServe::$ORIGINAL, WebPConvertAndServe::$HTTP_404, WebPConvertAndServe::$REPORT_AS_IMAGE and WebPConvertAndServe::$REPORT

critical-fail

Possible values: Same as above, except that "original" is not an option. Default value: "404"

Return value

Number indicating what was served. On fail or critical fail, the value will be one of the following constants: following constants WebPConvertAndServe::$ORIGINAL, WebPConvertAndServe::$HTTP_404, WebPConvertAndServe::$REPORT_AS_IMAGE and WebPConvertAndServe::$REPORT. On success, it will be WebPConvertAndServe::$CONVERTED_IMAGE. All fail constants are negative. The success constant is positive – so you can test success with a if ($returnValue > 0)

Example:

require 'vendor/autoload.php';

use WebPConvertAndServe\WebPConvertAndServe;

$source = __DIR__ . '/logo.jpg';
$destination = $source . '.webp';
$options = [
    'fail' => 'original',
    'critical-fail' => '404',

    // You can specify any WebPConvert option here - such as defining a converters array, which
    // is needed, if you need to use a cloud converter
    'converters' => [
        [
            'converter' => 'ewww',
            'options' => [
                'key' => 'blah',
            ],
        ],
        'cwebp',
        'gd'
    ];

$status = WebPConvertAndServe::convertAndServe($source, $destination, $options);

Installing

composer require rosell-dk/webp-convert-and-serve

rosell-dk/webp-convert-and-serve 适用场景与选型建议

rosell-dk/webp-convert-and-serve 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 901 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 04 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「images」 「gd」 「imagick」 「png」 「jpg」 「Webp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 rosell-dk/webp-convert-and-serve 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 901
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 8
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-04-18