PHP

 Home  Computers & Internet  Web Programming PHP
What is PHP?
Echo
Comments
Variables
Constants
Data Types
number_format()
Character Strings
Mathematical Operators
Comparison Operators
Logical Operators
Joining Strings
explode()
implode()
strtolower()
strtoupper()
strlen()
ucfirst()
ucwords()
strrev()
str_replace()
str_repeat()
trim()
strip_tags()
addslashes()
stripslashes()
strpos()
strrpos()
nl2br()
isset()
unset()
empty()
POST
GET
If Statements
If Else Statements
Elseif Statements
Switch Statements
For Loops
While Loops
Do While Loops
Foreach Loops
File Create
File Open
File Read
File Write
File Delete
fgets()
file_get_contents()
Date & Time
$_SERVER
Sessions
Cookies
Arrays

HOW TO CROP IMAGES ON THE FLY WITH PHP

This tutorial shows you how to crop images on the fly using PHP. The aspect ratio is preserved so that the images are not distorted. You either display the cropped image on the fly (as in the demo below) or save the cropped image.

Normal:


Cropped To 100px by 100px:


In order for this to work, you will need to have PHP installed with the GD library installed.

Option 1: Cropping And Displaying On The Fly

Create a file called crop.php with the following code:

<?
header ("Content-type: image/jpeg");
$file_name=$_GET['f'];
$crop_height=$_GET['h'];
$crop_width=$_GET['w'];
$file_type= explode('.', $file_name);
$file_type = $file_type[count($file_type) -1];
$file_type=strtolower($file_type);

$original_image_size = getimagesize($file_name);
$original_width = $original_image_size[0];
$original_height = $original_image_size[1];

if($file_type=='jpg')
{
$original_image_gd = imagecreatefromjpeg($file_name);
}

if($file_type=='gif')
{ $original_image_gd = imagecreatefromgif($file_name);
}

if($file_type=='png')
{
$original_image_gd = imagecreatefrompng($file_name);
}

$cropped_image_gd = imagecreatetruecolor($crop_width, $crop_height);
$wm = $original_width /$crop_width;
$hm = $original_height /$crop_height;
$h_height = $crop_height/2;
$w_height = $crop_width/2;

if($original_width > $original_height )
{
$adjusted_width =$original_width / $hm;
$half_width = $adjusted_width / 2;
$int_width = $half_width - $w_height;

imagecopyresampled($cropped_image_gd ,$original_image_gd ,-$int_width,0,0,0, $adjusted_width, $crop_height, $original_width , $original_height );
}
elseif(($original_width < $original_height ) || ($original_width == $original_height ))
{
$adjusted_height = $original_height / $wm;
$half_height = $adjusted_height / 2;
$int_height = $half_height - $h_height;

imagecopyresampled($cropped_image_gd , $original_image_gd ,0,-$int_height,0,0, $crop_width, $adjusted_height, $original_width , $original_height );
}
else {

imagecopyresampled($cropped_image_gd , $original_image_gd ,0,0,0,0, $crop_width, $crop_height, $original_width , $original_height );
}
imagejpeg($cropped_image_gd);

?>

The above script will take three parameters:
h= the desired height of cropped image (in pixels)
w= the desired width of cropped image (in pixels)
f= the filename of original image - use the correct path if necessary

To display the image example:

<img src="crop.php?h=100&w=100&f=bmw.jpg" />

Option 2: Saving The Cropped Image Only

1. Remove or comment out the first line of the script.
2. Replace the last two lines of the script with:
$dest ="test/bmw_cropped.jpg"; //edit filename for cropped image
//use the correct path if necessary
imagejpeg($cropped_image_gd ,$dest,100);

Home | Privacy Policy | Terms Of Use | Contact Us | Work At Home | Online Jobs | Smart Info | Online Jobs | Data Entry Jobs | Paid Surveys | Info | Info | Typing Jobs | Article Archive | Forum Posting Jobs