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 (16) Thread Tools Search this Thread Display Modes
Old 12-11-2007, 10:58 PM   15 links from elsewhere to this Post. Click to view. #1 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 318
Thanks: 18
Rendair is on a distinguished road
Default Image Reflections in PHP

Now this is a pretty simple tutorial on how to create a nice simple reflection on images. This won't show you how to make little ripples just yet.

You can view an example of what this script can do. HERE


Firstly we need to define the image we are reflecting and get its size. (Width & Height)

PHP Code:

$imgName 
$src;
$size getimagesize("$imgName"); 
Now we want to load the image into GD and also set some values like placing the height and width of the image into variables, but also set the height of the actual reflection image.

PHP Code:
$imgImport imagecreatefromjpeg($imgName);


$imgName_w $size[0];
$imgName_h $size[1];


$gradientHeight 100
Now we want to actually create the image background that the reflection will be placed on

PHP Code:
// Create new blank image with sizes.
$background imagecreatetruecolor($imgName_w$gradientHeight); 
Now we need to set some variables up for the background colour of the actual reflect and also the line height that will devide the reflection from the actual image itself.

PHP Code:
$gradientColor "255 255 255"//White
$gradparts explode(" ",$gradientColor); // get the parts of the  colour (RRR,GGG,BBB)
$dividerHeight 1
Now we need to set a starting point for the reflection so it doesnt overlap and also assign a colour to a variable

PHP Code:
$gradient_y_startpoint $dividerHeight;
$gdGradientColor=ImageColorAllocate($background,$gradparts[0],$gradparts[1],$gradparts[2]); 
Now we need to make an exact copy of the large image for use in the reflect and copy the parts we need (the bottom) then add it to the blank background.

PHP Code:
$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); 
Now we want to create the reflection.

PHP Code:

//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);


$i 0;
$transparency 30//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 
                {
                             
// this will determing the height of the
                                     //reflection. The higher the number, the smaller the reflection. 
                                     //1 being the lowest(highest reflection)
                    
$transparency $transparency 1

                }
        
    } 
Now we can set a header type for image and create that divider line that will be under the big image.

PHP Code:
header("Content-type: image/jpeg");

// Set the thickness of the line we're about to draw
imagesetthickness ($background$dividerHeight);

// Draw the line
imageline ($background00$imgName_w0$gdGradientColor); 
Now it is a matter of displaying the image and clearing some images from the memory to save some space.

PHP Code:
imagejpeg($background''100); //100 being the quality of image 100 Max(Best)
imagedestroy($background);
imagedestroy(gradient_line);
imagedestroy(newImage); 
Usage

In a new file. Place the full size image you want to reflect on the page and under that image, place the following code.

PHP Code:
<img src=wetfloor.php?src=linktoimage.jpg
This will then place the reflection of the image under it. wetfloor.php is what i called it, but you can call it what you want.
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
The Following 8 Users Say Thank You to Rendair For This Useful Post:
Gurnk (12-11-2007), hello-world (04-01-2009), ibndawood (12-13-2007), Matt83 (12-12-2007), Orc (12-11-2007), ReSpawN (12-12-2007), sketchMedia (12-13-2007), Village Idiot (12-13-2007)
Old 12-12-2007, 09:39 AM   1 links from elsewhere to this Post. Click to view. #2 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Awesome! This was one of THE tutorials I have been waiting for!

Thanks mate!
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 12-12-2007, 09:45 AM   #3 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 318
Thanks: 18
Rendair is on a distinguished road
Default

Your welcome
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-12-2007, 10:35 AM   #4 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Next to this, is there also a way to modify is the was Techzine did it? Like giving the image a gradient (wet floor) effect in the same image? You've got 2 images right now with a small white line.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 12-12-2007, 10:43 AM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,215
Thanks: 90
Wildhoney is on a distinguished road
Default

Any chance you can put up a before and after image? Save me copying down all the code. Apart from the absence of an after image though, it reads very well! Very useful!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-12-2007, 10:45 AM   #6 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Indeed it does. I am getting an error at the moment. Simply no image is showing up. I named the file image.php and the file that I am echo'ing it in pic.php.

Doesn't seem to be working, or I miscopied the code.

My current script:
PHP Code:
$imgName $_GET[src];
$size getimagesize("$imgName");  

$imgImport imagecreatefromjpeg($imgName);


$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);


$i 0;
$transparency 30//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 
                {
                             
// this will determing the height of the
                                     //reflection. The higher the number, the smaller the reflection. 
                                     //1 being the lowest(highest reflection)
                    
$transparency $transparency 1

                }
        
    } 

header("Content-type: image/jpeg");

// Set the thickness of the line we're about to draw
imagesetthickness ($background$dividerHeight);

// Draw the line
imageline ($background00$imgName_w0$gdGradientColor);  

imagejpeg($background''100); //100 being the quality of image 100 Max(Best)
imagedestroy($background);
imagedestroy(gradient_line);
imagedestroy(newImage); 
Found it, you need to work in $_GET[src] instead of $src since you are calling it from an other file. :)
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 12-12-2007, 10:57 AM   #7 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,215
Thanks: 90
Wildhoney is on a distinguished road
Default

Post an after image, Respawn!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-12-2007, 11:15 AM   #8 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Will do Honey!

My Code:
image.php
PHP Code:
<?php

$imgName 
$_GET[src];
$size getimagesize("$imgName");  

$imgImport imagecreatefromjpeg($imgName);

$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);

$i 0;
$transparency 30//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 
                {
                             
// this will determing the height of the
                                     //reflection. The higher the number, the smaller the reflection. 
                                     //1 being the lowest(highest reflection)
                    
$transparency $transparency 1

                }
        
    } 

header("Content-type: image/jpeg");

// Set the thickness of the line we're about to draw
imagesetthickness ($background$dividerHeight);

// Draw the line - me do not likey the liney
//imageline ($background, 0, 0, $imgName_w, 0, $gdGradientColor);

imagejpeg($background''100); //100 being the quality of image 100 Max(Best)
imagedestroy($background);
imagedestroy(gradient_line);
imagedestroy(newImage);  

?>
pic.php
PHP Code:
<style>body { background-color: white; }</style>

<?php

// Image name, handy for doubleclick-paste.
$image 'wii_zelda';

// Refix the .jpg extention
$image $image.'.jpg';

// Echo it out.
echo '<img src="'.$image.'"><br /><img src="image.php?src='.$image.'">';

?>
Original Source:


Result:
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
The Following 2 Users Say Thank You to ReSpawN For This Useful Post:
Alan @ CIT (01-22-2008), Wildhoney (12-12-2007)
Old 12-12-2007, 12:59 PM   #9 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 318
Thanks: 18
Rendair is on a distinguished road
Default

Yeah at the moment it creates a new image, but i am working on a class that will create it as one image hopefully.
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-12-2007, 01:12 PM   #10 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,215
Thanks: 90
Wildhoney is on a distinguished road
Default

Oh now that is smart! Even got the gradient there which is impressive stuff.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-13-2007, 08:43 AM   #11 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

It really is! What would be even more awesome, is to intergrade it like this.

1. You upload an image, let's stay, a wallpaper.
2. The script creates a thumb to display on the frontpage.
3. Your script, gives it the extra designerstyle edge by creating a wet-floor effect and creating a seperate image.
- You've got 3 images right now. The uploaded original, the thumb and your version.
4. Your script saves the file OVER (overwrite) the thumb and finito! You've got a thumbnail with a wet-floor effect.

Would be a nice project huh? If you need any help, I'm all yours (for what I CAN do).
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
The Following User Says Thank You to ReSpawN For This Useful Post:
Rendair (12-13-2007)
Old 12-13-2007, 10:57 AM   #12 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 318
Thanks: 18
Rendair is on a distinguished road
Default

Yes indeed that sounds like a good project and i am up for giving it a shot. If you have any MSN or something like that let me know, so you are able to help that way.
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-13-2007, 12:07 PM   #13 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Yeah I do. Feel free to add me @ markernstproductions@gmail.com.
Noticed that you're UK, so our time diffirence is only 1 hour I believe (GTM+1 here).

Tell me what to do and let's get crackin'. I'm new to the whole image thing in PHP, so that's going to be a challenge. BUT I am up for it!
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 12-13-2007, 07:51 PM   #14 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 318
Thanks: 18
Rendair is on a distinguished road
Default

If you check out my website now i have updated it using the image reflection as it just gave me such a great idea lol.

The images at the top will change everytime, sometimes displays the same image, but still working on that lol.

http://www.dalemooney.com/
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-13-2007, 10:20 PM   #15 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Ah very nice Dale, looks very professional. I like it a lot.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 01-22-2008, 09:27 AM   #16 (permalink)
The Visitor
 
cybeerboy's Avatar
 
Join Date: Jan 2008
Posts: 1
Thanks: 0
cybeerboy is on a distinguished road
Default

Thanks! I tried it already I love it.
__________________
www.the-device.net
cybeerboy is offline  
Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/advanced-php-programming/1714-image-reflections-php.html
Posted By For Type Date
Image reflection... - PHPBuilder.com This thread Refback 01-15-2008 01:56 AM
PHP image reflection help... This thread Refback 01-15-2008 12:42 AM
Image reflection... - PHPBuilder.com This thread Refback 01-15-2008 12:33 AM
PHP image reflection help... This thread Refback 01-15-2008 12:19 AM
reddit.com: newest submissions This thread Refback 01-10-2008 12:46 AM
reddit.com: newest submissions This thread Refback 01-09-2008 05:02 PM
reddit.com: newest submissions This thread Refback 01-09-2008 04:44 PM
reddit.com: newest submissions This thread Refback 01-09-2008 04:44 PM
PsdHouse / House of tutorials » PHP This thread Refback 01-06-2008 08:27 PM
[] php - Post #2 Pingback 01-05-2008 06:12 PM
jro07's bookmarks on del.icio.us This thread Refback 01-02-2008 12:36 AM
Image Reflections in PHP - TalkPHP This thread Refback 01-01-2008 05:56 PM
Eksperten | Programmering :: Script :: PHP :: Rette scrpit der laver spejl effekt p billeder This thread Refback 01-01-2008 05:45 PM
Upload file script not working! Help! - SitePoint Forums This thread Refback 12-30-2007 10:01 PM
United States IP range check - SitePoint Forums This thread Refback 12-30-2007 09:10 PM
Curl not working - SitePoint Forums This thread Refback 12-28-2007 06:44 PM

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


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

 
     

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