BigToach.com

PHP Snippets

image_borders

Information

(PHP Version >= 4.1.6)
Functions used in this snippet: is_array, strtolower, substr, strrpos, imagesx, imagesy, imagecreate, imagecopy, min.

Description

int image_borders( int img, array borders )

Adds borders to the image img. The borders array should have the indexes tl, tt, tr, rr, br, bb, bl, ll correspoding to that area's border. (see the example below for a better explanation)

Note: This function is fairly slow, so it would be best to apply it once, then save it for future showings.

Also, the wider the top(tt) and bottom(bb) and the longer the left(ll) and right(rr) borders are the faster the function will perform. They CAN be longer and wider than the actual image itself.

Snippet

<?php
function image_borders($img$borders){
    if(!
is_array($borders)){
        return 
$img;
    }
    
$border_data = array();
    
$types = array('jpeg'=>'imagecreatefromjpeg''jpg'=>'imagecreatefromjpeg''gif'=>'imagecreatefromgif''png'=>'imagecreatefrompng');
    foreach(
$borders as $key=>$value){
        
$border_data[$key]['img'] = $types[strtolower(substr($valuestrrpos($value'.') + 1))]($value);
        
$border_data[$key]['x']   = imagesx($border_data[$key]['img']);
        
$border_data[$key]['y']   = imagesy($border_data[$key]['img']);
    }
    
$x imagesx($img);
    
$y imagesy($img);
    
$dest imagecreate(($x $border_data['ll']['x'] + $border_data['rr']['x']), ($y $border_data['tt']['y'] + $border_data['bb']['y']));
    
$max_x $x $border_data['ll']['x'];
    
$actual_x $border_data['ll']['x'];
    
$ttx $border_data['ll']['x'];
    
$bbx $border_data['ll']['x'];
    while(
$actual_x $max_x){
        if(
$ttx $max_x){
            
imagecopy($dest$border_data['tt']['img'], $ttx000$border_data['tt']['x'], $border_data['tt']['y']);
            
$ttx += $border_data['tt']['x'];
        }
        if(
$bbx $max_x){
            
imagecopy($dest$border_data['bb']['img'], $bbx, ($border_data['ll']['y'] + $y), 00$border_data['bb']['x'], $border_data['bb']['y']);
            
$bbx += $border_data['bb']['x'];
        }
        
$actual_x min($ttx$bbx);
    }
    
$max_y $y $border_data['tt']['y'];
    
$actual_y $border_data['tt']['y'];
    
$lly $border_data['tt']['y'];
    
$rry $border_data['rr']['y'];
    while(
$actual_y $max_y){
        if(
$lly $max_y){
            
imagecopy($dest$border_data['ll']['img'], 0$lly00$border_data['ll']['x'], $border_data['ll']['y']);
            
$lly += $border_data['ll']['y'];
        }
        if(
$rry $max_y){
            
imagecopy($dest$border_data['rr']['img'], ($border_data['ll']['x'] + $x), $rry0,0$border_data['rr']['x'], $border_data['rr']['y']);
            
$rry += $border_data['rr']['y'];
        }
        
$actual_y min($lly$rry);
    }
    
imagecopy($dest$border_data['tl']['img'], 0000$border_data['tl']['x'], $border_data['tl']['y']);
    
imagecopy($dest$border_data['tr']['img'], ($x $border_data['tl']['x']), 000$border_data['tr']['x'], $border_data['tr']['y']);
    
imagecopy($dest$border_data['bl']['img'], 0, ($y $border_data['tl']['y']), 00$border_data['bl']['x'], $border_data['bl']['y']);
    
imagecopy($dest$border_data['br']['img'], ($x $border_data['tl']['x']), ($y $border_data['tl']['y']), 00$border_data['br']['x'], $border_data['br']['y']);
    
imagecopy($dest$img$border_data['ll']['x'], $border_data['tt']['y'], 00$x$y);
    return 
$dest;
}
?>

Example

image_borders Can be used in the following way:

<?php
$img 
imagecreatefromjpeg('baseimage.jpg');
// create the base image

$borders = array(    'tl'=>'topleft.jpg',
                    
'tt'=>'top.jpg',
                    
'tr'=>'topright.jpg',
                    
'rr'=>'right.jpg',
                    
'br'=>'bottomright.jpg',
                    
'bb'=>'bottom.jpg',
                    
'bl'=>'bottomleft.jpg',
                    
'll'=>'left.jpg');
// create your array with borders
// Note, the array indexes must be the same as above

$dest image_borders($img$borders);
// actually make the image

header('Content-type: image/png');
// send a header

imagepng($dest);
// send it to the browser
?>

Added on Jul 19th at 1 am by Scott - 0 Comments


0 Responses to image_borders

Name:

Site or Mail:

Comment:

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

Security Code: