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
 
 
LinkBack (3) Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
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: 319
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.jooney.co.uk - the online portfolio

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 4 Users Say Thank You to Rendair For This Useful Post:
danielneri (12-25-2007), maZtah (12-11-2007), Orc (05-08-2008), vaakash (04-03-2010)
 


LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/advanced-php-programming/1709-cropping-images-using-php.html
Posted By For Type Date
PHP PHP GD Library: Cropping Images whilst Maintaining the Aspect Ratio Tutorial This thread Refback 01-10-2008 03:22 PM
PHP Image Manipulation PHP GD Library: Cropping Images whilst Maintaining the Aspect Ratio Tutorial This thread Refback 01-10-2008 03:05 PM
Cropping Images using PHP - TalkPHP This thread Refback 12-29-2007 10:41 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 12:43 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