<?php

/***************************************************************/
/* GD Image Functions                                          */
/*                                                             */
/* (c)2005 - Kevin Muma - http://kevinmuma.com                 */
/***************************************************************/
/* These are several useful image functions for use with the   */
/* PHP GD image library.                                       */
/*                                                             */
/* Note: if a function returns an image that image should be   */
/* freed by calling imagedestroy on that image resource when   */
/* the image is no longer in use                               */
/***************************************************************/


/**
 * Copies and resizes $src_img image to have max dimensions of $sizeW and $sizeH.
 * Returns the resized image.
 */
function imgResize($src_img$sizeW=75$sizeH=-1){
    
    if(
$sizeH==-1)
        
$sizeH $sizeW;
    
    
// get image size
    
$srcW imagesx($src_img);
    
$srcH imagesy($src_img);
    
    
// find the longist side
    // and calculate resized dimensions
    
$dstW $sizeW;
    
$dstH = (int)(($srcH $srcW) * $dstW);
    if(
$dstH $sizeH){
        
$dstH $sizeH;
        
$dstW = (int)(($srcW $srcH) * $dstH);
    }
    
    
// create new image with new dimensions
    
$dest_img imageCreateTrueColor($dstW,$dstH);
    
    
// copy and resize the image
    
imageCopyResampled($dest_img$src_img0000$dstW,$dstH$srcW$srcH);
    
    
// return the resized image
    
return $dest_img;
}

/**
 * Copies $img and adds padding around it
 * $bgColorArr = R,G,B array for background color
 * $pad = array
 *   $pad[0] = top padding
 *   $pad[1] = left padding
 *   $pad[2] = bottom padding
 *   $pad[3] = right padding
 * Returns the new image
 */
function imgPad(&$img$pad$bgColorArr){
    
$imgX imagesx($img);
    
$imgY imagesy($img);
    
    
// make image
    
$newImg imagecreatetruecolor($imgX+$pad[1]+$pad[3],$imgY+$pad[0]+$pad[2]);
    
    
// get backgroud color
    
$col imgGetColor($newImg,$bgColorArr);
    
    
// fill with backgroud color
    
imagefilledrectangle($newImg00$imgX+$pad[1]+$pad[3], $imgY+$pad[0]+$pad[2],$col);
    
    
imagecopy(
        
$newImg,$img,
        
$pad[1],$pad[0],
        
0,0,
        
$imgX,$imgY
    
);
    
    return 
$newImg;
}

/**
 * Add the $src_img as an over lay for the image $dest_img
 * Positions are: Like number pad ...
 *             +---+---+---+
 *             | 7 | 8 | 9 |
 *             +---+---+---+
 *             | 4 | 5 | 6 |
 *             +---+---+---+
 *             | 1 | 2 | 3 |
 *             +---+---+---+
 * $padding is extra space from the edge of the image
 * xOff and yOff are offsets from the top left corner
 */
function imgLogo($dest_img$src_img$pos=3$padding=15$xOff=0$yOff=0){
    
    
imageAlphaBlending($dest_imgtrue);
    
    
$pos--;
    
    
$srcW imageSX($src_img);
    
$srcH imageSY($src_img);
    
    
$destW imageSX($dest_img);
    
$destH imageSY($dest_img);
    
    
//print "Pos: $pos = ";
    
    // X axis
    
switch($pos 3){
    
// left
        
case 0:
            
//print "left - ";
            
$destX $padding;
            break;
    
// middle
        
case 1:
            
//print "middle - ";
            
$destX = (int) ( $destW ) - ( $srcW );
            break;
    
//right
        
case 2:
            
//print "right - ";
            
$destX $destW $srcW $padding;
            break;
    }
    
    
    
// Y axis
    
switch((int) ($pos 3) ){
    
// top
        
case 2:
            
//print "top";
            
$destY $padding;
            break;
    
// middle
        
case 1:
            
//print "mid";
            
$destY = (int) ( $destH ) - ( $srcH );
            break;
    
//bottom
        
case 0:
            
//print "bottom";
            
$destY $destH $srcH $padding;
            break;
    }
    
    
imageCopy ($dest_img$src_img$destX+$xOff$destY+$yOff00$srcW$srcH);
}


/**
 * Put border around image $img
 *
 * $cImgs is an array of corner images for the border
 *   $cImgs[0] = top left image
 *   $cImgs[1] = top right image
 *   $cImgs[2] = bottom left image
 *   $cImgs[3] = bottom right image
 *
 * $sImgs is an array of side images for the border
 *   $sImgs[0] = top image
 *   $sImgs[1] = left image
 *   $sImgs[2] = bottom image
 *   $sImgs[3] = right image
 *
 * Note: The border images are placed along the image edge
 * the image is not resized
 *
 * if $tile is true the side images will be tiled, otherwise
 * they will be stretched
 *
 * Returns false on error
 */
