I have started to develope a GD class that will allow you to do things like watermark images and also add reflections to images.
There are to files at the moment one called reflection and one called watermark. This can be used to create the images.
You may be wondering about the reflection, because last time i wrote a tutorial on how to do it it came in 2 images. This new version has been changed and now will create it as a whole image so the reflection is actually part of the original image.
As far as i can see this should work for any image, give or take as i have not done it for every type but will do.
I shall also add a feature that creates thumbnails and stuff like that. I shall do that pretty soon. When i have some time.
PHP Code:
<?php
/**
*
* Copyright (c) 2009 TalkPHP
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*
* @author Dale Mooney(Rendair) ~ http://www.jooney.co.uk
*
**/
class gd
{
/**
* Constructor
*/
public function __contructor()
{
}
/**
* Add watermark to image
*/
public function addWaterMark($path)
{
$imagesource = $path;
$filetype = substr($imagesource,strlen($imagesource)-4,4);
$filetype = strtolower($filetype);
if($filetype == ".gif")
{
header('content-type: image/gif');
$image = @imagecreatefromgif($imagesource);
}
else if($filetype == ".jpg")
{
header('content-type: image/jpeg');
$image = @imagecreatefromjpeg($imagesource);
}
else if($filetype == ".png")
{
header('content-type: image/png');
$image = @imagecreatefrompng($imagesource);
}
$watermark = @imagecreatefrompng('images/watermark.png');
$imagewidth = imagesx($image);
$imageheight = imagesy($image);
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);
imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
imagejpeg($image,NULL,100);
imagedestroy($image);
imagedestroy($watermark);
}
/**
* Add Reflection to image
*/
public function addReflection($img)
{
$imgName = $src;
$size = getimagesize($img);
$imgImport = imagecreatefromjpeg($img);
$imgName_w = $size[0];
$imgName_h = $size[1];
$gradientHeight = 100;
// Create new blank image with sizes.
$background = imagecreatetruecolor($imgName_w, $gradientHeight);
$gradientColor = "255 255 255"; //White
$gradparts = explode(" ",$gradientColor); // get the parts of the colour (RRR,GGG,BBB)
$dividerHeight = 1;
$gradient_y_startpoint = $dividerHeight;
$gdGradientColor=ImageColorAllocate($background,$gradparts[0],$gradparts[1],$gradparts[2]);
$newImage = imagecreatetruecolor($imgName_w, $imgName_h);
for ($x = 0; $x < $imgName_w; $x++) {
for ($y = 0; $y < $imgName_h; $y++)
{
imagecopy($newImage, $imgImport, $x, $imgName_h - $y - 1, $x, $y, 1, 1);
}
}
// Add it to the blank background image
imagecopymerge ($background, $newImage, 0, 0, 0, 0, $imgName_w, $imgName_h, 100);
//create from a the image so we can use fade out.
$gradient_line = imagecreatetruecolor($imgName_w, 1);
// Next we draw a GD line into our gradient_line
imageline ($gradient_line, 0, 0, $imgName_w, 0, $gdGradientColor);
header("Content-type: image/jpeg");
$i = 0;
$transparency = 0; //from 0 - 100
while ($i < $gradientHeight) //create line by line changing as we go
{
imagecopymerge ($background, $gradient_line, 0,$gradient_y_startpoint, 0, 0, $imgName_w, 1, $transparency);
$i++;
$gradient_y_startpoint++;
if ($transparency == 100)
{
$transparency = 100;
}
else
{
$transparency = $transparency + 1;
}
}
// Set the thickness of the line we're about to draw
imagesetthickness ($background, $dividerHeight);
// Draw the line
imageline ($background, 0, 0, $imgName_w, 0, $gdGradientColor);
$firstPart = imagecreatetruecolor($imgName_w,($imgName_h + $gradientHeight));
imagecopymerge ($firstPart,$imgImport,0,0,0,0,$imgName_w,$imgName_h,100);
imagecopymerge ($firstPart,$background,0,$imgName_h,0,0,$imgName_w,$imgName_h,100);
imagejpeg($firstPart, '', 100); //100 being the quality of image 100 Max(Best)
imagedestroy($background);
imagedestroy(gradient_line);
imagedestroy(newImage);
}
/**
* Create a thumbnail of an image
*/
public function thumbnail($src_img,$new_w)
{
header('content-type: image/jpeg');
$src_img = imagecreatefromjpeg($src_img);
$old_x= imageSX($src_img);
$old_y= imageSY($src_img);
if ($old_x > $old_y)
{
$thumb_w=$new_w;
$thumb_h=$old_y*($new_w/$old_x);
}
if ($old_x < $old_y)
{
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_w;
}
if ($old_x == $old_y)
{
$thumb_w=$new_w;
$thumb_h=$new_w;
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
imagejpeg($dst_img, '', 100); //100 being the quality of image 100 Max(Best)
imagedestroy($dst_img);
}
/**
* Create a thumbnail and save the results.
*/
public function saveThumbnail($src_img,$new_w,$location)
{
//Coming soon
}
/**
* Rotate image
*/
public function rotateImage($image,$rotate)
{
header('content-type: image/jpeg');
$src_img = imagecreatefromjpeg($image);
$black = imagecolorallocate($src_img,255,255,255);
$rotate = imagerotate($src_img,$rotate,$black);
imagejpeg($rotate,'',100);
imagedestroy($rotate);
}
}
?>