Image Cropping/Trimming and Resizing Tool (PHP & GD Library)
This function, while taking the correct parameters, while clip designated areas(edges) of an image. It will call createthumb() which uses an images true aspect ratio to properly size the image to fit in the new dimensions of ($new_w x $new_h). If createthumb() fails to create a new image, it will return the source of a default image [user specified].
SAVE THIS FILE AS: _thumb.php
PHP Code:
<?php
//********************************************* [START] ********************************************// /* * TITLE: IMAGE CROPPING * VERSION: 1.2 * AUTHOR: David Russell * COPYRIGHT: Cypher101 Development (c) 2011 * CREATED: Thursday, March 10, 2011 12:33 AM EST (UTC -5) (America/Montreal) * * **IMPORTANT** * This script is open to the public. If being used please keep the shown copyright information for * publisher and owner development credit. Any modifications below can be made. * * --------------------------------------------------------------------------------------------------- * SUMMARY * --------------------------------------------------------------------------------------------------- * * This function, while taking the correct parameters, while clip designated areas(edges) of an image. * It will call createthumb() which uses an images true aspect ratio to properly size the image to fit * in the new dimensions of ($new_w x $new_h). If createthumb() fails to create a new image, it will * return the source of a default image [user specified]. * * **IMPORTANT** * Change the resource of the default image and the output source of newly generated default image * in createthumb(). This resized source will be returned as a string value if createthumb() fails to * generate an image. * * --------------------------------------------------------------------------------------------------- * PARAMETERS: cropthumb( resource $_file, output_resource $_newFile, int $new_w, int $new_h, * int $clip_x, int $clip_y, int $clip_x2, int $clip_y2 ) * --------------------------------------------------------------------------------------------------- * * 1) [resource $_file]: Existing image source. * Example: /images/thumbs/image.png * * 2) [output_resource $_newFile]: Output image source. * Example: /images/thumbs/sized/newimage.png * * 3) [int $new_w]: Maximum width of new image. * * 4) [int $new_h]: Maximum height of new image. * * 5) [int $clip_x]: Positive 'x' value. The amount of pixels being clipped from left of the image. * * 6) [int $clip_y]: Positive 'y' value. The amount of pixels being clipped from top of the image. * * 7) [int $clip_x2]: Positive 'x' value. The amount of pixels being clipped from right of the image. * * 8) [int $clip_y2]: Positive 'y' value. The amount of pixels being clipped from bottom of the image. * * --------------------------------------------------------------------------------------------------- * RETURN VALUE * --------------------------------------------------------------------------------------------------- * * Returns a string value - the source of the new image. * * --------------------------------------------------------------------------------------------------- * OTHER VARIABLES * --------------------------------------------------------------------------------------------------- * * [string $_PUB_DIR] {global scope variable}: Reference to a local scope variable defined as the Home * or Base directory of the domain. * Example Format: /home/user/public_html * Note: Either define $_PUB_DIR outside of function if using global or as a local variable * within the function. * * [string $HOME] {global scope variable}: Reference to a local scope variable defined as the Home or * Base directory of the domain. * Example Format: "../../.." or "http://thesite.com" * Note: Either define $HOME outside of function if using global or as a local variable within * the function. * * --------------------------------------------------------------------------------------------------- * OTHER FUNCTIONS * --------------------------------------------------------------------------------------------------- * * [string_return createthumb()]: * * SUMMARY: This functions is a stand-alone function and can be used separate from cropthumb() if * only resizing an image. It will use an images true aspect ratio to properly size the image to * fit in the new dimensions of ($new_w x $new_h). If it fails to create a new image, it will * return the source of a default image [user specified]. * * PARAMETERS: createthumb( resource $_file, output_resource $_newFile, int $new_w, int $new_h ) * * 1) [resource $_file]: Existing image source. * Example: /images/thumbs/image.png * * 2) [output_resource $_newFile]: Output image source. * Example: /images/thumbs/sized/newimage.png * * 3) [int $new_w]: Maximum width of new image. * * 4) [int $new_h]: Maximum height of new image. * * RETURN VALUE: Returns a string value - the source of the new image. * * * [bool_return thumbFldr()]: * * SUMMARY: If the output directory for the new image does not exist, this function will create all * directories leading up to the final directory. Setting permissions as 0755. * * PARAMETERS: thumbFldr( output_resource $_newFile ) * * 1) [output_resource $_newFile]: Output image source. * Example: /images/thumbs/sized/newimage.png * * RETURN VALUE: Bool value. * * --------------------------------------------------------------------------------------------------- * EXAMPLE USAGE * --------------------------------------------------------------------------------------------------- * * [string_return cropthumb()]: * * $_imgsource = cropthumb("/images/thumbs/mypic.jpg","/images/thumbs/sized/mynewpic.jpg",500,400,105, * 40,15,5); * echo "<img src='$_imgsource' border='0' id='thumbnail_1' title='Newly Cropped Thumbnail'/>"; * * [string_return createthumb()]: * * $_imgsource = createthumb("/images/thumbs/mypic.jpg","/images/thumbs/sized/mynewpic.jpg",500,400); * echo "<img src='$_imgsource' border='0' id='thumbnail_1' title='Newly Cropped Thumbnail'/>"; * */ //******************************************** [END] ***********************************************//
function thumbFldr($newFile) { global $_PUB_DIR; $dirs = explode('/',"/".str_replace(basename($newFile),'',$newFile)); $sDir = $_PUB_DIR;
if (preg_match("/jpg|jpeg/",$ext)) $src_img=imagecreatefromjpeg($file); else if (preg_match("/png/",$ext)) $src_img=imagecreatefrompng($file); else if (preg_match("/gif/",$ext)) $src_img=imagecreatefromgif($file);
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); if(imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y)) { if (preg_match("/png/",$ext)) imagepng($dst_img,$newFile); else if (preg_match("/jpg|jpeg/",$ext)) imagejpeg($dst_img,$newFile); else if (preg_match("/gif/",$ext)) imagegif($dst_img,$newFile);
imagedestroy($dst_img); imagedestroy($src_img);
return $HOME.$_newFile; } } }
//== !! CHANGE FIRST & SECOND PARAMETERS [user defined default image] !! ==// return createthumb("/images/avatar/nouser.png","/images/avatar/nouser/nouser$new_w".".png",$new_w,$new_h);
}
function cropthumb($_file,$_newFile,$new_w,$new_h,$clip_x,$clip_y,$clip_x2,$clip_y2) { global $_PUB_DIR,$HOME; $file = $_PUB_DIR.$_file; $newFile = $_PUB_DIR.$_newFile;
if (preg_match("/jpg|jpeg/",$ext)) $src_img=imagecreatefromjpeg($file); else if (preg_match("/png/",$ext)) $src_img=imagecreatefrompng($file); else if (preg_match("/gif/",$ext)) $src_img=imagecreatefromgif($file);
$dst_img=ImageCreateTrueColor(($old_x-$clip_x-$clip_x2),($old_y-$clip_y-$clip_y2)); if(imagecopyresampled($dst_img,$src_img,0,0,$clip_x,$clip_y,$old_x,$old_y,$old_x,$old_y)) { if (preg_match("/png/",$ext)) imagepng($dst_img,$newFile); else if (preg_match("/jpg|jpeg/",$ext)) imagejpeg($dst_img,$newFile); else if (preg_match("/gif/",$ext)) imagegif($dst_img,$newFile);
if (preg_match("/jpg|jpeg/",$ext)) $src_img=imagecreatefromjpeg($file); else if (preg_match("/png/",$ext)) $src_img=imagecreatefrompng($file); else if (preg_match("/gif/",$ext)) $src_img=imagecreatefromgif($file);
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); if(imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y)) { if (preg_match("/png/",$ext)) imagepng($dst_img,$newFile); else if (preg_match("/jpg|jpeg/",$ext)) imagejpeg($dst_img,$newFile); else if (preg_match("/gif/",$ext)) imagegif($dst_img,$newFile);
if (preg_match("/jpg|jpeg/",$ext)) $src_img=imagecreatefromjpeg($file); else if (preg_match("/png/",$ext)) $src_img=imagecreatefrompng($file); else if (preg_match("/gif/",$ext)) $src_img=imagecreatefromgif($file);
$dst_img=ImageCreateTrueColor(($old_x-$this->clip_x-$this->clip_x2),($old_y-$this->clip_y-$this->clip_y2)); if(imagecopyresampled($dst_img,$src_img,0,0,$this->clip_x,$this->clip_y,$old_x,$old_y,$old_x,$old_y)) { if (preg_match("/png/",$ext)) imagepng($dst_img,$newFile); else if (preg_match("/jpg|jpeg/",$ext)) imagejpeg($dst_img,$newFile); else if (preg_match("/gif/",$ext)) imagegif($dst_img,$newFile);
imagedestroy($dst_img); imagedestroy($src_img);
$this->_file=$this->_newFile; } } }
return $this->createthumb();
} }
$crop = new crop();
//CHANGE THE DEFINITIONS BELOW
$crop->_defaultImg = "/img/noimage.png"; //== Default Image ==// $crop->_file = "/img/background/try2.jpg"; //== Image being cropped ==// $crop->_newFile = "/img/background/sized/try5.jpg"; //== Output directory & name or newly cropped image ==// $crop->new_w = 1920; //== Maximum dimensions(Width) of new image (Image will fit in this dimension maintaining its ratio) ==// $crop->new_h = 416; //== Maximum dimensions(Height) of new image (Image will fit in this dimension maintaining its ratio) ==// $crop->clip_x = 50; //== How far to clip from left of the image ==// $crop->clip_y = 100; //== How far to clip from top of the image ==// $crop->clip_x2 = 254; //== How far to clip from right of the image ==// $crop->clip_y2 = 50; //== How far to clip from bottom of the image ==//