sivka / img-5
v1.0.1
2019-02-10 21:31 UTC
This package is auto-updated.
Last update: 2024-09-11 14:48:04 UTC
README
此库提供实时调整图片大小、添加水印的功能。支持 PHP 5.0 或更高版本。
结果图片将被缓存,因此只有在源或转换的图片未修改时才会进行一次转换。
这是一个没有命名空间的旧版本,以兼容旧版本的 PHP。
要求
- PHP 5.0+
- GD 扩展
安装
composer require sivka/img-5
或者 下载 zip 压缩包,解压到您的项目目录中。
示例
$params = array( 'width' => 100, 'height' => 150, 'crop' => false, 'watermark' => '', 'wm_position' => 'center', 'wm_opacity' => 0.6, 'quality' => 80, 'wm_text' => '', 'wm_text_color' => '#FFFFFF', 'wm_text_size' => 64, 'wm_text_font' => 'arial.ttf', 'default' => '' ); require_once 'Img/Img.php'; //without setting cache directory $src = Img::get('path/to/file.jpg', $params)); $src = Img::get('path/to/file.jpg', array('width' => 100, 'height' => 150))); //returns /path/to/thumbs/100x150/file.jpg //or simple $src = Img::get('path/to/file.jpg', $width, [$height, [$crop]]); $src = Img::get('path/to/file.jpg', 100, 100, true); //returns /path/to/thumbs/100x100cp/file.jpg //with setting cache directory Img::setCacheDirectory('cache'); $src = Img::get('path/to/file.jpg', 100, 100, true); //returns /cache/path/to/thumbs/100x100cp/file.jpg
参数
number width - with to resize
number height - height to resize
mixed crop - boolean or number(1/0) or string with crop position center|top|left|//bottom|right|top left|top right|bottom left|bottom right
string watermark - path to watermark file_ext
string wm_position - watermark position center|top|left|bottom|right|top|left|top right|bottom left|bottom right
float wm_opacity - opacity of watermar or watermark text
number quality - quality for result images
string wm_text - text for overlay on images
mixed wm_text_color - color of watermark text, maybe string('#FFFFFF')
or array ['r'=>255,'g'=>255, 'b'=> 255, 'a'=>1] or array [255, 255, 255, 1]
number wm_text_size - font size of watermark text
string wm_text_font - name of font for watermark text, the font file mast be in same directory with Img.php
string default - path to placeholder, if defined and source image does not exists, this file will be converted
方法
static get($filepath, $params); static get($filepath, $width, [$height, [$crop]]); convert image and returns relative path to converted images static setCacheDirectory($path); sets path to cache directory static setDocRoot($docRoot); set DOCUMENT_ROOT, use this if $_SERVER['DOCUMENT_ROOT'] is not real absolute path to home directory static setPlaceholder($filepath); set path to placeholder image. If source image does not exists, path to placeholder will be returned.
构建工具
作者
- AlexSivka - 初始工作 - AlexSivka
许可证
本项目采用 MIT 许可证 许可。