Monday, June 15, 2009

Cropping in PHP

When an image is uploaded, Complete image will be displayed, But there may be a situation where we need to have only part of the image to be displayed instead of complete image. So in that scenario's we use the concept called CROPPING.
The croppping option is the apt solution for such type of queries. This provides with same many options to the user. User can easily select the area, Which area he wants to crop. By providing the options user feels free to use the website.

The below downloading code is the basic version of the cropping. The downloading folder will contain the 2 folder and 2 files.

1. JS folder which contains the javascript function needed for the cropping option.
2. upload_pic which contains the uploaded images and the cropped images. When the
images are cropped, Then the images are saved in this folder.
3. Cropping document.txt which contains all the data related to the cropping like
where to change the width and height of the cropped area, where the files are
saving etc.
4. upload_crop.php contains the php code.

Code Explanation:

resizeThumbnailImage() This function is used to save the cropped image,It takes the thumb image name , height , width, starting height,starting width, scale .

getWidth,getHeight These methods are used for getting the uploaded image width and the height.

Leia Mais…

Monday, June 1, 2009

Upload Image With PHP

This example provides the uploaded image width and height before uploading the image which will be useful to restrict the users from uploading the large images.

//checks if the form has been submitted
if(isset($_POST['submit']))
{
//upload directory where the file will be stored
$uploaddir = 'uploads/';

$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

$uploadfiles = basename($_FILES['userfile']['name']);

list($width, $height, $type, $attr) =
getimagesize($_FILES['userfile']['tmp_name']);

if (file_exists($uploadfile)) {
//print error message
echo $uploadfiles;
echo "- file already exists");
die();
} else {
echo "The file $uploadfiles does not exist
";
}


if ($width > 80 || $height > 80)
{
//print error message
echo "Maximum allowed size is 80x80 pixels";
die();
}


if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "
Image Uploaded Successfully!

";

echo "
Image Width: ";
echo $width;
echo "
Image Height: ";
echo $width;
echo "
Image type: ";
echo $type;
echo "
Attribute: ";
echo $attr;
echo "
";
} else {
//print error message
echo "
File was not successfully uploaded

";
die();
}

}else{

?>

<form enctype="multipart/form-data" method="post" action="index.php">
<TABLE><TR><TD>
//define a maxim size for the uploaded images
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
Send this file: <input name="userfile" type="file">
</TD></TR><TR><TD>
<input type="submit" name="submit" value="upload">
</form>
</TD></TR></TABLE>

}
?>

Leia Mais…