<?php
/**
* 给图片加边框
* @param $ImgUrl 图片地址
* @param $SavePath 新图片保存路径
* @return false|string <boolean, 新图片绝对路径>
*/
function ImageAddBoard($ImgUrl, $SavePath)
{
$px = rand(1, 4);// 1至4px随机
$colorArr = [[255, 255, 255], [209, 209, 209], [210, 210, 210], [211, 211, 211], [212, 212, 212], [213, 213, 213], [204, 204, 204], [193, 193, 193]];
$colorRand = $colorArr[array_rand($colorArr)];//多个颜色随机
$aPathInfo = pathinfo($ImgUrl);
//$sFileName = $aPathInfo ['filename'];// 文件名称
$sExtension = $aPathInfo ['extension'];// 图片扩展名
list($img_w, $img_h) = getimagesize($ImgUrl);// 获取原图大小
// 读取图片
if (strtolower($sExtension) == 'png') {
$resource = imagecreatefrompng($ImgUrl);
} elseif (strtolower($sExtension) == 'jpg' || strtolower($sExtension) == 'jpeg') {
$resource = imagecreatefromjpeg($ImgUrl);
} elseif (strtolower($sExtension) == 'gif') {
$resource = imagecreatefromgif($ImgUrl);
}
// 背景图片
$im = @imagecreatetruecolor(($img_w + $px * 2), ($img_h + $px * 2)) or die ("Cannot Initialize new GD image stream");
// 为真彩色画布创建背景,再设置为透明
list($r, $g, $b) = $colorRand;
$color = imagecolorallocate($im, $r, $g, $b);
imagefill($im, 0, 0, $color);
imageColorTransparent($im, $color);
//边框px
imagecopy($im, $resource, $px, $px, 0, 0, $img_w, $img_h);
$uuid = uniqid();
$imgNewUrl = $SavePath . $uuid . '.' . $sExtension;
if (strtolower($sExtension) == 'png') {
$ret = imagepng($im, $imgNewUrl);
} elseif (strtolower($sExtension) == 'jpg' || strtolower($sExtension) == 'jpeg') {
$ret = imagejpeg($im, $imgNewUrl);
} elseif (strtolower($sExtension) == 'gif') {
$ret = imagegif($im, $imgNewUrl);
}
imagedestroy($im);
return $ret ? $imgNewUrl : false;
}
$savePath = '/home/Go_WorkSpace/';
$url = '/home/Go_WorkSpace/test.jpg';
var_dump(ImageAddBoard($url, $savePath));
php图片增加边框
最新推荐文章于 2023-05-23 08:55:48 发布