php图片增加边框

<?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));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值