Showing posts with label Thumbnails. Show all posts
Showing posts with label Thumbnails. Show all posts

Sunday, November 8, 2009

Image Upload and Cut their Thumbnail of DIff Size and Store it diff location and Insert recrod in Database

This is a image upload Program in which we upload image and dynamic cute their 4 diff size Thumbnail and store it diff location and enter these entry in image table of mysql data base

structure of Project

wallpaper->image->thum_170x120
->thum_1280x124
->thum_800x600
->connection.php
->imageUpload.php

File:-connection.php


*
Define all the constant variable
for Database Connection
*/



DEFINE('HOSTNAME','localhost');


DEFINE('USERNAME','root');



DEFINE('PASSWORD','');

DEFINE('DATABASE','image');


//End of Constraint Declaration.




/*

Function for DataBase Connection
$dbc variable for database connection

*/

$dbc=mysql_connect(HOSTNAME,USERNAME,PASSWORD) or die('There is error to connect to server:-'.mysqli_connect_error());
$db_selected = mysql_select_db(DATABASE,$dbc);

?>

//End of File connection.php


File:-imageUpload.php

require_once('connection.php'); //****include Database DAO*******************
set_time_limit(0); // Set Upload Processing Timer Infinite
//********************Variable Declare*******************
$title=$_POST['title']; //Fetch Image Title from form
/*
*Remove space with - from file name
*@param txt $name
*/
function getImageName($name){
$type=$name;
//$occur=strrpos($type,'.');
//echo $occur;
//$type=substr($type,0,$occur);
$type = str_replace( ' ', '-', $type );
return($type);
}
/*
*get File extension
*@param txt $filename
*
*/
function getFileExtension($fileName){
$fileNameParts = explode( ".", $fileName );

$fileExtension = end( $fileNameParts );

$fileExtension = strtolower( $fileExtension );
return($fileExtension);
}
if(isset($_FILES['upload'])){// Check File in Url or not
$current_date = date('Y-m-d'); //Store Current(Today) Date
$dirname='image/';
$newFileName=getImageName($title);
$fileExtension=getFileExtension($_FILES['upload']['name']);
$filesave=$newFileName.'-'.$newId.'.'.$fileExtension;
$destination=$dirname.$filesave;
$dirname='image/thum/';
$destinationthum=$dirname.$filesave;
$dirname1='image/thum_1280x1024//';
$destinationthum1=$dirname.$filesave;
$dirname3='image/thum_800x600/';
$destinationthum3=$dirname.$filesave;

if($_FILES['upload']['type'] != "image/gif"&&$_FILES['upload']['type'] != "image/jpg"&&$_FILES['upload']['type'] != "image/jpeg"&&$_FILES['upload']['type'] !="image/png" ) {
echo 'You may only upload Image files.

';
}else{
#echo 'in array';
if(move_uploaded_file($_FILES['upload']['tmp_name'],$destination)){
echo '
Image Upload


';
create_thumbnail($destination,$destinationthum,'170','130');
create_thumbnail($destination,$destinationthum1,'1280','1024');
create_thumbnail($destination,$destinationthum3,'800','600');
$insertPhoto="insert into image(title,image_name,status) values('$title','$destination','y')";
mysql_query($insertPhoto,$dbc);
}
}
}
if ($_FILES['upload']['error'] > 0) {
echo '

The file could not be uploaded because: ';


// echo a message based upon the error.
switch ($_FILES['upload']['error']) {
case 1:
echo 'The file exceeds the upload_max_filesize setting in php.ini.';
break;
case 2:
echo 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
break;
case 3:
echo 'The file was only partially uploaded.';
break;
case 4:
echo 'No file was uploaded.';
break;
case 6:
echo 'No temporary folder was available.';
break;
case 7:
echo 'Unable to write to the disk.';
break;
case 8:
echo 'File upload stopped.';
break;
default:
echo 'A system error occurred.';
break;
}
echo '

';
}
function create_thumbnail($source,$destination,$thum_width,$thum_height){
//echo $source.'
';
$size=getimagesize($source);
//echo $size[0];
$width=$size[0];
$height=$size[1];
$x=0;
$y=0;
/*if($width>$height){
$x=ceil(($width-$height)/2);
$width=$height;
}if($width<$height){
$x=ceil(($height-$width)/2);
$height=$width;
}*/
//echo '
'.$width.'--'.$height;
$new_image=imagecreatetruecolor($thum_width,$thum_height)or die('Cannot Initialize new GD image stream');
$extension=getExtension($source);
//echo '
Exten:-'.$extension.'
';
if($extension=='jpg'||$extension=='jpeg'){
$image=imagecreatefromjpeg($source);
}
if($extension=='gif'){
$image=imagecreatefromgif($source);
}
if($extension=='png'){
$image=imagecreatefrompng($source);
}
if($extension=='bmp'){
$image=@imagecreatefromwbmp($source);
}
imagecopyresampled($new_image,$image,0,0,$x,$y,$thum_width,$thum_height,$width,$height);
if($extension=='jpg'||$extension=='jpeg'){
imagejpeg($new_image,$destination);
}
if($extension=='gif'){
imagegif($new_image,$destination);
}
if($extension=='png'){
imagepng($new_image,$destination);
}
if($extension=='bmp'){
image2wbmp($new_image,$destination);
}
}
function getExtension($name){
$type=$_FILES['upload']['type'];
$type = str_replace( 'image/', '', $type );
return($type);
}

?>

//Php COde Ended

HTML code Begin


Upload Image


*Select Image To Upload:- (.jpg,.gif,.png only)
Enter Title*




//End of File image upload


mysql command:-
CREATE TABLE IF NOT EXISTS `wallpaper` ( `id` bigint(10) NOT NULL AUTO_INCREMENT, `title` varchar(100) NOT NULL, `image_name` varchar(200) NOT NULL,
'staus' enum('y','n'));