digitalzenworks/php-image-optimizer

优化和调整图像大小的响应式图像工作流

1.5.16 2023-06-03 10:53 UTC

README

优化和调整图像大小的响应式图像工作流。详细信息可在以下优秀文章中找到:使用 ImageMagick 进行高效图像调整大小

最初基于(分支)https://github.com/nwtn/php-respimg

要求/依赖项

安装

Git

git clone https://github.com/jamesjohnmcguire/PhpImageOptimizer

Composer

composer require https://packagist.org.cn/packages/digitalzenworks/php-image-optimizer

示例

调整单个位图图像大小,不进行优化

$image = new DigitalZenWorks\ImageOptimizer($input_filename);
$image->smartResize($output_width, $output_height, false);
$image->writeImage($output_filename);

调整单个位图图像大小并保持宽高比,不进行优化

$image = new DigitalZenWorks\ImageOptimizer($input_filename);
$image->smartResize($output_width, 0, false);
$image->writeImage($output_filename);

调整单个位图图像大小并保持宽高比,进行优化

$image = new DigitalZenWorks\ImageOptimizer($input_filename);
$image->smartResize($output_width, 0, true);
$image->writeImage($output_filename);
nwtn\Respimg::optimize($output_filename, 0, 1, 1, 1);

调整目录中的多个位图图像大小并保持宽高比,进行优化

$exts = array('jpeg', 'jpg', 'png');
if ($dir = opendir($input_path)) {
	while (($file = readdir($dir)) !== false) {
		$base = pathinfo($file, PATHINFO_BASENAME);
		$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
		if (in_array($ext, $exts)) {
			$image = new DigitalZenWorks\ImageOptimizer($input_path . '/' . $file);
			$image->smartResize($width, 0, true);
			$image->writeImage($output_path . '/' . $base . '-w' . $w . '.' . $ext);
		}
	}
}
DigitalZenWorks\ImageOptimizer::optimize($output_path, 0, 1, 1, 1);

调整目录中的多个位图图像和 SVG 图像并保持宽高比,进行优化

$exts = array('jpeg', 'jpg', 'png');
if ($dir = opendir($input_path)) {
	while (($file = readdir($dir)) !== false) {
		$base = pathinfo($file, PATHINFO_BASENAME);
		$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
		if (in_array($ext, $exts)) {
			$image = new DigitalZenWorks\ImageOptimizer($input_path . '/' . $file);
			$image->smartResize($width, 0, true);
			$image->writeImage($output_path . '/' . $base . '-w' . $w . '.' . $ext);
		} elseif ($ext === 'svg') {
			copy($input_path . '/' . $file, $output_path . '/' . $file);
			DigitalZenWorks\ImageOptimizer::rasterize($input_path . '/' . $file, $output_path . '/', $width, 0);
		}
	}
}
DigitalZenWorks\ImageOptimizer::optimize($output_path, 3, 1, 1, 1);

贡献

如果您有改进此项目的建议,请分支仓库并创建一个拉取请求。您也可以简单地创建一个带有“增强”标签的问题。别忘了为项目加星!再次感谢!

  1. 分支项目
  2. 创建您的功能分支(git checkout -b feature/AmazingFeature
  3. 提交您的更改(git commit -m 'Add some AmazingFeature'
  4. 将更改推送到分支(git push origin feature/AmazingFeature
  5. 打开拉取请求

许可证

在 MIT 许可证下分发。有关更多信息,请参阅 LICENSE

联系方式

James John McGuire - @jamesmc - jamesjohnmcguire@gmail.com

项目链接:https://github.com/jamesjohnmcguire/PhpImageOptimizer