renderthat / php-pngquant
pngquant 的非官方 PHP 包装类,pngquant 是一个用于 PNG 图像无损压缩的命令行工具和库。
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-29 02:39:23 UTC
README
PHP-PNGQuant 是 PNGQuant 的非官方包装器,PNGQuant 是一个用于 PNG 图像无损压缩的命令行工具和库。
要求
您需要通过 PATH 访问 pngquant
。如果由于任何原因该工具不可用,您可以使用 setBinaryPath
方法更改二进制文件的路径
<?php use ourcodeworld\PNGQuant\PNGQuant; $instance = new PNGQuant(); // Change the path to the binary of pngquant, for example in windows would be (with an example path): $instance->setBinaryPath("C:\\Users\\sdkca\\Desktop\\pngquant.exe") // Other options of PNGQuant here ->execute();
安装
php-pngquant 可与或无需 Composer 使用。
使用 Composer
使用 php-pngquant 的首选方法是使用 Composer。执行以下命令将此软件包安装为项目的依赖项
composer require ourcodeworld/php-pngquant
不使用 Composer
如果您不使用 composer,您仍然可以使用包装器。从存储库下载 PNGQuant.php 类,然后在您的代码中使用 require_once
导入它
<?php require_once("PNGQuant.php"); $instance = new PNGQuant(); // Configuration here ...
示例
将文件直接保存到具有文件名的目录中。
// Include the class use ourcodeworld\PNGQuant\PNGQuant; $instance = new PNGQuant(); // Set the path to the image to compress $exit_code = $instance->setImage("/a-folder/image-original.png") // Set the output filepath ->setOutputImage("/a-folder/image-compressed.png") // Overwrite output file if exists, otherwise pngquant will generate output ... ->overwriteExistingFile() // As the quality in pngquant isn't fixed (it uses a range) // set the quality between 50 and 80 ->setQuality(50,80) // Execute the command ->execute(); // if exit code is equal to 0 then everything went right ! if(!$exit_code){ echo "Image succesfully compressed"; }else{ echo "Something went wrong (status code $exit_code) with description: ". $instance->getErrorTable()[(string) $exit_code]; }
如果您需要图像的原始数据(存储压缩图像的二进制数据),您可以使用 getRawOutput
函数
// Include the class use ourcodeworld\PNGQuant\PNGQuant; $instance = new PNGQuant(); // Set the path to the image to compress $result = $instance->setImage("/a-folder/image-original.png") // Overwrite output file if exists, otherwise pngquant will generate output ... ->overwriteExistingFile() // As the quality in pngquant isn't fixed (it uses a range) // set the quality between 50 and 80 ->setQuality(50,80) // Retrieve RAW data from pngquant ->getRawOutput(); $exit_code = $result["statusCode"]; // if exit code is equal to 0 then everything went right ! if($exit_code == 0){ $rawImage = imagecreatefromstring($result["imageData"]); // Example Save the PNG Image from the raw data into a file or do whatever you want. // imagepng($rawImage , 'newfile.png'); echo "Image succesfully compressed, do something with the raw Data"; }else{ echo "Something went wrong (status code $exit_code) with description: ". $instance->getErrorTable()[(string) $exit_code]; }
文档
有关包装器的更多信息及示例,请访问官方文档。
MIT 许可证 (MIT)
版权所有 © 2017 Our Code World
在此特此授予任何获得此软件及其相关文档文件(统称为“软件”)副本的个人免费使用软件的权利,不受任何限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,以及允许获得软件副本的个人这样做,前提是遵守以下条件
上述版权声明和本许可声明应包含在软件的任何副本或主要部分的副本中。
软件按“原样”提供,不提供任何形式的保证,明示或暗示,包括但不限于适销性、适用于特定目的和不侵犯版权的保证。在任何情况下,作者或版权所有者不应对任何索赔、损害或其他责任负责,无论这些责任是因合同、侵权或其他方式引起的,无论这些责任是否源于、因或与软件或软件的使用或其他交易有关。