Thursday 17 May 2012

Upload multiple images with thumbnail creation included php code

<?php

if(isset($_POST['submit'])){
$destpath = "photos/";

while(list($key,$value) = each($_FILES["file"]["name"])) {
   
if(!empty($value))
{
if (($_FILES["file"]["type"][$key] == "image/gif")
|| ($_FILES["file"]["type"][$key] == "image/jpeg")
|| ($_FILES["file"]["type"][$key] == "image/pjpeg")
|| ($_FILES["file"]["type"][$key] == "image/png")
&& ($_FILES["file"]["size"][$key] < 2000000))
    {



$source = $_FILES["file"]["tmp_name"][$key] ;
$filename = $_FILES["file"]["name"][$key] ;
move_uploaded_file($source, $destpath . $filename) ;
//echo "Uploaded: " . $destpath . $filename . "<br/>" ;
//thumbnail creation start//
$tsrc="photos/thimg/".$_FILES["file"]["name"][$key];   // Path where thumb nail image will be stored
//echo $tsrc;
/*if (!($_FILES["file"]["type"] =="image/jpeg" OR $_FILES["file"]["type"] =="image/png" OR $_FILES["file"]["type"]=="image/gif")){echo "Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
}*/
$n_width=100;          // Fix the width of the thumb nail images
$n_height=100;         // Fix the height of the thumb nail imaage

/////////////////////////////////////////////// Starting of GIF thumb nail creation///////////
$add=$destpath . $filename;
if($_FILES["file"]["type"][$key]=="image/gif"){
//echo "hello";
$im=ImageCreateFromGIF($add);
$width=ImageSx($im);              // Original picture width is stored
$height=ImageSy($im);                  // Original picture height is stored
$newimage=imagecreatetruecolor($n_width,$n_height);
imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
if (function_exists("imagegif")) {
Header("Content-type: image/gif");
ImageGIF($newimage,$tsrc);
}
if (function_exists("imagejpeg")) {
Header("Content-type: image/jpeg");
ImageJPEG($newimage,$tsrc);
}
    }
//chmod("$tsrc",0777);
////////// end of gif file thumb nail creation//////////
$n_width=100;          // Fix the width of the thumb nail images
$n_height=100;         // Fix the height of the thumb nail imaage

////////////// starting of JPG thumb nail creation//////////
if($_FILES["file"]["type"][$key]=="image/jpeg"){
    echo $_FILES["file"]["name"][$key]."<br>";
$im=ImageCreateFromJPEG($add);
$width=ImageSx($im);              // Original picture width is stored
$height=ImageSy($im);             // Original picture height is stored
$newimage=imagecreatetruecolor($n_width,$n_height);                
imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
ImageJpeg($newimage,$tsrc);
chmod("$tsrc",0777);
}
////////////////  End of png thumb nail creation //////////
if($_FILES["file"]["type"][$key]=="image/png"){
//echo "hello";
$im=ImageCreateFromPNG($add);
$width=ImageSx($im);              // Original picture width is stored
$height=ImageSy($im);                  // Original picture height is stored
$newimage=imagecreatetruecolor($n_width,$n_height);
imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
if (function_exists("imagepng")) {
//Header("Content-type: image/png");
ImagePNG($newimage,$tsrc);
}
if (function_exists("imagejpeg")) {
//Header("Content-type: image/jpeg");
ImageJPEG($newimage,$tsrc);
}
    }

// thumbnail creation end---
    }
else{echo "error in upload";}
}
}
echo "files has been uploaded to the photos";
}
?>
<html>
<form name="frm" method="post" enctype="multipart/form-data"  >
<input type="file" name="file[]" multiple="multiple"/>
<input type="submit" name="submit"/>
</form>
</html>

14 comments:

  1. Thanks for the script and work you have done.

    ReplyDelete
  2. Excellent, thanks for this - it works great!

    Is there a way to use it with ajax so the page doesn't refresh?

    ReplyDelete
  3. yes there's a way to do this process without refreshing current page

    You should use ajax to submit your form data

    ReplyDelete
  4. Fatal error: require_once() [function.require]: Failed opening required 'C:/xampp/htdocs\includes\defines.php'

    ReplyDelete
  5. Hello Mr.Er.Dharmesh How to fix width and height of large image....

    ReplyDelete
  6. Thank's,,I wanna use this in my web

    ReplyDelete
  7. Hi excellent script, however I have an issue - if I remove the echo statement from any of thumbnail creation sections the page does not reload, just a small square box in the top left corner of the screen. I actually want a message to appear in the body of the page so I don't want an echo statement in the body. Any ideas?

    ReplyDelete
  8. Nice script for beginners.

    ReplyDelete
  9. this is nice script.. its work fine

    ReplyDelete
  10. things so much for your code.
    i realy need this love you

    ReplyDelete