msztorc/image-processor

ImageProcessor是一个用于GD2、ImageMagick和epeg等图形库的包装类。

1.2 2017-06-08 12:15 UTC

This package is auto-updated.

Last update: 2024-09-17 13:39:52 UTC


README

Build Status License

ImageProcessor类

ImageProcessor是一个用于GD2、ImageMagick和epeg等图形库的包装类。实现了常用的图形处理方法。

需求

  • GD2(PHP扩展)
  • ImageMagick(PECL库)
  • epeg(命令行工具)

支持对象库

  • GD2
  • ImageMagick(PECL库)

支持静态方法库

  • ImageMagick(一些功能,如调整大小、裁剪、效果)
  • epeg(命令行工具,仅适用于epeg_resize方法)

使用说明

对象

$obj = new ImageProcessor('imagick', 'file.jpg'); // or 'gd' (default)
$obj->resize(100, 50, true, false); //width, height, aspect_ratio, enlarge
$obj->clear(); //free memory

使用ImageMagick(PECL扩展)静态调整图像大小

ImageProcessor::imagick_resize('input-file.jpg', 'output-file.jpg', 800, 800); //infile, outfile, width, height, quality = 100, aspect_ratio = true, filter = imagick::FILTER_LANCZOS

使用epeg(命令行工具)静态调整图像大小

ImageProcessor::epeg_resize('input-file.jpg', 'output-file.jpg', 800, 800); //infile, outfile, width, height, quality = 100, aspect_ratio = true

加载文件

$obj = new ImageProcessor('imagick', 'infile.jpg');
// or
$obj2 = new ImageProcessor('gd', 'infile.jpg');

// clean constructor
$obj3 = new ImageProcessor(); //gd is default argument in constructor
$obj3->open('infile.jpg'); //open image

基本方法

灰度

$obj->grayscale();

负片

$obj->negative();

亮度

$obj->brightness($threshold); // +/- 100

着色

$obj->colorize(80,90,60); //rgb

棕褐色

$obj->sepia();

自定义棕褐色

$obj->grayscale();
$obj->colorize(90,60,40);

显示图像

$obj->display();

保存图像

$obj->save('outfile.jpg', 99); //filename, JPEG quality

您还可以对图像资源进行操作

$obj->image()->resizeImage(1200,1200, imagick::FILTER_LANCZOS, 1, true); //resize using Imagick object; object must be init with second argument 'imagick'
$obj = new ImageProcessor();
$obj->open_image('imagick', $file);
//$obj->image()->setOption('jpeg:size', '300x300'); //uncomment this if you want increase speed of resize
$obj->image()->resizeImage(300,300, imagick::FILTER_LANCZOS, 1, true);

$obj->save($outfile, 75);
$obj->display();

调整大小性能

ImageMagick

将JPEG图像从5906x5906调整为1181x1181的时间。

FILTER_POINT: 0.334532976151 sec
FILTER_BOX: 0.777871131897 sec
FILTER_TRIANGLE: 1.3695909977 sec
FILTER_HERMITE: 1.35866093636 sec
FILTER_HANNING: 4.88722896576 sec
FILTER_HAMMING: 4.88665103912 sec
FILTER_BLACKMAN: 4.89026689529 sec
FILTER_GAUSSIAN: 1.93553304672 sec
FILTER_QUADRATIC: 1.93322920799 sec
FILTER_CUBIC: 2.58396601677 sec
FILTER_CATROM: 2.58508896828 sec
FILTER_MITCHELL: 2.58368492126 sec
FILTER_LANCZOS: 3.74232912064 sec
FILTER_BESSEL: 4.03305602074 sec
FILTER_SINC: 4.90098690987 sec

CATROM的结果与LANCZOS非常相似,但速度明显更快。注意!以上结果仅供参考。执行时间取决于系统配置(处理器速度、内存大小等...)

您可以通过在打开文件之前设置jpeg:size选项,使用Imagick扩展类显著加快处理大文件的速度

$image = new Imagick();
$image->setOption('jpeg:size', '500x500');
$image->readImage('file.jpg');

或使用ImageProcessor类

//$time_start = microtime(true);

$obj = new ImageProcessor('imagick');
$obj->image()->setOption('jpeg:size', '300x300');
$obj->open($file);

$obj->image()->resizeImage(300,300, imagick::FILTER_LANCZOS, 1, true);

$obj->save($outfile, 99);

//$time_end = microtime(true);
//$time = $time_end - $time_start;
//echo $time ."\n\n";

链接

许可证

MIT