TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-28-2008, 06:05 PM   #1 (permalink)
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
Old 12-28-2008, 06:27 PM   #2 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

How about keeping it in one file?
And have it like:
PHP Code:
<img src="sourceImage.php?add=watermark&file=http://www.chromewaves.net/images/interface/20061020batmanVsWolverine.jpg" /> 
Also, there could be a way of doing multiple tasks in one link like such:
PHP Code:
<img src="sourceImage.php?create=thumb&add=reflection|watermark&file=http://www.chromewaves.net/images/interface/20061020batmanVsWolverine.jpg" /> 
(this is ofcourse clearly theoretical, but you get the point!
__________________
Tanax is offline  
Reply With Quote
Old 12-28-2008, 06:31 PM   #3 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Oh, and I have a really great thumbnail function in an image gallery class that I started working on before.. tell me if you want it.
The thing about it that's great is the mathematical logics in it, that allows you to specify a max width and height, and it will create thumb if the orig file exceeds that criterias. And it keeps the proportions of the orig file aswell!

Anyways, tell me if you want it. You could prob tweek it if you find it failing this script somewhere.
__________________
Tanax is offline  
Reply With Quote
Old 12-28-2008, 07:12 PM   #4 (permalink)
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

Yeah sure send it to me. I did have one somewhere but cant find it.
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-28-2008, 10:00 PM   #5 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

This function creates a thumbnail even if it's below the max size.. but you can change that.

php Code:
/**
     * Creates thumbnail of image and moves to thumbnail dir
     *
     * @return void
     */

    private function createThumb()
    {
       
        switch($_FILES[$this->form_inputName]['type'])
        {
           
            case 'image/jpeg':
               
                $srcimage = imagecreatefromjpeg($this->image_bigPath . $this->file_name);
                break;

            case 'image/gif':

                $srcimage = imagecreatefromgif($this->image_bigPath . $this->file_name);
                break;

            case 'image/png':
               
                $srcimage = imagecreatefrompng($this->image_bigPath . $this->file_name);
                break;
           
            default:
               
                $srcimage = imagecreatefromjpeg($this->image_bigPath . $this->file_name);
               
        }
       

        $srcimagex = imagesx($srcimage);
        $srcimagey = imagesy($srcimage);
       
        if($srcimagex > $this->thumb_width || $srcimagey > $this->thumb_height)
        {
           
            $diffx = $srcimagex - $this->thumb_width;
            $diffy = $srcimagey - $this->thumb_height;
           
            if($srcimagex > $srcimagey || $srcimagey > $srcimagex)
            {
               
                if($diffx > $diffy)
                {
                   
                    $ratio = $this->thumb_width / $srcimagex;
                   
                }
               
                elseif($diffy > $diffx)
                {
                   
                    $ratio = $this->thumb_height / $srcimagey;
                   
                }
               
                else
                {
                   
                    $ratio = $this->thumb_width / $srcimagex;
                   
                }
               
            }
           
            else
            {
               
                if($this->thumb_width > $this->thumb_height || $this->thumb_height > $this->thumb_width)
                {
                   
                    if($diffx > $diffy)
                    {
                       
                        $ratio = $this->thumb_width / $srcimagex;
                       
                    }
                   
                    elseif($diffy > $diffx)
                    {
                       
                        $ratio = $this->thumb_height / $srcimagey;
                       
                    }
                   
                    else
                    {
                       
                        $ratio = $this->thumb_width / $srcimagex;
                       
                    }
                   
                }
               
                else
                {
                   
                    $ratio = $this->thumb_height / $srcimagey;
                   
                }
               
            }

            $thumbwidth = $srcimagex * $ratio;
            $thumbheight = $srcimagey * $ratio;
           
            $thumbnail = imagecreatetruecolor($thumbwidth, $thumbheight);
            imagecopyresampled($thumbnail, $srcimage, 0, 0, 0, 0, $thumbwidth, $thumbheight, $srcimagex, $srcimagey);
            $success = imagejpeg($thumbnail, $this->image_thuPath . $this->file_name, 75);
           
            if($success)
            {
               
                $this->thumb_creation = 1;
               
            }
           
            else throw new Exception('Thumbnail creation did not work.');         

        }
       
        else
        {
           
            if(!imagejpeg($srcimage, $this->image_thuPath . $this->file_name, 75))
            {
                   
                throw new Exception('An error occured when creating a thumbnail.');
                       
            }
           
            else return true;
           
        }
       
    }
__________________
Tanax is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
A Generic Singleton Base Class Theo Advanced PHP Programming 7 08-18-2008 02:25 AM
[Tutorial] Basic tutorial about class basics Tanax Absolute Beginners 14 07-24-2008 01:37 PM
PHP5 Classes A to Z Part 1 quantumkangaroo Advanced PHP Programming 11 04-01-2008 04:21 AM
Tutorial: PHP and OOP, a beginners guide Village Idiot Tips & Tricks 0 09-06-2007 04:23 PM


All times are GMT. The time now is 08:00 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design