megaoptim/megaoptim-php

PHP sdk,用于利用MegaOptim.com的图像优化API

v1.0.5 2019-11-23 15:40 UTC

This package is auto-updated.

Last update: 2024-09-24 02:21:53 UTC


README

MegaOptim提供了基于REST的API,用于优化图像,同时保持图像与原始图像几乎相同,但使用其先进的算法显著减小图像大小。

最终结果是满足页面速度要求、快速网站加载、减少空间使用等。

此库可以用composer安装,也可以不使用composer安装,并可用于不同的用例,例如优化您的库、优化上传的图像等。

要求

  • MegaOptim.com的API密钥

安装

Composer

您可以使用composer安装它

composer require megaoptim/megaoptim-php

不使用Composer

这不是推荐的方法。但是,您可以要求加载loadnoncomposer.php文件,这将加载库而不使用composer。

require "megaoptim-php/loadnoncomposer.php"

如何使用

此库有一个类将在99%的用例中使用,称为MegaOptim\Optimizer,而run方法是用于优化的方法。其定义如下

public function run($resource, $args = array())
  1. $resource - string|array 资源路径,可以是:单个图像路径 OR 单个URL OR 多个本地路径 OR 数组中的多个URL。 但不能是混合URL和路径的数组
  2. $args - array 描述优化如何进行的参数。它有几个属性
  • 'keep_exif' - 1 OR 0
  • 'max_width' - 数值px值
  • 'max_height' - 数值px值
  • 'cmyktorgb' - 1 OR 0
  • 'compression' - 智能型(默认),超强 OR 无损
  • 'webp' - 1 OR 0(生成或不生成webp)

$args参数不是必需的,默认值如下

[
	'keep_exif' => 0, 
	'max_width' => 0,
	'max_height' => 0,
	'cmyktorgb' => 1, 
	'compression' => 'intelligent',
        'webp'        => 1, 
] 

要优化单个图像,只需调用给定参数的run()方法,如下所示

单个本地文件

use MegaOptim\Optimizer;
$megaoptim = new Optimizer('your-api-key');
$response   = $megaoptim->run( '/path/to/file.jpg', array( 'compression' => Optimizer::COMPRESSION_INTELLIGENT ) );

单个URL

use MegaOptim\Optimizer;
$megaoptim = new Optimizer('your-api-key');
$response   = $megaoptim->run( 'http://yoursite.com/some_image.jpg', array( 'compression' => Optimizer::COMPRESSION_INTELLIGENT ) );

多个文件(最多5个)

use MegaOptim\Optimizer;
$megaoptim = new Optimizer('your-api-key');
$resources = array(
	'/path/to/file1.jpg',
	'/path/to/file2.jpg',
	'/path/to/file3.jpg',
);

$response   = $megaoptim->run( $resources, array( 'compression' => Optimizer::LOSSY ) );

多个URL(最多5个)

use MegaOptim\Optimizer;
$megaoptim = new Optimizer('your-api-key');
$resources = array(
	'http://somesite.com/path/to/file1.jpg',
	'http://somesite.com/path/to/file2.jpg',
	'http://somesite.com/path/to/file3.jpg',
);

$response   = $megaoptim->run( $resources, array( 'compression' => Optimizer::LOSSY ) );

处理响应

一旦我们使用run()方法运行了优化,我们就有MegaOptim\Http\Response实例,其中包含所有响应变量和优化图像对象的数组。

$response方法的内容如下

  • $response->isSuccessful() - 返回布尔值以确定优化是否成功。
  • $response->isError() - 返回布尔值以确定优化是否未成功。
  • $response->getErrors() - 返回包含错误的array
  • $response->getOptimizedFiles() - 返回来自MegaOptim\Http\Result[]类的优化图像对象的数组。

要获取优化图像对象并进一步处理它们,我们可以使用getOptimizedFiles()方法,该方法将返回上面提到的数组

$files = $response->getOptimizedFiles();
foreach($files as $file) {
 // Do something
}

对于每个优化图像对象,我们有以下属性和方法可供使用

  • $file->getFileName() - 返回原始文件名。
  • $file->getOptimizedSize() - 返回新大小(字节)。
  • $file->getSavedBytes() - 返回总共节省的字节。
  • $file->getSavedPercent() - 返回总共节省的空间(百分比)。
  • $file->getUrl() - 返回优化后的图像URL。您需要将其下载并存储在您的服务器上,因为它在1小时后将从MegaOptim服务器上删除
  • $file->getWebP() - 如果没有可用的webp版本或该文件的webp版本对应的ResultWebP实例,则返回NULL。

为了将优化后的图片本地保存,我们有三种方法可供选择:

  • $file->saveOverwrite() - 将新优化的文件覆盖本地文件。仅适用于正在优化的本地文件,不适用于URL。
  • $file->saveAsFile( $file_path ) - 将优化后的文件保存在指定的文件路径中。如果结果目录不存在,则会尝试递归创建它。
  • $file->saveToDir( $dir_path ) - 将优化后的文件保存在指定的目录路径中。如果结果目录不存在,则会尝试递归创建它。
if( !empty($files) ) {
	// Note: Thise are for demonstration purposes, you don't have to use it like this.
	// Overwrite
	$files[0]->saveOverwrite();
	// Or save as other file
	$files[0]->saveAsFile('/path/to/other/file.jpg');
	// Or save in some other dir with the original name
	$files[0]->saveToDir('/path/to/other');
}

待办事项

  • 在不同操作系统(如Windows)上测试库。

开发

如果您发现了错误或想为脚本做出贡献,请随时创建pull请求以使其更加完善!

许可证

Copyright (C) 2018 MegaOptim (https://megaoptim.com)

This file is part of MegaOptim Image Optimizer

MegaOptim Image Optimizer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

MegaOptim Image Optimizer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with MegaOptim Image Optimizer. If not, see <https://gnu.ac.cn/licenses/>.