<?php
# Version 1.0
/**
image path, save path, max size (counts for width or height whatever hits first), quality
$f->thumb('images/p-016-small.jpg','images/p-016-small_t.jpg',250,85)
//Watermark arguments listed below this line
#1. Original img source
#2. Source for image to be created to
#3. the watermark to use
#4. image output quality 100 = BEST, 0 = WORST
#5. Watermark size relative to the original image
# Example:
# 1 = Watermark image goes from edge to edge - edge padding
# .75 = Watermark is 75% across image
# .33 = Watermark is 33% across image
#6. Vertical Position of watermark
# (top,center,bottom)
#7. Horizontal Position of watermark
# (left, center, right)
#8. Edge Padding if watermark to image ratio is 1 or greater
#9. Save image as when done (jpeg or png) only
$f->waterMark('images/p-016-small.jpg','images/p-016-small_w.jpg','images/watermark.png',100)
This class requires the GD library
Note: GIF support was removed from the GD library in Version 1.6,
and added back in Version 2.0.28. This function is not available between these versions.
**/
class image_handle
{
##-----------------------------------------------------------------------------##
## Watermarking an Image ##
##-----------------------------------------------------------------------------##
//Watermark arguments listed below this line
#1. Original img soruce
#2. Soruce for image to be created to
#3. the watermark to use
#4. image output quality 100 = BEST, 0 = WORST
#5. Watermark size relative to the original image
# Example:
# 1 = Watermark image goes from edge to edge - edge padding
# .75 = Watermark is 75% across image
# .33 = Watermark is 33% across image
#6. Vertical Position of watermark
# (top,center,bottom)
#7. Horizontal Position of watermark
# (left, center, right)
#8. Edge Padding if wtaermark to image ratio is 1 or greater
#9. Save image as when done (jpeg or png) only
function watermark($original = NULL, $watermarked = NULL, $default_watermark = NULL, $quality=100,
$wm_size=.5, $v_pos = 'center', $h_pos = 'center', $edgePadding=10, $saveas = 'jpeg')
{
if( file_exists( $original ) && is_file( $original ) &&
file_exists( $default_watermark ) && is_file( $default_watermark ) )
{
// file upload success
$size = getimagesize($original);
// 2 = JPEG, 3 = PNG
if($size[2] == 2 || $size[2] == 3)
{
$target = $watermarked;
$watermark = $default_watermark;
$wmTarget = $watermark.'.tmp';
// Get original image size
$origInfo = getimagesize($original);
$origWidth = $origInfo[0];
$origHeight = $origInfo[1];
// Get watermark image size
$waterMarkInfo = getimagesize($default_watermark);
$waterMarkWidth = $waterMarkInfo[0];
$waterMarkHeight = $waterMarkInfo[1];
// watermark scaling info
$waterMarkDestWidth = round($origWidth * $wm_size);
$waterMarkDestHeight = round($origHeight * $wm_size);
if($wm_size==1)
{
$waterMarkDestWidth -= (2 * $edgePadding);
$waterMarkDestHeight -= (2 * $edgePadding);
}
// Scale the watermarked image
image_handle::resize_png_image($watermark,$waterMarkDestWidth,$waterMarkDestHeight,$wmTarget);
// get the size info for this watermark.
$wmInfo = getimagesize($wmTarget);
$waterMarkDestWidth = $wmInfo[0];
$waterMarkDestHeight = $wmInfo[1];
$differenceX = $origWidth - $waterMarkDestWidth;
$differenceY = $origHeight - $waterMarkDestHeight;
// where to place the watermark?
switch($h_pos)
{
// find the X coord for placement
case 'left':
$placementX = $edgePadding;
break;
case 'center':
$placementX = round($differenceX / 2);
break;
case 'right':
$placementX = $origWidth - $waterMarkDestWidth - $edgePadding;
break;
}
switch($v_pos)
{
// find the Y coord for placement
case 'top':
$placementY = $edgePadding;
break;
case 'center':
$placementY = round($differenceY / 2);
break;
case 'bottom':
$placementY = $origHeight - $waterMarkDestHeight - $edgePadding;
break;
}
//If image is png
if($size[2]==3)
$resultImage = imagecreatefrompng($original);
//If image is JPEG
else
$resultImage = imagecreatefromjpeg($original);
imagealphablending($resultImage, TRUE);
$finalWaterMarkImage = imagecreatefrompng($wmTarget);
$finalWaterMarkWidth = imagesx($finalWaterMarkImage);
$finalWaterMarkHeight = imagesy($finalWaterMarkImage);
// Create watermarked image
imagecopy($resultImage,$finalWaterMarkImage,$placementX,$placementY,
0,0,$finalWaterMarkWidth,$finalWaterMarkHeight);
// Save as PNG
if($saveas=='png')
{
imagealphablending($resultImage,FALSE);
imagesavealpha($resultImage,TRUE);
imagepng($resultImage,$target);
// Save as JPEG
} else
{
imagejpeg($resultImage,$target,$quality);
}
imagedestroy($resultImage);
imagedestroy($finalWaterMarkImage);
return true;
} else
return false;
} else
return false;
}
##-----------------------------------------------------------------------------##
## Create Thumbnail From JPG/JPEG/PNG/GIF ##
##-----------------------------------------------------------------------------##
/*
$pic is the path and file of the image
$thumb is the destination path and file
$asize is the max size for the thumb (just one number; applies to width and height)
$force_square forces the thumbnail to be the exact desired dimensions
This will distort an image if nessassary
EX: thumb('images/picture.jpg','images/thumbs/picture.jpg',250);
*/
function thumb($pic = NULL, $thumb = NULL, $asize = 150, $quality = 80, $force_square = false)
{
if( file_exists( $pic ) && is_file( $pic ) && $thumb != NULL )
{
if($size = @getimagesize($pic))
{
// If image in smaller then desired thumb then leave alone
if($size[0] <= $asize && $size[1] <= $asize)
{
return true;
// Image needs to have a thumb created
}else
{
switch($size[2])
{
case 1: $simg = imagecreatefromgif ($pic); break; // GIF
case 2: $simg = imagecreatefromjpeg ($pic); break; // JPG
case 3: $simg = imagecreatefrompng ($pic); break; // PNG
// Invalid file type
default: return false;
}
// If height is greater then width
if ($size[1] > $size[0])
{
$zoom = ($asize / $size[1]); // (Desired size / actual height)
$newheight = $asize; // Height Is Equal To Max Height
$newwidth = floor ($size[0] * $zoom); // Creates The New Width
// If width is greater then height or both are equal
}else
{
$zoom = ($asize / $size[0]); // (Desired size / actual width)
$newwidth = $asize; // Width Is Equal To Max Width
$newheight = floor ($size[1] * $zoom); // Creates The New Height
}
// Forces the thumbnail to be the exact desired dimensions
// This will distort an image if nessassary
if($force_square == true)
{
$newheight = $asize;
$newwidth = $asize;
}
// Create a new true color image
$dimg = ImageCreateTrueColor($newwidth, $newheight);
// Convert a true color image to a palette image
imagetruecolortopalette($simg, false, 256);
// Find out the number of colors in an image's palette
$palsize = ImageColorsTotal($simg);
// Counting Colors In The Image
for ($i = 0; $i < $palsize; $i++)
{
// This returns an associative array with red, green, blue
// and alpha keys that contain the appropriate values for the specified color index.
$colors = ImageColorsForIndex($simg, $i); // Number Of Colors Used
// Tell The Server What Colors This Image Will Use
ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);
}
//$dimg = ImageCreateTrueColor($newwidth,$newheight);
// Copy and resize part of an image with resampling
imagecopyresampled($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $size[0], $size[1]);
switch($size[2])
{
case 1: if( imagegif ($dimg, $thumb) ) $make = true; break; //GIF
case 2: if( imagejpeg ($dimg, $thumb,$quality) ) $make = true; break; //JPG
case 3: if( imagepng ($dimg, $thumb) ) $make = true; break; //PNG
}
@imagedestroy($simg); // Destroy The Temporary Image
@imagedestroy($dimg); // Destroy The Temporary Image
//Thumbnail was made
if(isset($make))
return true;
//Thumbnail was not made
else
return false;
}
}
//Invalid file
else
return false;
}else
return false;
}
##-----------------------------------------------------------------------------##
## Resize watermark for image ##
##-----------------------------------------------------------------------------##
//This is used in the watermark function, it resizes the watermark to be used
//according to the sizeof watermark in relation to orig img you want
function resize_png_image($img,$newWidth,$newHeight,$target)
{
$srcImage=imagecreatefrompng($img);
if($srcImage=='')
return FALSE;
$srcWidth=imagesx($srcImage);
$srcHeight=imagesy($srcImage);
$percentage=(double)$newWidth/$srcWidth;
$destHeight=round($srcHeight*$percentage)+1;
$destWidth=round($srcWidth*$percentage)+1;
if($destHeight > $newHeight)
{
// if the width produces a height bigger than we want, calculate based on height
$percentage=(double)$newHeight/$srcHeight;
$destHeight=round($srcHeight*$percentage)+1;
$destWidth=round($srcWidth*$percentage)+1;
}
$destImage=imagecreatetruecolor($destWidth-1,$destHeight-1);
if(!imagealphablending($destImage,FALSE))
return FALSE;
if(!imagesavealpha($destImage,TRUE))
return FALSE;
if(!imagecopyresampled($destImage,$srcImage,0,0,0,0,$destWidth,$destHeight,$srcWidth,$srcHeight))
return FALSE;
if(!imagepng($destImage,$target))
return FALSE;
imagedestroy($destImage);
imagedestroy($srcImage);
return TRUE;
}
}
?>