Thread: The GD class
View Single Post
Old 12-28-2008, 06:05 PM   #1 (permalink)
Rendair
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default The GD class

Hey all

I have started to develope a GD class that will allow you to do things like watermark images and also add reflections to images.

You can view an example of the results: http://www.mypubquiz.co.uk/showwatermark.php

How it works
There are to files at the moment one called reflection and one called watermark. This can be used to create the images.

for instance the following will produce a watermark over the image.

PHP Code:
<img src="watermark.php?file=http://www.chromewaves.net/images/interface/20061020batmanVsWolverine.jpg" /> 
And the following will produce a reflection off the image.

PHP Code:
<img src="reflection.php?file=http://www.chromewaves.net/images/interface/20061020batmanVsWolverine.jpg" /> 
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.

The two files like reflection.php file are just simple like the following.

PHP Code:
<?php
include("includes/gd.class.php");
$gd = new gd();
echo 
$gd->addReflection($_GET["file"]);
?>
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.

Further Features

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$startheight00$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$y11);
            }
        }
        
        
// Add it to the blank background image
        
imagecopymerge ($background$newImage0000$imgName_w$imgName_h100);  
        
        
//create from a the image so we can use fade out.
        
$gradient_line imagecreatetruecolor($imgName_w1);
        
        
// Next we draw a GD line into our gradient_line
        
imageline ($gradient_line00$imgName_w0$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_line0,$gradient_y_startpoint00$imgName_w1$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 ($background00$imgName_w0$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_ximageSX($src_img);
        
$old_yimageSY($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);
    }    
}
?>
__________________
www.jooney.co.uk - the online portfolio

Last edited by Rendair : 12-31-2008 at 12:47 PM.
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote