...

View Full Version : Image Cropping/Trimming and Resizing Tool (PHP & GD Library)



Cypher 101
03-10-2011, 07:15 AM
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

//********************************************* [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;

foreach($dirs as $fldr)
{
if($fldr != "")
{
$sDir .= "/".$fldr;
if(!is_dir($sDir))
{
if(!mkdir($sDir,0755)) return false;
chmod($sDir,0755);
}
}
}
return true;
}

function createthumb($_file,$_newFile,$new_w,$new_h)
{
global $_PUB_DIR,$HOME;
$file = $_PUB_DIR.$_file;
$newFile = $_PUB_DIR.$_newFile;

if(file_exists($file))
{
thumbFldr($_newFile);
$ext=strtolower(substr(strrchr($file,'.'),1));

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);

if(isset($src_img))
{
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);

$ratio_x = $old_x/$old_y;
$ratio_y = $old_y/$old_x;
$diff_x = $old_x-$new_w;
$diff_y = $old_y-$new_h;

if($old_x <= $new_w && $old_y <= $new_h)
return $HOME.$_file;
if($diff_x >= $diff_y)
{
$thumb_w = $new_w;
$thumb_h = round($ratio_y*$new_w);
}
else if($diff_y > $diff_x)
{
$thumb_w = round($ratio_x*$new_h);
$thumb_h = $new_h;
}
else
return $HOME.$_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(file_exists($file))
{
thumbFldr($_newFile);
$ext=strtolower(substr(strrchr($file,'.'),1));

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);

if(isset($src_img))
{
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);

$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);

imagedestroy($dst_img);
imagedestroy($src_img);
return createthumb($_newFile,$_newFile,$new_h,$new_w);
}
}
}

return createthumb($_file,$_newFile,$new_w,$new_h);

}
?>


TEST IT HERE:



<?php
$_PUB_DIR = $_SERVER['DOCUMENT_ROOT'];

//$HOME will = "" stating this file and the _thumb.php file is in your home directory
$HOME = "";

//INCLUDE THE THUMB FILE
include '_thumb.php';

//DEFINE THE IMAGE YOU WILL CROP
$image = "/img/background/try2.jpg";

//DEFINE THE OUTPUT IMAGE & DIRECTORY
$newimage = "/img/thumbs/sized/try3.jpg";

//DEFINE MAXIMUM IMAGE DIMENSIONS
$new_w = 500;
$new_h = 500;

//DEFINE HOW FAR TO CLIP FROM LEFT
$clip_x = 100;

//DEFINE HOW FAR TO CLIP FROM TOP
$clip_y = 100;

//DEFINE HOW FAR TO CLIP FROM RIGHT
$clip_x2 = 100;

//DEFINE HOW FAR TO CLIP FROM BOTTOM
$clip_y2 = 100;

$_imgsource = cropthumb($image,$newimage,$new_w,$new_h,$clip_x,$clip_y,$clip_x2,$clip_y2);

echo "<table cellpadding='0' cellspacing='0' align='center'>".
"<tr><td align='left'>".
"<div style='padding-bottom:5px; border-bottom:1px solid #000000;' align='left'><h2>Original Image</h2></div>".
"<div style='padding-top:5px;' align='left'>".
"<img src='$image' border='0' id='thumbnail_1' title='Old Thumbnail'/>".
"</div>".
"</td></tr><tr><td style='padding-top:10px;' align='left'>".
"<div style='padding-bottom:5px; border-bottom:1px solid #000000;' align='left'><h2>Cropped Image</h2></div>".
"<div style='padding-top:5px;' align='left'>".
"<img src='$_imgsource' border='0' id='thumbnail_2' title='Newly Cropped Thumbnail'/>".
"</div>".
"</td></tr></table>";
?>


IF YOU ARE FAMILIAR WITH CLASSES, USE AND TEST THE CROPPING TOOL WITH JUST THIS FILE:




<?php
class crop
{
//Reference to home directory
public $_PUB_DIR;
public $HOME;

public $_file;
public $_newFile;
public $_defaultImg;
public $new_w;
public $new_h;
public $clip_x;
public $clip_y;
public $clip_x2;
public $clip_y2;

public function __construct(){
$tmp = $this->_PUB_DIR = $_SERVER['DOCUMENT_ROOT'];
$this->HOME = "";
$dir = explode('/',$_SERVER['REQUEST_URI']);

for($x=1; $x<count($dir)-1; $x++)
{
$tmp .= "/".$dir[$x];
if(is_dir($tmp))
if($this->HOME=="") $this->HOME = "..";
else $this->HOME .= "/..";
}

$this->new_w = 100;
$this->new_h = 100;
}


public function exten($file)
{
return strtolower(substr(strrchr($file,'.'),1));
}

public function thumbFldr()
{
$dirs = explode('/',"/".str_replace(basename($this->_newFile),'',$this->_newFile));
$sDir = $this->_PUB_DIR;

foreach($dirs as $fldr)
{
if($fldr != "")
{
$sDir .= "/".$fldr;
if(!is_dir($sDir))
{
if(!mkdir($sDir,0755)) return false;
chmod($sDir,0755);
}
}
}
return true;
}

public function createthumb()
{
$file = $this->_PUB_DIR.$this->_file;
$newFile = $this->_PUB_DIR.$this->_newFile;

if(file_exists($file))
{
$this->thumbFldr($this->_newFile);
$ext=$this->exten($this->_file);

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);

if(isset($src_img))
{
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);

$ratio_x = $old_x/$old_y;
$ratio_y = $old_y/$old_x;
$diff_x = $old_x-$this->new_w;
$diff_y = $old_y-$this->new_h;

if($old_x <= $this->new_w && $old_y <= $this->new_h)
return $this->HOME.$this->_file;
if($diff_x >= $diff_y)
{
$thumb_w = $this->new_w;
$thumb_h = round($ratio_y*$this->new_w);
}
else if($diff_y > $diff_x)
{
$thumb_w = round($ratio_x*$this->new_h);
$thumb_h = $this->new_h;
}
else
return $this->HOME.$this->_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 $this->HOME.$this->_newFile;
}
}
}


$this->_file = $this->_defaultImg;
$defaultExt = ".".$this->exten($this->_defaultImg);
$this->_newFile = str_replace($defaultExt,'',$this->_file)."_".($this->new_w).$defaultExt;
return $this->createthumb();

}

public function cropthumb()
{
$file = $this->_PUB_DIR.$this->_file;
$newFile = $this->_PUB_DIR.$this->_newFile;

if(file_exists($file))
{
$this->thumbFldr($this->_newFile);
$ext=$this->exten($this->_file);

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);

if(isset($src_img))
{
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);

$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 ==//

echo "<img src='".$crop->cropthumb()."' border='0'/>";
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum