php图片处理Imagine常用例子

php 图片加载
创建时间:2021-11-20 15:57:15

目录

Imagine是一个php处理图片的库,自己项目里面用到又经常忘记一些变量名,官方文档打开还挺慢的,赶紧记下。

图片指定格式保存压缩

<?php $imagine = new Imagine\Imagick\Imagine(); $imagine->open('/path/to/image.jpg') ->save('/path/to/image.jpg', array('jpeg_quality' => 50)) // from 0 to 100 ->save('/path/to/image.png', array('png_compression_level' => 9)); // from 0 to 9 ->save('/path/to/image.webp', array('webp_quality' => 50)) // from 0 to 100

截图

<?php use Imagine\Image\Box; use Imagine\Image\Point; $image->resize(new Box(15, 25)) ->rotate(45) ->crop(new Point(0, 0), new Box(45, 45)) ->save('/path/to/new/image.jpg');

缩略图

$imagine = new Imagine\Gd\Imagine(); // or $imagine = new Imagine\Imagick\Imagine(); // or $imagine = new Imagine\Gmagick\Imagine(); $size = new Imagine\Image\Box(40, 40); // The original image is scaled so it is fully contained within the thumbnail dimensions (the image width/height ratio doesn't change). $mode = Imagine\Image\ImageInterface::THUMBNAIL_INSET; // or //The thumbnail is scaled so that its smallest side equals the length of the corresponding side in the original image (the width or the height are cropped). $mode = Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND; $imagine->open('/path/to/large_image.jpg') ->thumbnail($size, $mode) ->save('/path/to/thumbnail.png')

调整图像大小

<?php use Imagine\Image\Box; use Imagine\Image\Point; $image->resize(new Box(15, 25))

设置交错类型

$image = $imagine->open('/path/to/image.jpg'); $image->interlace(\Imagine\Image\ImageInterface::INTERLACE_LINE)->show('png');

图像引擎和各个模式的支持情况

引擎 INTERLACE_NONE INTERLACE_LINE INTERLACE_PLANE INTERLACE_PARTITION
GD Y Y N N
Gmagick Y N N N
Imagick Y Y Y Y
模式 INTERLACE_NONE INTERLACE_LINE INTERLACE_PLANE INTERLACE_PARTITION
描述 基线模式 线交错扫描 平面交错扫描 分区交错扫描

interlace type

interlace type
the type of interlacing scheme: None, Line, Plane, or Partition. The default is None.
This option is used to specify the type of interlacing scheme for raw image formats such as RGB or YUV. None means do not interlace (RGBRGBRGBRGBRGBRGB…), Line uses scanline interlacing (RRR…GGG…BBB…RRR…GGG…BBB…), and Plane uses plane interlacing (RRRRRR…GGGGGG…BBBBBB…). Partition is like plane except the different planes are saved to individual files (e.g. image.R, image.G, and image.B).
Use Line, or Plane to create an interlaced GIF or progressive JPEG image.