zoukankan      html  css  js  c++  java
  • php 图片处理 grafika 使用

    感谢,推荐看他们的,我的只是保存一份,害怕以后找不到

    https://learnku.com/articles/39118

    https://blog.csdn.net/qq_34657922/article/details/79479892

    Grafika 是一个 PHP 图像处理库,是基于 Imagick 和 GD,可以用于改变图片大小,剪裁,比较,添加水印等等功能。还有感知哈希,高级图像过滤,绘制贝塞尔曲线等功能,功能非常强大。优点:缩略图的速度非常快,质量非常高、支持智能剪裁、很好的支持 GIF 图片、5 种缩略图模式、图像对比功能、图像高级过滤功能、图像混合、其他图像处理库支持的 API 基本都支持

     

    一、 环境需求

    • PHP >= 5.3,当然官方推荐 php7
    • GD 库 >= 2.0 版本
    • Imagick 最好(不强求)>=3.3.0 , ImageMagick >= 6.5.3
     

    二、 安装及配置

    composer require kosinix/grafika:dev-master --prefer-dist
     

    三、常用 API 及用法#

     

    创建编辑器#

    • Create Editor 编辑器用于处理图像,官方建议使用 Grafika::createEditor() 创建 ,Grafika 会自动选择可用的最佳编辑器(将检查 Imagick 是否可用,如果没有就会使用 GD)
     // 导入扩展包
    use GrafikaGrafika;
    
    // 创建最佳可用编辑器
    $editor = Grafika::createEditor(); 
    • Imagick Editor() 当然也可直接使用 Imagick 类库
    use GrafikaImagickEditor;
    
    // 创建 Imagick 编辑器
    $editor = new Editor(); 

    注意:在使用 Imagick 编辑器时因为某些 PHP 安装默认情况下没有 Imagick 编辑器,需要添加一些安全检查:

    use GrafikaImagickEditor;
    
    $editor = new Editor(); 
    
    // 安全检查
    if( $editor->isAvailable() ) {
         //  ……
    }
    • GD Editor 也可直接使用 GD 库,也有些情况可能不支持,注意检查
    use GrafikaGdEditor;
    //  ……
    // 创建 GD 编辑器
    $editor = new Editor();
    
    // 安全检查
    if( $editor->isAvailable() ) {
       //  ……
    }
     

    创建图像,Grafika 允许使用 4 种方式创建一个待处理的图像#

    • 编辑器打开
    use GrafikaGrafika;
     //  ……
    $editor = Grafika::createEditor();
    $editor->open( $image, 'images/foo.jpg');
    • 使用静态方法打开图片
    use GrafikaGrafika;
    //  ……
    $editor = Grafika::createEditor();
    $image = Grafika::createImage('images/foo.jpg'); 
    • 创建一个空白的画布
    use GrafikaGrafika;
     //  ……
    $image = Grafika::createBlankImage(100,100);
    • 从已有图片拷贝一个副本
    $copy = clone $image;
     

    缩放#

    • resizeFit() 等比例缩放,调整图像大小以适应给定的宽度和高度,重新调整大小的图像不会超过给定的尺寸,如果要保留宽高比,则此选项非常有用
    resizeFit ( ImageInterface &$image, int $newWidth, int $newHeight ): EditorInterface 
    参数描述
    image 基础图片
    newWidth 新的宽度
    newWidth 新的高度
    use GrafikaGrafika;
    //  ……
    // 打开图像
    $editor->open( $image, "images/foo.jpg" );
    // 将图像调整大小
    $editor->resizeFit( $image, 200, 200 ); 
    // 保存
    $editor->save( $image, "images/testResizeFit.jpg"); 
    • resizeExact() 固定尺寸缩放,调整图像大小以精确尺寸,会忽略宽高比。
    resizeExact ( ImageInterface &$image, int $newWidth, int $newHeight ): EditorInterface 
    参数描述
    image 基础图片
    newWidth 新的宽度
    newWidth 新的高度
    use GrafikaGrafika;
    //  ……
    $editor->open( $image, "images/foo.jpg" );
    $editor->resizeExact( $image, 200, 200 );
    $editor->save( $image, "images/testResizeExact.jpg");

    resizeExactWidth() 等宽缩放,高度自动计算
    resizeExactHeight() 等高缩放,宽度自动计算

    • resizeFill() 缩放裁剪,调整图像大小以填充给定维度中的所有空间,多余的部分被裁切。
    resizeFill ( ImageInterface &$image, int $newWidth, int $newHeight ): EditorInterface
    参数描述
    image 基础图片
    newWidth 新的宽度
    newWidth 新的高度
    use GrafikaGrafika;
    //  ……
    $editor->open( $image, "images/foo.jpg" );
    $editor->resizeFill( $image, 200, 200 );
    $editor->save( $image, "images/testResizeFill.jpg");
    $editor = Grafika::createEditor();
    $editor->open( $image, "images/foo.png" );
    $editor->resizeFit($image, 200, 200);  // 等比缩放
    //$editor->resizeExact($image, 200, 200);  // 固定尺寸缩放
    //$editor->resizeFill($image, 200, 200);  // 缩放裁剪
    header('Content-type: image/png');
    $image->blob('PNG');
    • compare() 图片相似度对比,返回值越接近于 0 表示图片越相似,如果数字在 0-10 范围内那么图片都可能相似,果数字大于 10 那么可能就完全不同。
    compare(string|ImageInterface $image1, string|ImageInterface $image2): int 
    参数描述
    image1 图片 1
    image2 图片 2
    use GrafikaGrafika;
    //  ……
    $editor = Grafika::createEditor();
    $result = $editor->compare('images/foo-gray.png' , 'images/foo.png');
    • blend() 图片合并,将两张图像合并,第一张图像作为基础,第二张图像位于基础之上,支持多种混合模式。
    blend(ImageInterface &$image1, ImageInterface $image2, string $type = 'normal', float $opacity = 1, string $position = 'top-left', int $offsetX = 0, int $offsetY = 0): EditorInterface 
    参数描述
    image1 基础图片
    image2 放置在基础图片之上的图片
    type 图片叠加模式,值为:normal、multiply,、overlay 、 screen
    opacity 图片 2 的不透明度,取值范围为 0.0 到 1,默认为 1
    position 图片 2 在图片 1 上的位置,值为:top-left、op-center、top-right、center-left、 center、 center-right、 bottom-left、bottom-center、bottom-right 、 smart,默认为 top-left,smart 表示 grafika 来判断摆放在哪里好
    offsetX 图片 2 距离图片 1 左边的距离
    offsetY 图片 2 距离图片 1 上边的距离
    use GrafikaGrafika;
    //  ……
    $editor = Grafika::createEditor();
    $editor->open($image1 , 'images/foo-h.jpg');
    $editor->open($image2 , 'images/foo.jpg');
    $editor->blend ( $image1, $image2 , 'normal', 0.9, 'center');
    $editor->save($image1,'images/foo-blend.jpg');
    • rotate() 图像旋转
    rotate(ImageInterface &$image, int $angle, Color|null $color = null): EditorInterface 
    参数描述
    image 基础图片
    angle 角度
    color 背景色
    use GrafikaGrafika;
    use GrafikaColor;
     //  ……
    $editor = Grafika::createEditor();
    $editor->open($image , 'images/foo.png');
    $editor->rotate($image ,'45',new Color('#ff0000'));
    header('Content-type: image/png');
    $image->blob('PNG');
    • text() 图片写文字
    text (ImageInterface &$image, string $text, int $size = 12, int $x = 0, int $y = 12, Color $color = null, string $font = '', int $angle = 0): EditorInterface 
    参数描述
    image 图片
    text 文字
    size 字体大小 (可选),默认为 12px
    x 从图像左边缘到文本左边缘的距离 (可选),默认为 0
    y 从图像上边缘到文本基线的距离 (可选),默认值为 12(等于字体大小),以便将文本放置在图像中
    color 字体颜色 (可选),默认为黑色
    font 字体路径 (可选)
    angle 文字旋转角度 (可选),取值范围为 0-359,默认为 0
    use GrafikaGrafika;
    use GrafikaColor;
    //  ……
    $editor = Grafika::createEditor();
    $editor->open($image , 'images/foo.jpg');
    $editor->text($image ,'foo',30,200,100,new Color("#000000"),'',45);
    $editor->save($image,'images/foo-text.jpg');
    $editor = Grafika::createEditor();
    $editor->open($image , 'images/foo.png');
    $editor->text($image, '测试文字', 24, 50, 50, new Color("#000000"), 'images/Alibaba-PuHuiTi-Light.ttf',90);
    header('Content-type: image/png');
    $image->blob('PNG');

    *getWidth() 获取图片宽度

    getWidth (): int 
    use GrafikaGrafika;
    //  ……
    $editor = Grafika::createEditor();
    $editor->open( $image, 'images/foo.jpg' );
    $image->getWidth();

    getHeight() 获取高度 、getImageFile() 获取名称 getType() 获取类型 isAnimated 是否是动图

    • 绘制直线
    参数描述
    point1 起始点的坐标(数组)
    point2 结束点的坐标(数组)
    thickness 宽度,其中 GD 库会忽略掉,默认为 1
    color 颜色,默认为黑色
    $editor = Grafika::createEditor();
    $editor->open($image , 'images/foo.jpg');
    // 创建绘制对象,同时检测可用的编辑器
    $drawingObject = Grafika::createDrawingObject('Line', [0, 0], [200, 200], 1, new Color('#FF0000'));
    // 在图像上绘制上面创建的绘制对象
    $editor->draw( $image, $drawingObject ); 
    header('Content-type: image/png');
    $image->blob('PNG');
    • 绘制椭圆
    参数描述
    width 宽度
    height 高度
    pos 位置 [x,y],X 为椭圆最左边距离图像最左边值,Y 最上边距离图形最上边值
    borderSize 边宽度,0 表示无边框,默认为 1
    borderColor 边颜色,NULL 表示无边框颜色,默认为黑色
    fillColor 填充颜色,NULL 表示无填充颜色,默认为白色
    // ……
    $drawingObject = Grafika::createDrawingObject('Ellipse', 100, 50, [50, 75], 1, new Color('#000000'), new Color('#FF0000'));
    $editor->draw( $image, $drawingObject ); 
    • 绘制矩形
    参数描述
    width 宽度
    height 高度
    pos 位置 [x,y],X 为矩形最左边距离图像最左边值,Y 为矩形最上边距离图像最上边值,默认为 [0,0]
    borderSize 边宽度,0 表示无边框,默认为 1
    borderColor 边颜色,NULL 表示无边框颜色,默认为黑色
    fillColor 填充颜色,NULL 表示无填充颜色,默认为白色
    // 没有边框的85x50绿色矩形
    // ……
    $drawingObject = Grafika::createDrawingObject('Rectangle', 85, 50,[105, 70], 0, null, new Color('#00FF00'));
    $editor->draw( $image,drawingObject );
    $editor = Grafika::createEditor();
    $editor->open($image , 'images/foo.png');
    // 绘制直线
    $drawingObject = Grafika::createDrawingObject('Line', [5, 145], [190, 145], 1, new Color('#FF0000'));
    // 绘制椭圆
    //$drawingObject = Grafika::createDrawingObject('Ellipse', 180, 70, [10, 10], 1, new Color('#000000'), new Color('#FF0000'));
    // 绘制矩形
    //$drawingObject = Grafika::createDrawingObject('Rectangle', 180, 70,[10, 10], 0, null, new Color('#FF0000'));
    $editor->draw($image, $drawingObject);
    header('Content-type: image/png');
    $image->blob('PNG');
    • 绘制多边形
    参数描述
    points 多边形角的坐标 [[0, 0], [50, 0], [0, 50]],至少有三个
    borderSize 边宽度,0 表示无边框,默认为 1
    borderColor 边颜色,NULL 表示无边框颜色,默认为黑色
    fillColor 填充颜色,NULL 表示无填充颜色,默认为白色
    $drawingObject = Grafika::createDrawingObject('Polygon', [[100, 0], [140, 50], [100, 100], [60, 50]], 1, NULL, new Color('#FF0000'))
    $editor->draw( $image,drawingObject );
    • blob() 图像二进制输出
    blob(string|ImageType $type): void 
    参数描述
    type 图片格式 GIF、JPEG、PNG、WBMP
    use GrafikaGrafika;
    //  ……
    $editor = Grafika::createEditor();
    $editor->open( $image, 'images/foo.jpg' ); 
    header('Content-type: image/png'); // 设置浏览器输出的类型是图片
    $image->blob('PNG'); 
    • save() 保存
    save(ImageInterface $image, string $file, null|string $type = null, null|string $quality = null, bool $interlace = false, int $permission = 0755): EditorInterface 
    参数描述
    image 图片
    file 保存路径
    type 图像格式,可以为空,gif、png 或 jpeg,如果为空,将根据 $file 中的输出文件名选择适当的格式
    quality 图像质量,仅适用于 JPEG,范围为数字 0-100,其中 0 是最低的,100 是最高的质量,默认为空,如果空值为 75,则为默认质量
    interlace 设置为渐进式 JPEG,仅适用于 JPEG,默认为 false
    permission 目录不存在时创建目录的默认权限,默认值为 0755
    $editor->open($image, 'images/foo.png');
    $editor->save($image, 'images/output.png', null, null, false, 0777);
  • 相关阅读:
    [转载]Linux下mail使用技巧
    VTK Online教程大全
    拓扑结构与TopoDS
    三维视图变换与相机模型
    说说DoDataExchange(CDataExchange* pDX)
    Lua Lib在VC下的编译
    建立最简单的OpenCASCADE程序
    构建通用类型 继承 VS 聚合
    【软件】新瓶装老酒 MyCapture
    用std::find查找文件流中的内容
  • 原文地址:https://www.cnblogs.com/zonglonglong/p/14003510.html
Copyright © 2011-2022 走看看