站点图标

PHP GD图片处理[转换格式-水印-缩略图]

2019-02-14折腾记录PHP / PHP-GD
本文最后更新于 607 天前,文中所描述的信息可能已发生改变

最近准备弄一个图床,既然是图床就需要能对图片进行一些常用的操作,比如水印,转换格式,生成缩略图等,前几天刚好接触了 PHP 的 GD 库,于是便花了点时间封装了这个简单的图片处理函数。


_151
/**
_151
* 将图片转换为webp或其他格式,同时可以添加水印
_151
*
_151
* @author Otstar Lin
_151
*
_151
* @param String $source 原图片地址
_151
* @param String $type 转换后图片格式
_151
* @param String $to 转换后图片地址 若为 null 则代表直接输出到浏览器
_151
* @param Array $thumbnail_size 缩略图尺寸
_151
* @param Integer $thum 要输出到浏览器的图片,当 $to == null 的时候生效, -1 代表使用原尺寸图,其他数字分别代表使用 $thumbnail_size 指定索引的尺寸图
_151
* @param Boolean $watermark 是否加水印
_151
* @param Integer $watermark_type 水印格式 1:文字 2:图片
_151
* @param String $watermark_content 水印内容,若为图片则是水印图片地址
_151
* @param Integer $position 水印位置
_151
* @param Integer $alpha 水印透明度
_151
* @param Array $color 水印内容颜色
_151
* @param Integer $font_size 水印文字大小
_151
* @param String $font_name 水印字体地址
_151
*/
_151
function image_change($source, $type='webp', $to = null, $thumbnail_size = [], $thum = -1,
_151
$watermark = false, $watermark_type = 1, $watermark_content = '', $position = 9,
_151
$alpha = 0, $color = [0, 0, 0], $font_size = 14, $font_name = 'Roboto-Medium.ttf')
_151
{
_151
$image = imagecreatefromstring(file_get_contents($source));
_151
// 生成水印
_151
if($watermark) {
_151
// 获取原图信息
_151
$image_info = getimagesize($source);
_151
// 设置颜色
_151
$content_color = imagecolorallocatealpha($image, $color[0], $color[1], $color[2], $alpha);
_151
// 载入字体
_151
putenv('GDFONTPATH=' . realpath('.'));
_151
$font = $font_name;
_151
$left = 0;
_151
$top = 0;
_151
// 获取水印占据的空间大小
_151
$watermark_content_info = [];
_151
if($watermark_type == 1) {
_151
$temp = imagettfbbox($font_size, 0, $font_name, $watermark_content);
_151
$watermark_content_info[] = $temp[2] - $temp[0];
_151
$watermark_content_info[] = $temp[1] - $temp[7];
_151
} else {
_151
$temp = getimagesize($watermark_content);
_151
$watermark_content_info[] = $temp[0];
_151
$watermark_content_info[] = $temp[1];
_151
}
_151
// 对水印进行定位
_151
switch($position) {
_151
case 1:
_151
$left = 20;
_151
$top = 20;
_151
break;
_151
case 2:
_151
$left = $image_info[0]/2 - $watermark_content_info[0]/2;
_151
$top = 20;
_151
break;
_151
case 3:
_151
$left = $image_info[0] - $watermark_content_info[0] - 20;
_151
$top = 20;
_151
break;
_151
case 4:
_151
$left = 20;
_151
$top = $image_info[1]/2 - $watermark_content_info[1]/2;
_151
break;
_151
case 5:
_151
$left = $image_info[0]/2 - $watermark_content_info[0]/2;
_151
$top = $image_info[1]/2 - $watermark_content_info[1]/2;
_151
break;
_151
case 6:
_151
$left = $image_info[0] - $watermark_content_info[0] - 20;
_151
$top = $image_info[1]/2 - $watermark_content_info[1]/2;
_151
break;
_151
case 7:
_151
$left = 20;
_151
$top = $image_info[1] - $watermark_content_info[1] - 20;
_151
break;
_151
case 8:
_151
$left = $image_info[0]/2 - $watermark_content_info[0]/2;
_151
$top = $image_info[1] - $watermark_content_info[1] - 20;
_151
break;
_151
case 9:
_151
$left = $image_info[0] - $watermark_content_info[0] - 20;
_151
$top = $image_info[1] - $watermark_content_info[1] - 20;
_151
break;
_151
}
_151
// 对文字水印和图片水印进行不同的处理
_151
if($watermark_type == 1) {
_151
imagettftext($image, $font_size, 0, $left, $font_size + $top, $content_color, $font, $watermark_content);
_151
} else {
_151
$watermark_image = imagecreatefromstring(file_get_contents($watermark_content));
_151
imagecopy($image, $watermark_image, $left, $top, 0, 0, $watermark_content_info[0], $watermark_content_info[1]);
_151
}
_151
}
_151
// 获取图片文件名(去除后缀)
_151
$image_pathinfo = pathinfo($source);
_151
$image_name = str_replace('.'.$image_pathinfo['extension'], '', $image_pathinfo['basename']);
_151
$path = null;
_151
// 输出原图
_151
if($to === null) {
_151
header('Content-type:image/'.$type);
_151
} else {
_151
$path = $to.$image_name.'.'.$type;
_151
}
_151
if($type == 'jepg') {
_151
if($thum == -1) imagejpeg($image, $path);
_151
} else if($type == 'png') {
_151
if($thum == -1) imagepng($image, $path);
_151
} else {
_151
if($thum == -1) imagewebp($image, $path);
_151
}
_151
// 输出缩略图
_151
foreach ($thumbnail_size as $num => $size) {
_151
$set = true;
_151
if(!isset($size['height'])) {
_151
$set = false;
_151
$size['height'] = $size['width'] * $image_info[1]/$image_info[0];
_151
}
_151
$thum_img = imagecreatetruecolor($size['width'], $size['height']);
_151
imagecopyresampled($thum_img, $image, 0, 0, 0, 0, $size['width'], $size['height'], $image_info[0], $image_info[1]);
_151
if($to !== null) {
_151
if($type == 'jepg') {
_151
imagewebp($thum_img, $to.$image_name.'-'.$size['width'].'.'.$type);
_151
} else if($type == 'png') {
_151
imagewebp($thum_img, $to.$image_name.'-'.$size['width'].'.'.$type);
_151
} else {
_151
imagewebp($thum_img, $to.$image_name.'-'.$size['width'].'.'.$type);
_151
}
_151
} else {
_151
if($num == $thum) {
_151
if($type == 'jepg') {
_151
imagewebp($thum_img);
_151
} else if($type == 'png') {
_151
imagewebp($thum_img);
_151
} else {
_151
imagewebp($thum_img);
_151
}
_151
}
_151
}
_151
}
_151
}
_151
// 缩略图的尺寸
_151
$thum = [
_151
[
_151
'width' => 200,
_151
],
_151
[
_151
'width' => 400,
_151
'height' => 200
_151
]
_151
];
_151
image_change('img.png', 'webp', '', $thum, -1, true, 1, 'Otstar Lin', 9, 0);

PHP GD图片处理[转换格式-水印-缩略图]

https://blog.ixk.me/post/php-gd-image-processing
  • 许可协议

    BY-NC-SA

  • 发布于

    2019-02-14

  • 本文作者

    Otstar Lin

转载或引用本文时请遵守许可协议,注明出处、不得用于商业用途!

PHP GD生成验证码Origami - 简洁轻快的WordPress主题