Give border to image created in PHP

耗尽温柔 提交于 2019-12-12 08:48:24

问题


How do I give a border to an image created using PHP?


回答1:


function drawBorder(&$img, &$color, $thickness = 1) 
{
    $x1 = 0; 
    $y1 = 0; 
    $x2 = ImageSX($img) - 1; 
    $y2 = ImageSY($img) - 1; 

    for($i = 0; $i < $thickness; $i++) 
    { 
        ImageRectangle($img, $x1++, $y1++, $x2--, $y2--, $color); 
    } 
}

Then the usage would just to do.

$color = imagecolorallocate($img, 255, 0, 0);
drawBorder($img,$color, 255);



回答2:


I didn't test this but I think it will do the trick.

function addBorder($image, $width, $height)
{
    $gd = imagecreatetruecolor($width, $height);

    for($i = 0; $i<$height; $i++)
    {
        // add left border
        imagesetpixel($image,0,$i, imagecolorallocate($gd, 0,0,0) );
        // add right border
        imagesetpixel($image,$width-1,$i, imagecolorallocate($gd, 0,0,0) );
    } 
    for($j = 0; $j<$width; $j++)
    {
        // add bottom border
        imagesetpixel($image,$j,0, imagecolorallocate($gd, 0,0,0) );
        // add top border
        imagesetpixel($image,$j,$height-1, imagecolorallocate($gd, 0,0,0) );
    }

    return $image;
}

$image = //your image
$width = //your iimage width
$height = //your image height


$image = addBorder($image, $width, $height);



回答3:


With ImageMagick:

bool Imagick::borderImage ( mixed $bordercolor , int $width , int $height )

Surrounds the image with a border of the color defined by the bordercolor ImagickPixel object.



来源:https://stackoverflow.com/questions/3006007/give-border-to-image-created-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!