<?
/**************************************
* Smart Photo Resizer *
* By: Greg Schoppe *
* www.gschoppe.com *
* 04/01/2010 *
* Resizes, Crops, Tints, watermarks *
* Requires GD *
**************************************/
/*
function smart_resize($file,
$width = 0,
$height = 0,
$proportional = 'absolute', // absolute | proportional | proportionalcrop | crop
$padding = 'none'; // none | transparent | #xxxxxx
$filter = 'none', // none | bw | sepia | washout | #xxxxxx
$overlay = 'none', // define watermark, or thumbnail overlay as path
$overlay_position = 'br', // define watermark position - (t|m|b)(l|c|r) eg tl = top left
$output = 'file', // file | browser | return
$outputFile = 'none', // none or directory path
$delete_original = true,
$use_linux_commands = false ) {
*/
function validateHexColor($color) {
$color = substr($color, 0, 7);
return preg_match('/#[0-9a-fA-F]{6}/', $color);
}
function imagefilterhue($im,$r,$g,$b){
$rgb = $r+$g+$b;
$col = array($r/$rgb,$b/$rgb,$g/$rgb);
$height = imagesy($im);
$width = imagesx($im);
for($x=0; $x<$width; $x++){
for($y=0; $y<$height; $y++){
$rgb = ImageColorAt($im, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$newR = $r*$col[0] + $g*$col[1] + $b*$col[2];
$newG = $r*$col[2] + $g*$col[0] + $b*$col[1];
$newB = $r*$col[1] + $g*$col[2] + $b*$col[0];
imagesetpixel($im, $x, $y,imagecolorallocate($im, $newR, $newG, $newB));
}
}
}
function imagesepia( $img ) {
$total = imagecolorstotal( $img );
for ( $i = 0; $i < $total; $i++ ) {
$index = imagecolorsforindex( $img, $i );
$red = ( $index["red"] * 0.393 + $index["green"] * 0.769 + $index["blue"] * 0.189 ) / 1.351;
$green = ( $index["red"] * 0.349 + $index["green"] * 0.686 + $index["blue"] * 0.168 ) / 1.203;
$blue = ( $index["red"] * 0.272 + $index["green"] * 0.534 + $index["blue"] * 0.131 ) / 2.140;
imagecolorset( $img, $i, $red, $green, $blue );
}
}
function smart_resize($file,
$width = 0,
$height = 0,
$proportional = 'absolute', // absolute | proportional | proportionalcrop | crop
$padding = 'none', // none | transparent | #xxxxxx
$filter = 'none', // none | bw | sepia | washout | #xxxxxx
$overlay = 'none', // define watermark, or thumbnail overlay as path
$overlay_position = 'br', // define watermark position - (t|m|b)(l|c|r) eg tl = top left
$output = 'file', // file | browser | return
$outputFile = 'none', // none or directory path
$delete_original = true,
$use_linux_commands = false ) {
if ( $height <= 0 && $width <= 0 ) return false;
# Setting defaults and meta
$info = getimagesize($file);
$image = '';
$final_width = 0;
$final_height = 0;
list($width_old, $height_old) = $info;
$cropIt = false;
# Calculating proportionality
switch ( $proportional ) {
case 'proportional' :
if ($width == 0) $factor = $height/$height_old;
elseif ($height == 0) $factor = $width/$width_old;
else $factor = min( $width / $width_old, $height / $height_old );
$final_width = round( $width_old * $factor );
$final_height = round( $height_old * $factor );
break;
case 'proportionalcrop' :
if ($width == 0) $factor = $height/$height_old;
elseif ($height == 0) $factor = $width/$width_old;
else $factor = max( $width / $width_old, $height / $height_old );
$final_width = round( $width_old * $factor );
$final_height = round( $height_old * $factor );
$cropIt = true;
break;
case 'crop' :
$final_width = $width_old;
$final_height = $height_old;
$cropIt = true;
break;
default:
$final_width = ( $width <= 0 ) ? $width_old : $width;
$final_height = ( $height <= 0 ) ? $height_old : $height;
}
# Loading image to memory according to type
switch ( $info[2] ) {
case IMAGETYPE_GIF: $image = imagecreatefromgif($file); break;
case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($file); break;
case IMAGETYPE_PNG: $image = imagecreatefrompng($file); break;
default: return false;
}
$image_resized = imagecreatetruecolor( $final_width, $final_height );
if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
$transparency = imagecolortransparent($image);
if ($transparency >= 0) {
$transparent_color = imagecolorsforindex($image, $trnprt_indx);
$transparency = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
imagefill($image_resized, 0, 0, $transparency);
imagecolortransparent($image_resized, $transparency);
}
elseif ($info[2] == IMAGETYPE_PNG) {
imagealphablending($image_resized, false);
$color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
imagefill($image_resized, 0, 0, $color);
imagesavealpha($image_resized, true);
}
}
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);
//filter Code
// none | bw | sepia | washout | #xxxxxx
switch ( $filter ) {
case 'bw' :
//make it bw
imagefilter($image_resized, IMG_FILTER_GRAYSCALE);
break;
case 'sepia' :
//make it sepia
imagefilter($image_resized, IMG_FILTER_GRAYSCALE);
imagefilter($image_resized, IMG_FILTER_COLORIZE, 90, 55, 30);
break;
case 'washout' :
//wash it out <-- still unimplemented
break;
default :
if (validateHexColor($filter))
{
//colorize to #xxxxxx
$cA = sscanf($filter, '#%2x%2x%2x');
imagefilterhue($image_resized, $cA[0], $cA[1], $cA[2]);
}
}
//padding code
if($padding != 'none') {
$image_resized2 = imagecreatetruecolor( $width, $height );
if( ($padding == 'transparent') && ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) )
{
$transparency = imagecolortransparent($image);
if ($transparency >= 0) {
$transparent_color = imagecolorsforindex($image, $trnprt_indx);
$transparency = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
imagefill($image_resized, 0, 0, $transparency);
imagecolortransparent($image_resized, $transparency);
}
elseif ($info[2] == IMAGETYPE_PNG) {
imagealphablending($image_resized, false);
$color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
imagefill($image_resized, 0, 0, $color);
imagesavealpha($image_resized, true);
}
}
elseif (validateHexColor($padding))
{
$cA = sscanf($padding, '#%2x%2x%2x');
$color = imagecolorallocate($image_resized2, $cA[0], $cA[1], $cA[2]);
imagefill($image_resized2, 0, 0, $color);
}
imagecopyresampled($image_resized2, $image_resized, 0, 0, 0, 0, $final_width, $final_height, $final_width, $final_height);
$image_resized = $image_resized2;
}
//crop code for proportionalcrop and crop mode
if($cropIt)
{
// assuming that $img holds the image with which you are working
$img_width = imagesx($image_resized);
$img_height = imagesy($image_resized);
// Starting point of crop
$tlx = floor($img_width / 2) - floor ($width / 2);
$tly = floor($img_height / 2) - floor($height / 2);
// Adjust crop size if the image is too small
if ($tlx < 0)
{
$tlx = 0;
}
if ($tly < 0)
{
$tly = 0;
}
if (($img_width - $tlx) < $width)
{
$width = $img_width - $tlx;
}
if (($img_height - $tly) < $height)
{
$height = $img_height - $tly;
}
$image_resized2 = imagecreatetruecolor($width, $height);
imagecopy($image_resized2, $image_resized, 0, 0, $tlx, $tly, $width, $height);
$image_resized = $image_resized2;
}
//bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
# Taking care of original, if needed
if ( $delete_original ) {
if ( $use_linux_commands ) exec('rm '.$file);
else @unlink($file);
}
//insert overlay Code
if($overlay != 'none')
{
// Find base image size
$swidth = imagesx($image_resized);
$sheight = imagesy($image_resized);
// Turn on alpha blending
imagealphablending($image_resized, true);
// Create overlay image
$overlay = imagecreatefrompng($overlay);
// Get the size of overlay
$owidth = imagesx($overlay);
$oheight = imagesy($overlay);
// define posion for top left corner $oposx, $oposy
switch ( substr($overlay_position, 0, 1) ) {
case 't' :
$oposy= 0;
break;
case 'm' :
$oposy= ($sheight - $oheight) / 2;
break;
default :
$oposy= $sheight - $oheight;
}
switch ( substr($overlay_position, 1, 1) ) {
case 'l' :
$oposx= 0;
break;
case 'c' :
$oposx= ($swidth - $owidth) / 2;
break;
default :
$oposx= $swidth - $owidth;
}
// Overlay watermark
imagecopy($image_resized, $overlay, $oposx, $oposy, 0, 0, $owidth, $oheight);
}
# Preparing a method of providing result
switch ( strtolower($output) ) {
case 'browser':
$mime = image_type_to_mime_type($info[2]);
header("Content-type: $mime");
$output = NULL;
break;
case 'file':
if($outputFile == 'none')
$output = $file;
else
$output = $outputFile;
break;
case 'return':
return $image_resized;
break;
default:
break;
}
# Writing image according to type to the output destination
switch ( $info[2] ) {
case IMAGETYPE_GIF: imagegif($image_resized, $output); break;
case IMAGETYPE_JPEG: imagejpeg($image_resized, $output); break;
case IMAGETYPE_PNG: imagepng($image_resized, $output); break;
default: return false;
}
return true;
}
?>