定制 zodream/image 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zodream/image

Composer 安装命令:

composer require zodream/image

包简介

image, captcha

README 文档

README

使用 gd 对图片处理的封装,包含 验证码、水印、缩略图、二维码、图片拖拽验证

当前状态:一直开发中

简单使用教程

验证码

滑动验证码

文字点击验证

二维码

水印

缩略图

图片比较(简单版)

内容生成图片

使用 iconfont 等类似字体图标

$image = new Image();
$res = $image->instance();
$res->create(ImageManager::createSize(200, 200));
$res->fill('#fff');
$res->text("\u{e709}", ImageManager::createFont(app_path('data/fonts/iconfont.ttf')), ImageManager::createPoint(100, 100));
$image->show();

** 请注意:\u{e709} 表示一个字符,且字符串必须用双引号 **

ico 生成

use Zodream\Image\Ico;

$image = new Ico('1.png');
$image->saveAsSize('1.ico', $image->getSizes());

验证码

use Zodream\Image\Captcha;

$captcha = new Captcha();
$captcha->setConfigs([
    'width' => 200,
    'fontSize' => 20,
    'fontFamily' => 'Ubuntu_regular.ttf'
]);
$source = $captcha->generate();

$captcha->verify($_POST['captcha'], $source);

默认配置

[
    'characters' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', //随机因子
    'length' => 4,    //验证码长度
    'fontSize' => 0,   //指定字体大小
    'fontColor' => '',   //指定字体颜色, 可以是数组
    'fontFamily' => null,  //指定的字体
    'width' => 100,        // 图片宽
    'height' => 30,       // 图片高
    'angle' => 0,         //角度
    'sensitive' => true,   // 大小写敏感
    'mode' => 0           // 验证码模式: 0 文字 1 公式
]

二维码

use Zodream\Image\QrCode;

$qr = new QrCode();
$image = $qr->encode('123123');
$image->save();

水印

文字水印

$image = new WaterMark();
$image->instance()->loadResource('1.jpg');
$image->addTextByDirection('water');

自定义水印

$image = new WaterMark();
$image->instance()->loadResource($file);
$font = new Font((string)app_path(config('disk.font')), 12, '#fff');
$textBox = $image->instance()->fontSize($text, $font);
// 根据文字的尺寸获取水印的位置
list($x, $y) = $image->getPointByDirection(WaterMark::RightTop, $textBox->getWidth(), $textBox->getHeight(), 20);
// 给文字添加阴影
$image->addText($text, $x + 2, $y + 2, $font->getSize(), '#777', $font->getFile());
$image->addText($text, $x, $y, $font->getSize(), $font->getColor(), $font->getFile());

滑动验证码

use Zodream\Image\SlideCaptcha;

$captcha = new SlideCaptcha();
$captcha->setConfigs([
    'width' => 300,
    'height' => 130,
]);
$captcha->instance()->open('bg.jpg');
$captcha->setShape('shape.jpg'); // 根据图片抠图
$source = $captcha->generate();

$captcha->verify($_POST['captcha'], $source);

$imgData = $captcha->toArray();
$html = '';
foreach ($imgData['imageItems'] as $point) {
    $html .= sprintf('<div class="slide-img" style="background-position: %spx %spx"></div>', $point['x'], $point['y']);
}

$html = <<<HTML
<style>
.slide-box {
    width: {$imgData['width']}px;
    height: {$imgData['height']}px;
    position: relative;
}
.slide-box .slide-img {
    float: left;
    margin: 0;
    padding: 0;
    background-image: url({$imgData['image']});
    background-repeat: no-repeat;
    width: {$imgData['imageItems'][0]['width']}px;
    height: {$imgData['imageItems'][0]['height']}px;
}
.slide-box .slide-cut {
    position: absolute;
    top: {$imgData['controlY']}px;
    background-image: url({$imgData['control']});
    background-repeat: no-repeat;
    width: {$imgData['controlWidth']}px;
    height: {$imgData['controlHeight']}px;
    z-index: 9;
}
</style>
<div class="slide-box">
    <div class="slide-cut"></div>
    <div class="slide-list">
        {$html}
    </div>
    
</div>
HTML;

点击验证码

依次点击图片上的文字

$captcha = new HintCaptcha();
$items = ['', '', '', ''];
$captcha->setConfigs([
    'width' => 300,
    'height' => 130,
    'fontSize' => 20,
    'fontFamily' => 'Yahei.ttf',
    'words' => $items,
    'count' => 3,
]);
$captcha->instance()->open('images/banner.jpg');
$source = $captcha->generate();

$captcha->verify($_POST['captcha'], $source);

$imgData = $captcha->toArray();

内容生成图片

$str = <<<TEXT



[padding=10 background=#fff width=470]
[img width=450 height=450]aaa
[size=20 padding=10,0 bold]sbfajahaa
[color=#ccc size=12]sdfsssdfdafsdaasfs
[img width=100 height=100 center]iadfasdsad
[size=10 color=#ccc center]123131231
TEXT;
$box = BoxNode::parse($str);
dr($box);


$img = __DIR__.'/assets/images/banner.jpg';
$font = __DIR__.'/../data/fonts/msyh.ttc';
$box = BoxNode::create([
    'padding' => 10,
    'background' => 'white',
    'width' => 470
])->append(
    ImgNode::create($img, [
        'width' => '100%',
        'height' => '100%'
    ]),
    TextNode::create('sbfajahaa', [
        'size' => 20,
        'letterSpace' => 20,
        'padding' => [
            10,
            0,
        ],
        'bold' => true,
        'font' => $font
    ]),
    TextNode::create('1234avccg', [
        'size' => 12,
        'font' => $font,
        'letterSpace' => 4,
        'lineSpace' => 4,
        'color' => '#ccc'
    ]),
    ImgNode::create($img, [
        'width' => '100',
        'height' => '100',
        'center' => true
    ]),
    TextNode::create('sbfajahaa', [
        'size' => 12,
        'color' => '#ccc',
        'letterSpace' => 4,
        'lineSpace' => 4,
        'wrap' => false,
        'font' => $font,
        'center' => true
    ]),
    BorderNode::create([
        'size' => 1,
        'fixed' => true,
        'margin' => 10
    ]),
    LineNode::create(10, 10, 10, 100, [
        'size' => 1,
        'fixed' => true,
        'color' => 'black'
    ]),
    RectNode::create([
        'points' => [
            [0, 0],
            [200, 0],
            [0, 200],
        ],
        'color' => 'black'
    ])
);
$box->beginDraw()->show();

zodream/image 适用场景与选型建议

zodream/image 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 174 次下载、GitHub Stars 达 16, 最近一次更新时间为 2018 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「zodream」 「image gd」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 zodream/image 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 zodream/image 我们能提供哪些服务?
定制开发 / 二次开发

基于 zodream/image 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 174
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 16
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 16
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-01-04