function imgBorderImg($img$cImgs$sImgs$tile=false){
    
    
// allow alpha
    
imageAlphaBlending($imgtrue);
    
    
// check for arrays
    
if(!is_array($cImgs) || !is_array($sImgs)
            || 
count($cImgs)<|| count($sImgs)<)
        return(
false);
    
    
// get image size
    
$imgW imagesx($img);
    
$imgH imagesy($img);
    
    
// add corners
    
for ($i 0$i<4$i++){
        
// get corner height and width
        
$cCords[$i]['w'] = imagesx($cImgs[$i]);
        
$cCords[$i]['h'] = imagesy($cImgs[$i]);
        
        
// set x and y cords for corner
        
$cCords[$i]['x'] = $i $imgW $cCords[$i]['w'] : 0;
        
$cCords[$i]['y'] = $i $imgH $cCords[$i]['h'] : 0;
        
        
// add the images
        
imagecopy($img,$cImgs[$i],
                
$cCords[$i]['x'],$cCords[$i]['y'],
                
0,0,$cCords[$i]['w'],$cCords[$i]['h']);
    }
    
    
// check side mode
    
if($tile)
        
$sideFunc 'imgCopyTiled';
    else
        
$sideFunc 'imageCopyResampled';
    
    
// add sides
    
for ($i 0$i<4$i++){
        
// side height and width
        
$sCords[$i]['w'] = imagesx($sImgs[$i]);
        
$sCords[$i]['h'] = imagesy($sImgs[$i]);
        
        
// side num 1 or 3 (left-right)
        
if( $i ){
            
$sCords[$i]['x'] = $i&$imgW $sCords[$i]['w']:0;
            
$sCords[$i]['y'] = $cCords[$i>>1]['h'];
            
$sCords[$i]['dw'] = $sCords[$i]['w'];
            
$sCords[$i]['dh'] = $cCords[$i|2]['y']-$sCords[$i]['y'];
            
            
// see if faster CopyResized can be used
            
$thisSideFunc $sCords[$i]['h'] == 'imageCopyResized' $sideFunc;
        
        
// side num 0 or 2 (top-bottom)
        
} else {
            
$sCords[$i]['x'] = $cCords[$i]['w'];
            
$sCords[$i]['y'] = $i $imgH $sCords[$i]['h']: 0;
            
$sCords[$i]['dw'] = $cCords[$i+1]['x']-$sCords[$i]['x'];
            
$sCords[$i]['dh'] = $sCords[$i]['h'];
            
            
// see if faster CopyResized can be used
            
$thisSideFunc $sCords[$i]['w'] == 'imageCopyResized' $sideFunc;
        }
        
        
$thisSideFunc $img$sImgs[$i], 
                
$sCords[$i]['x'], $sCords[$i]['y'], 
                
00$sCords[$i]['dw'], $sCords[$i]['dh'], 
                
$sCords[$i]['w'], $sCords[$i]['h']
        );
    }
}

/**
 * Fills rect area in destImg with tiled image $srcImg
 *
 * Same paramaters as GD function imageCopyResampled
 */
function imgCopyTiled ($destImg$srcImg$dstX$dstY$srcX$srcY$dstW$dstH$srcW$srcH){
    
    
$x2 $dstX $dstW;
    
$y2 $dstY $dstH;
    for(
$x $dstX$x $x2$x += $srcW){
        
        
$imgW $x2 $x;
        if(
$imgW $srcW)
            
$imgW $srcW;
        
        for(
$y $dstY$y $y2$y += $srcH){
            
            
$imgH $y2 $y;
            if(
$imgH $srcW)
                
$imgH $srcH;
            
            
imagecopy(
                        
$destImg,$srcImg,
                        
$x,$y,
                        
$srcX,$srcY,
                        
$imgW,$imgH
                    
);
        }
    }
}

/**
 * Returns array of R,G,B values for the color
 * parsed as rgb values or #HexCode
 * or false on error
 */
function imgGetColorArr($str){
    
$col null;
    
// hex colour
    
if(substr($str,0,1)=='#'){
        if(
strlen($str)==7){
            for(
$n=1;$n<7;$n+=2){
                
$col[] = hexdec(substr($str$n2));
            }
            return 
$col;
        }else{
            return 
false;
        }
    
// rgb colour string
    
} else {
        
$col explode(','$str);
        if(
sizeof($col)==3)
            return 
$col;
        else
            return 
false;
    }
}

/**
 * Returns a color from an image using
 * an array of RGB color values for input
 */
function imgGetColor($img,$cArr){
    if(!
is_array($cArr)){
        
$cArr = array(0,0,0);
    }
    
    return 
ImageColorAllocate($img$cArr[0], $cArr[1], $cArr[2]);
}

/**
 * Draws Border on image
 * $colour is the colour of the border
 * as an array of R,G,B valuse (0-255)
 */
function imgBorder($img$thickness=1$colour=NULL){
    
    
$width imageSX($img);
    
$height imageSY($img);
    
    if(
$thickness == 1){
        
$width--;
        
$height--;
    } else {
        
$thickness 2*$thickness;
    }
    
    
imagesetthickness($img$thickness);
    
    
// get colour
    
$colour imgGetColor($img$colour);
    
    
ImageRectangle($img00$width$height$colour);
}

/**
 * Loads an image from a file
 * returns the image or false on error
 */
function imgLoad($file){
    
    
// get info
    
$imgInf = @getimagesize($file);
    
    
// check for error
    
if ($imgInf === NULL)
        return 
false;
    
    
// check imgage
    
switch ($imgInf[2]){
        
// gif
        
case 1:
            
$type 'gif';
            break;
        
//jpg
        
case 2:
            
$type 'jpeg';
            break;
        
// png
        
case 3:
            
$type 'png';
            break;
        default:
            return 
false;
    }
    
    
    
    if( !
$src_img = @call_user_func('imagecreatefrom'.$type$file) )
        return 
false;
    
    return 
$src_img;
}

?>