coldume/imagecraft
Composer 安装命令:
composer require coldume/imagecraft
包简介
A reliable and extensible PHP image manipulation library
README 文档
README
Imagecraft is a reliable and extensible PHP image manipulation library. It can edit and compose images in multiple layers and supports watermark, resize and text. Furthermore, it keeps GIF animation, optimizes memory usage, catches memory exhausted error, and gives an user-friendly/translated feedback.
Imagecraft is intended to be an image abstraction & manipulation layer, which offers a PDO-like API. It currently supports GD extension, and will support ImageMagick in version 2.0. If you have any suggestions, comments or feedback, let me know. Thanks.
Requirement
- PHP >= 5.4.0
- PHP GD extension
Installation
Composer is the recommended way to install Imagecraft, simply add a dependency to your project's composer.json file:
{
"require": {
"coldume/imagecraft": "~1.0"
}
}
Or, you can install from an archive file: imagecraft_1.0.4.zip
Usage
Provide the User with a Hint on What He Needs to Upload
use Imagecraft\ImageBuilder; $options = ['engine' => 'php_gd']; $builder = new ImageBuilder($options); $context = $builder->about(); if (!$context->isEngineSupported()) { echo 'Sorry, image processing service is currently not available.'.PHP_EOL; } else { $formats = $context->getSupportedImageFormatsToString(); echo 'Make sure that you\'re using one of the following image formats: '.$formats.'.'.PHP_EOL; $formats = $context->getSupportedFontFormatsToString(); echo 'We accept the following font formats: '.$formats.'.'.PHP_EOL; }
Build an Image in Fluent Pattern
- Client uploads an image from URL.
- Server performs resize and watermark operations.
- Server returns a message in English if an error occured, such as data size exceeds allowable limit (2MB), timeout expired (20s), file not found, etc.
use Imagecraft\ImageBuilder; $options = ['engine' => 'php_gd', 'locale' => 'en']; $builder = new ImageBuilder($options); $image = $builder ->addBackgroundLayer() ->http('www.imagecraft.cc/web/images/pikachu.gif', 2048, 20) ->resize(400, 400, 'shrink') ->done() ->addImageLayer() ->filename(__DIR__.'/pikachu_what_to_do_logo_by_mnrart-d5h998b.png') ->move(-20, -20, 'bottom_right') ->done() ->save() ; if ($image->isValid()) { file_put_contents(__DIR__.'/output.'.$image->getExtension(), $image->getContents()); } else { echo $image->getMessage().PHP_EOL; }
Build an Image in Classic Pattern
- Client uploads an image directly.
- Server performs thumbnail and text operations.
- Server returns a message in traditional Chinese if an error occured.
use Imagecraft\ImageBuilder; $options = ['engine' => 'php_gd', 'locale' => 'zh_TW']; $builder = new ImageBuilder($options); $layer = $builder->addBackgroundLayer(); $layer->filename(__DIR__.'/yotsuba_koiwai.gif'); $layer->resize(150, 150, 'fill_crop'); $layer = $builder->addTextLayer(); $layer->font(__DIR__.'/minecraftia.ttf', 12, '#FFF'); $layer->label(date('F j, Y, g:i a')); $layer->move(-10, -10, 'bottom_right'); $image = $builder->save(); if ($image->isValid()) { file_put_contents(__DIR__.'/output.'.$image->getExtension(), $image->getContents()); } else { echo $image->getMessage().PHP_EOL; }
When Debugging is Easier than Expected
use Imagecraft\ImageBuilder; // Build an image if ($image->isValid()) { file_put_contents(__DIR__.'/output.'.$image->getExtension(), $image->getContents()); print_r($image->getExtras()); } else { echo $image->getMessage().PHP_EOL; print_r($image->getVerboseMessage()); }
Cheat Sheet
Options
| Name | Default | Available | Description |
|---|---|---|---|
engine |
php_gd |
php_gd |
image library to be used |
cache_dir |
n/a |
n/a |
project-specific cache directory |
debug |
true |
true, false |
debug mode of your project |
locale |
en |
en, zh_TW, .. |
error message language |
jpeg_quality |
100 |
[0, 100] |
quality of the JPEG image |
png_compression |
100 |
[0, 100] |
compression level of the PNG image |
memory_limit |
-10 |
[-∞, ∞] |
maximum memory to use in MB |
gif_animation |
true |
true, false |
whether to maintain the GIF animation |
output_format |
default |
jpeg, png, .. |
output image format |
Layers
- Letting your mouse hover over
should cause a tooltip to appear.
- Method
http(),filename()orcontents()is required forBackgroundLayerandImageLayer. - Methods
font()andlabel()are required forTextLayer.
Tips
-
In addition to the default English error message, Imagecraft can be switched to other languages. You can help with existing translations or to add another language. The translation files are located at:
Resources
-
Mike Flickinger (2005) "What's In A GIF".
-
David C. Kay (1994) "Graphic File Formats".
coldume/imagecraft 适用场景与选型建议
coldume/imagecraft 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 34.03k 次下载、GitHub Stars 达 101, 最近一次更新时间为 2014 年 10 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「image」 「thumbnail」 「resize」 「gd」 「watermark」 「animation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 coldume/imagecraft 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 coldume/imagecraft 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 coldume/imagecraft 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
A PHP library for fetching thumbnails from a URL
Image cache
The lireincore/imgcache integration for the Yii2 framework
The Yii2 extension uses jQuery PrettyPhoto and OwlCarousel js and makes image galary from php array of structure defined.
统计信息
- 总下载量: 34.03k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 101
- 点击次数: 12
- 依赖项目数: 2
- 推荐数: 3
其他信息
- 授权协议: MIT
- 更新时间: 2014-10-24

