图片处理 PictureLibrary

图片追加水印、文字以及将长图拆分为1:1多图

调用类:

Abnermouke\Supports\Library\PictureLibrary::make($source)

默认需传入处理图片绝对路径

添加图片水印实例:

$main_img_path = 'path to image';
$water_img_path = 'path to water image';
$width = 100;            //水印图片宽度
$height = 100;           //水印图片高度
$x = 10;                 //水印图片X轴坐标
$y = 10;                 //水印图片Y轴坐标
$position = 'top-left';  //水印图片对其位置(默认:左上)
$save_img_path = 'path to save image';

Abnermouke\Supports\Library\PictureLibrary::make($main_img_path)
->draw($water_img_path, $width, $height, $x, $y, $position)
->save($save_img_path);

添加文字水印实例:

$main_img_path = 'path to image';
$string = 'xxxxxxx';                 //水印文字内容
$ttf = 'path to ttf file';           //水印文字字体文件地址
$x = 10;                             //水印文字X轴坐标
$y = 10;                             //水印文字Y轴坐标
$font_size = 20;                     //水印文字大小
$save_img_path = 'path to save image';

Abnermouke\Supports\Library\PictureLibrary::make($main_img_path)
->text($string, $ttf, $x, $y, $font_size, '#000000', 'left', 'top')
->save($save_img_path);

当然,水印肯定是可以重叠使用的:

$main_img_path = 'path to image';
$save_img_path = 'path to save image';

Abnermouke\Supports\Library\PictureLibrary::make($main_img_path)
->draw('path_to_water_path', 100, 100, 500, 500, ''top-left')
->draw('path_to_water_path', 300, 300, 100, 100, ''top-left')
->text('xxxxxxx', 'path_to_ttf_file', 100, 100 '#000000', 'left', 'top')
->text('xxxxxxx', 'path_to_ttf_file', 200, 300 '#FFFFFF', 'left', 'top')
->save($save_img_path);

拆剪正方形

常用于商品详情图,如供应链或厂家提供的为长图,为减小加载压力,需将长图拆剪为等比图片进行展示

$main_img_path = 'path to image';
$prefix = 'square-';

Abnermouke\Supports\Library\PictureLibrary::make($main_img_path)
->squares($prefix, 100, null);

拆剪的长图将生成多张图片,文件名为:$prefix[序号]_[原文件名],例如:square-0_main_picture.jpg

Last updated