yannickl88/image

用于读取、转换和写入图像文件的库。

2.1.1 2022-08-17 20:44 UTC

This package is auto-updated.

Last update: 2024-09-18 01:33:29 UTC


README

Latest Version Software License Build Status

用于读取、转换和写入图像文件的库。

这个库是为了满足对图像交互具有一致API的需求而诞生的。目标是提供简单的方法来执行常见的图像任务,如裁剪和缩放。

用法

支持的读取文件扩展名

  • PNG
  • JPG
  • JPEG
  • GIF

支持的写入文件扩展名

  • PNG
  • WEBP(如果已启用WebP支持的mod gd)

示例用法

$image = \Yannickl88\Image\Image::fromFile('/some/image.png');

// Resize to 50 x 50
$thumbnail = $image->resize(50, 50);
$thumbnail->save('/some/thumbnail.png');

// Fit the image to a width and height of 50, 50 while maintaining it's aspect ratio.
$thumbnail = $image->fit(50, 50);
$thumbnail->save('/some/thumbnail.png');

// Crop at 50, 50 with a square of 100 x 100
$thumbnail = $image->crop([50, 50, 100, 100]);
$thumbnail->save('/some/thumbnail.png');

// Resize and crop at the same time
$thumbnail = $image->sampleTo([50, 50, 100, 100], [0, 0, 50, 50]);
$thumbnail->save('/some/thumbnail.png');

// Set the quality, where 0 being low and 1 high (value between 0 and 1)
$compressed = $image->quality(0.25);
$compressed->save('/some/preview.png');

// Get image width
var_dump($image->width()); // int

// Get image height
var_dump($image->height()); // int

// Get image bounding box
var_dump($image->rect()); // array(0, 0, width, height)

// Get a color at a given coordinate
var_dump($image->color(0, 0)); // array(red, green, blue, alpha)

// Get the image orientation
var_dump($image->orientation()); // ImageInterface::ORIENTATION_LANDSCAPE

// Get the raw data and output it
header('Content-type: image/png');
header('Content-Disposition: filename="image.png"');
echo $image->data('.png');

迁移

参见迁移指南

安装