marcha/imagecow

PHP库,用于处理和生成响应式图片

v1.3.1 2015-04-30 08:48 UTC

This package is auto-updated.

Last update: 2024-09-20 05:11:50 UTC


README

Build Status Scrutinizer Code Quality

由Oscar Otero创建 http://oscarotero.com oom@oscarotero.com

什么是Imagecow?

它是一个用于将图像处理成Web的PHP库。

  • 使用PHP 5.3编写
  • 使用GD2或Imagick库(并可扩展更多)
  • 包含可选的客户端JavaScript以生成响应式图像
  • 非常简单易用。功能不多,只有基本操作:裁剪、调整大小、调整裁剪等。
  • 使用PSR-4自动加载标准

关于1.x版本的一些说明

1.x版本的API略有变化(不多,仅在对实例进行创建时)。

如何使用它?

使用静态函数Imagecow\Image::create()加载图像并返回一个imageCow实例。此函数有两个参数

  • image: 图像文件路径或包含图像数据的二进制字符串
  • library: 使用的库(Gd或Imagick)。如果不提供,则自动检测(首选顺序:Imagick,Gd)
use Imagecow\Image;

//Create an Imagick instance of "my-image.jpg" file:
$image = Image::create('my-image.jpg', 'Imagick');

//Create an instance detecting the library automatically
$image = Image::create('my-image.jpg');

//Create an instance from a binary file
$data = file_get_contents('my-image.jpg');

$image = Image::create($data);

//You can use also the direct functions:
$image = Image::createFromString($data);
$image = Image::createFromFile($file);

裁剪图像

//Arguments: ($width, $height, $x = 'center', $y = 'middle');

$image->crop(200, 300); //Crops the image to 200x300px
$image->crop(200, 300, 'left', 'top'); //Crops the image to 200x300px starting from left-top
$image->crop(200, 300, 20, '50%'); //Crops the image to 200x300px starting from 20px (x) / 50% (y)
$image->crop('50%', '50%'); //Crops the image to half size

调整图像大小

//Arguments: ($width, $height = 0, $enlarge = false)

$image->resize(200, 300); //Resizes the image to max size 200x300px (keeps the aspect ratio. If the image is lower, don't resize it)
$image->resize(800, 600, 1); //Resizes the image to max size 800x600px (keeps the aspect ratio. If the image is lower enlarge it)
$image->resize(800); //Resizes the image to 800px width and calculates the height maintaining the proportion.

调整大小和裁剪图像

//Arguments: ($width, $height, $x = 'center', $y = 'middle')

$image->resizeCrop(200, 300); //Resizes and crops the image to this size.

旋转

$image->rotate(90); //Rotates the image 90 degrees
$image->autoRotate(); //Rotates the image according its EXIF data.

将图像转换为其他格式

$image->format('png');

将图像保存到文件

$image->save('my-new-image.png');

//Overwrite the image (only if has been loaded from a file)
$image->save();

执行多个函数(调整大小、裁剪、调整裁剪、格式)

这在使用get变量动态转换图像时很有用:image.php?transform=resize,200,300|format,png

$image->transform('resize,200,300|format,png');

显示图像

$image->show();

其他函数

$image->getWidth();
$image->getHeight();
$image->getMimeType();

$image->getString(); //Returns the image in a string

$image->setBackground(array(255, 255, 255)); //Set a default background used in some transformations (for example, convert a transparent png to jpg)
$image->getExifData();
$image->setCompressionQuality(80); //Define the image compression quality for jpg images

响应式图像

在HTML页面中包含Imagecow.js库并执行函数Imagecow.init();

<script src="Imagecow.js" type="text/javascript" charset="utf-8"></script>

<script type="text/javascript">
	Imagecow.init();
</script>

此函数保存一个包含客户端信息(宽度、高度、连接速度)的cookie。您可以配置cookie。默认值是

Imagecow.cookie_seconds = 3600*24;
Imagecow.cookie_name = 'Imagecow_detection';
Imagecow.cookie_path = '/';

在服务器端,使用cookie生成响应式操作

use Imagecow\Image;

$operations = Image::getResponsiveOperations($_COOKIE['Imagecow_detection'], $_GET['transform']);

$image = Image::create();

$image->load($_GET['img'])->transform($operations)->show();

现在您可以根据客户端尺寸转换图像。可用选项包括

  • 最大宽度
  • 最小宽度
  • 最大高度
  • 最小高度
  • 宽度
  • 高度

您可以使用与transform相同的语法,但用";"分隔"媒体查询"。

img.php?img=my_picture.png&transform=resizeCrop,800,600;max-width=400:resize,400

获取图像"my_picture.png",使用调整裁剪到800x600。如果客户端的最大宽度为400,则调整到400。

在PHP框架中的使用

对于Laravel和PHP FuelPHP用户,您可以使用@kevbaldwyn提供的包装器:https://github.com/kevbaldwyn/image/

其他工具

IconExtractor。类从.ico文件中提取图像并将其转换为png。仅适用于Imagick

use Imagecow\Utils\IconExtractor;

$icon = new IconExtractor('favicon.ico');

//Gets the better image from the icon (quality = color_depth + (width * height))
$image = $icon->getBetterQuality();

//Do imagecow stuff
$image->resize(100)->save('my-image.png');

SvgExtractor。此类允许从svg文件生成图像(对于不支持svg格式的浏览器很有用)

use Imagecow\Utils\SvgExtractor;

$svg = new SvgExtractor('image.svg');

//Gets the image
$image = $svg->get();

//Now you can execute the imagecow methods:
$image->resize(200)->format('jpg')->save('image.jpg');

维护者

  • @oscarotero(创建者)
  • @eusonlito(贡献者)
  • @AndreasHeiberg(贡献者)
  • @kevbaldwyn(贡献者)