BigToach.com

PHP Snippets

image_add_border

Information

(PHP Version >= 4.0.6)
Functions used in this snippet: imagesx, imagesy, str_replace, strlen, list, sscanf, imagecreatetruecolor, imagecolorallocate, imagefill, imagecopy.

Description

int image_add_border( int img, str color, int width )

Adds a width width pixel border to the image img in the color color.


Here is an Example of image_add_border

Snippet

<?php
function image_add_border($img$color$width=1){
    
$x imagesx($img);
    
$y imagesy($img);
    
$color str_replace('#','',$color);
    
$color strlen($color) === $color{0} . $color{0} . $color{1} . $color{1} . $color{2} . $color{2} : $color;
    list(
$r$g$b) = sscanf($color"%2x%2x%2x");
    
$dest imagecreatetruecolor(($x + ($width 2)), ($y + ($width 2)));
    
$color imagecolorallocate($dest$r$g$b);
    
imagefill($dest00$color);
    
imagecopy($dest$img$width$width00$x$y);
    return 
$dest;
}
?>

Example

image_add_border Can be used in the following way:

<?php
$img 
imagecreatefromjpeg('someimage.jpg'); // load an image

$dest image_add_border($img'000000'); 
// add a black 1 pixel border

$dest image_add_border($img'FF0000'5);
// add a red 5 pixel border

header('Content-type: image/jpeg'); // set header

imagejpeg($dest); //send to browser
?>

Added on Jul 7th at 5 pm by Scott - 0 Comments
Updated on Aug 3rd at 4 am.


0 Responses to image_add_border

Name:

Site or Mail:

Comment:

To try to reduce the ammount of spam, please enter the word GOOSE in the following form

Security Code: