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
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack (3) Thread Tools Search this Thread Display Modes
Old 12-11-2007, 05:53 PM   3 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: 296
Thanks: 18
Rendair is on a distinguished road
Default Cropping Images using PHP

I thought i would write a nice tutorial on how to crop images using php.

Demo:HERE

What will it do?

This script will allow you to make a smaller image of a bigger image, but at the same time crop it. This will prevent the image looking stretched and deformed.

We will create this using a class (Sorry using PHP 4 at the moment, but should work on 5)
Firstly lets set up the class.

PHP Code:
class cropImage{
 
//code here

Now we need to set up some variables to be used throughout the program.

PHP Code:
var $imgSrc,$myImage,$cropHeight,$cropWidth,$x,$y,$thumb
The variables above will be explained once we use them. Now we need to create the first function. This function will get the image we are going to crop, work out its dimension and then use them dimensions to work out how we are going to crop it.

PHP Code:

function setImage($image)
{

//Your Image
   
$this->imgSrc $image
                     
//getting the image dimensions
   
list($width$height) = getimagesize($this->imgSrc); 
                     
//create image from the jpeg
   
this->myImage imagecreatefromjpeg($this->imgSrc) or die("Error: Cannot find image!"); 
            
       if(
$width $height$biggestSide $width//find biggest length
       
else $biggestSide $height
                     
//The crop size will be half that of the largest side 
   
$cropPercent .5// This will zoom in to 50% zoom (crop)
   
$this->cropWidth   $biggestSide*$cropPercent
   
$this->cropHeight  $biggestSide*$cropPercent
                     
                     
//getting the top left coordinate
   
$this->= ($width-$this->cropWidth)/2;
   
$this->= ($height-$this->cropHeight)/2;
             

Now we actually need to start creating the actual cropped image.

PHP Code:
function createThumb()
{
                    
  
$thumbSize 250// will create a 250 x 250 thumb
  
$this->thumb imagecreatetruecolor($thumbSize$thumbSize); 

  
imagecopyresampled($this->thumb$this->myImage00,$this->x$this->y$thumbSize$thumbSize$this->cropWidth$this->cropHeight); 

Now all we need to do is render the image out.

PHP Code:
function renderImage()
{
                     
   
header('Content-type: image/jpeg');
   
imagejpeg($this->thumb);
   
imagedestroy($this->thumb); 

Now we just need to create the instance

PHP Code:
$image = new cropImage;
$image->setImage($src);
$image->createThumb();
$image->renderImage(); 
You can use this script to display a thumb of images on a new page, by using the following page.

PHP Code:
<img src="thumbcreate.php?src=images/largimg.jpg"//link to large image 
__________________
www.mypubquiz.co.uk - The Quiz Community - NEW QUIZZES

Last edited by Rendair : 12-11-2007 at 07:58 PM.
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
The Following 3 Users Say Thank You to Rendair For This Useful Post:
danielneri (12-25-2007), maZtah (12-11-2007), Orc (05-08-2008)
Old 12-11-2007, 07:31 PM   #2 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 445
Thanks: 49
ReSpawN is on a distinguished road
Default

Awesome script! I've skimmed the edges but it looks very nice!

Say, do you have any scripts for the "Wet Floor" effect?
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 12-11-2007, 07:39 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,600
Thanks: 72
Wildhoney is on a distinguished road
Default

The wet floor effect?

__________________
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-11-2007, 07:44 PM   #4 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 445
Thanks: 49
ReSpawN is on a distinguished road
Default

... Dude ? How the hell did you do that? :P Last time I can recall it has been referred to as "The Wet Floor" effect. Thus giving a similar reflexion with a gradient, slowly fading out the text into the base color of the layout.

How did you manage that Wildhoney?
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 12-11-2007, 07:58 PM   #5 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 296
Thanks: 18
Rendair is on a distinguished road
Default

Yes i do actually have a "wet floor effect" let me sort it out and ill post a tutorial.
__________________
www.mypubquiz.co.uk - The Quiz Community - NEW QUIZZES
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
The Following User Says Thank You to Rendair For This Useful Post:
ReSpawN (12-11-2007)
Old 12-11-2007, 08:53 PM   #6 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 445
Thanks: 49
ReSpawN is on a distinguished road
Default

Awesome man! That would come in handy! I'll be looking forward to the tutorial and the script behind it. Only one notion, can you make it compatible with PHP 4 and up as well? Since some of my customers still do not use 5+.

Thanks a bunch!

Mark
__________________
"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, 04:51 PM   #7 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Location: On your Hard Drive, hiding like a Virus
Posts: 824
Thanks: 163
Orc is on a distinguished road
Default

The "Wet Floor" effect is actually pretty easy in Photoshop! Or in my standards it is :p
Orc is offline  
Reply With Quote
Old 12-12-2007, 09:03 PM   #8 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 296
Thanks: 18
Rendair is on a distinguished road
Default

Yeah indeed the wet floor effect is easy to do in photoshop, but to be able to do it via a website and to alot of images at once would be better.
__________________
www.mypubquiz.co.uk - The Quiz Community - NEW QUIZZES
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-12-2007, 09:05 PM   #9 (permalink)
Jay
The Contributor
Good Samaritan 
 
Join Date: Dec 2007
Posts: 53
Thanks: 5
Jay is on a distinguished road
Default

I wanna know how Wildhoney did his, with PHP or Photoshop!
Jay is offline  
Reply With Quote
Old 12-14-2007, 02:36 PM   #10 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,600
Thanks: 72
Wildhoney is on a distinguished road
Default

I used Photoshop I was asking if that's how you wanted it. That's a task for the GD master - Rendair! In extension to his article the other day on reflections.
__________________
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-14-2007, 03:06 PM   #11 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 445
Thanks: 49
ReSpawN is on a distinguished road
Default

Yeah that was a good article. We're gonna expand it when we both have the time. Since I am pretty interested in the whole thing, it might be neat to implement it into my CMS.
__________________
"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-14-2007, 04:01 PM   #12 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 715
Thanks: 2
Salathe is on a distinguished road
Default



Fig. 1: Funky little reflections, careful you don't slip on the wet surface!
I was curious to see whether it was possible to approximate Wildhoney's photoshopped version of the wet floor using GD and I'm always up for a challenge. After playing around with a few different techniques I came up with something and the result can be seen above (Fig. 1, using some of Dale's images, thanks!) -- do you think that would qualify as a 'wet floor' effect?

Essentially, I just place a 'wave' to the reflection and add a little blur. The code itself is still very raw (read: a procedural mess!!!) but that can be refactored easily enough. I just want to say, it wasn't easy getting my head around the problem (though insomnia can lead to some creative solutions)!
__________________
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Rendair (12-14-2007)
Old 12-14-2007, 04:14 PM   #13 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 296
Thanks: 18
Rendair is on a distinguished road
Default

Well personally i think your code would class as more of a wet floor effect considering water ripples when it reflects. I would be very much interested in seeing the code and also, nice one using my images looks cool.
__________________
www.mypubquiz.co.uk - The Quiz Community - NEW QUIZZES
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-14-2007, 04:39 PM   #14 (permalink)
The Contributor
Upcoming Programmer 
 
Matt83's Avatar
 
Join Date: Oct 2007
Location: Argentina
Posts: 72
Thanks: 18
Matt83 is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post


Fig. 1: Funky little reflections, careful you don't slip on the wet surface!
I was curious to see whether it was possible to approximate Wildhoney's photoshopped version of the wet floor using GD and I'm always up for a challenge. After playing around with a few different techniques I came up with something and the result can be seen above (Fig. 1, using some of Dale's images, thanks!) -- do you think that would qualify as a 'wet floor' effect?

Essentially, I just place a 'wave' to the reflection and add a little blur. The code itself is still very raw (read: a procedural mess!!!) but that can be refactored easily enough. I just want to say, it wasn't easy getting my head around the problem (though insomnia can lead to some creative solutions)!
That looks very good would you mind posting a higher res screenshot?
__________________
http://www.mattvarone.com
Matt83 is offline  
Reply With Quote
Old 12-14-2007, 10:27 PM   #15 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 715
Thanks: 2
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Matt83 View Post
That looks very good would you mind posting a higher res screenshot?
Sure, http://i19.photobucket.com/albums/b1...tfloor-big.jpg (linked because it's too wide for here)
__________________
Salathe is offline  
Reply With Quote
Old 12-25-2007, 05:17 PM   #16 (permalink)
The Contributor
 
Join Date: Dec 2007
Location: Florida
Posts: 73
Thanks: 12
danielneri is on a distinguished road
Default

Hey real nice tutorial! Lots to learn here, I'll prolly be using that in the future :p
Send a message via AIM to danielneri
danielneri is offline  
Reply With Quote
Old 12-26-2007, 09:57 AM   #17 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 445
Thanks: 49
ReSpawN is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
Sure, http://i19.photobucket.com/albums/b1...tfloor-big.jpg (linked because it's too wide for here)
Looks good, it's a different version as the one Dale made, but it looks good as well. Post the code if you have the time.
__________________
"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-02-2008, 11:39 AM   #18 (permalink)
The Addict
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 247
Thanks: 6
Kalle is on a distinguished road
Default

The wet floor effect can be made pretty easily using GD. All it requires is to mirror some of the image and apply a custom made wave filter on it, like the wave filter used in vBulletin's captha functions =)
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline