naucon/image

此包为php提供简单的图像处理类,用于创建和修改GIF、PNG、JPEG图像。

1.0.0 2015-08-24 09:07 UTC

This package is auto-updated.

Last update: 2024-08-29 04:14:28 UTC


README

关于

此包为php提供简单的图像处理类,用于创建和修改GIF、PNG、JPEG图像。图像类是GD php扩展函数的面向对象实现。需要GD库2.0和GD php扩展。

功能

  • 创建图像
  • 加载图像数据
  • 打开图像文件
  • 返回图像宽度和高度
  • 缩放图像
  • 滤镜
    • 灰度
    • 亮度
    • 对比度
    • 伽玛
    • 着色
    • 负片
    • 轮廓
    • 浮雕
    • 模糊(高斯)
    • 锐化
    • 草图
    • 平滑
    • 像素化
    • alpha混合
    • 反锯齿

兼容性

  • PHP5.3
  • GDlib 2.0
  • ext-GD

安装

使用composer安装最新版本

composer require naucon/image

基本用法

Image类是GD php扩展函数的面向对象实现。图像(GIF、PNG、JPEG)可以被创建、加载或打开。这些图像可以被缩放、绘制或应用滤镜。

要使用Image类,请创建一个Image的实例。

use Naucon\Image\Image;
$image = new Image();

之后,可以创建、加载或打开图像。要创建图像,请调用create()并提供图像的尺寸。

// create image in 320x213
$imageWriter = $image->create(320,213);

要从二进制数据加载图像,请调用load($binary)

// or load image data
$imageData = file_get_contents(__DIR__ . '/example.png');   // from a string
$imageWriter = $image->load($imageData);

要打开现有的图像文件,请使用绝对文件路径调用open($path)

// or open image file
$imageWriter = $image->open(__DIR__ . '/example.png');

方法create()load()open()返回一个ImageWriter实例。实际图像处理是通过这个实例完成的。从现在开始,我们将使用这个实例。

要获取图像的宽度和高度,请调用getWidth()getHeight()

echo 'width: ' . $imageWriter->getWidth();  // width: 320
echo '<br/>';
echo 'height: ' . $imageWriter->getHeight();    // height: 213

要缩放图像,请使用宽度和高度调用scale()。图像将被均匀缩放。

$imageWriter->scale(100,100);        // scale image (equally)

可以在图像上应用一个或多个滤镜。

$imageWriter
    ->transparentColor(0,0,0)
    ->negative()        // invert colors
    ->grayscale()       // grayscale colors
    ->brightness(100)   // brightness -255 to 255
    ->contrast(50)      // contras -100 to 100
    ->gamma(1.0, 2.0)   // gamma correction
    ->colorize(0,255,0) // colorize to green
    ->outline()         // highlight the edges
    ->emboss()          // emboss image
    ->blur()            // blur image
    ->blurGaussian()    // blur image with gaussian method
    ->sharpen()         // sharpen image
    ->sketchy()         // sketchy effect
    ->smooth(8)         // smooth image
    ->pixelation(3)     // pixelation effect
    ->alphaBlending(true)   // enable alpha blending mode
    ->antialiase(true)      // enable antialiase
    ->scale(100,100);        // scale image (equally)

处理后的图像可以保存到文件...

// scale and save file
$imageWriter
    ->transparentColor(0,0,0)
    ->scale(100,100)        // scale image (equally)
    ->save(__DIR__ . '/tmp/new_image.png');     // save image as file

...或输出到输出缓冲区。因此,必须指定图像格式。

// scale and dump image
$imageWriter
    ->transparentColor(0,0,0)
    ->scale(100,100)        // scale image (equally)
    ->dump('gif');     // dump to output buffer

ImageWriter实例被销毁时,图像资源将被关闭。

unset($imageWriter);        // close image resource

许可

MIT许可(MIT)

版权所有(c)2015 Sven Sanzenbacher

特此授予任何获得本软件及其相关文档副本(以下简称“软件”)的人免费使用软件的权利,不受任何限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,并允许将软件提供给有权获得软件的人,以便他们可以这样做,前提是遵守以下条件

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

本软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、针对特定目的的适用性和非侵权性保证。在任何情况下,作者或版权所有者都不对任何索赔、损害或其他责任负责,无论是基于合同、侵权或其他方式,源于、因、与软件或其使用或其他交易有关。