Friday, January 30, 2015
upload and resize multiple images with one input using php
<?php
if(isset($_POST["btn"])){
extract($_POST);
$error=array();
$extension=array("jpeg","jpg","png","gif");
foreach($_FILES["uploadedfile"]["tmp_name"] as $key=>$tmp_name)
{
$time=mktime(date("h"),date("i"),date("s"),date("d"));
$tt=date("hisd",$time);
$target_path = "photo_gallery/".$tt;
$target_path = $target_path . basename( $_FILES["uploadedfile"]['name'][$key]);
$target_path2 = "photo_gallery/small/".$tt;
$target_path2 = $target_path2 . basename( $_FILES["uploadedfile"]['name'][$key]);
/* $pn="photo_gallery/".basename( $_FILES["uploadedfile"]['name']);*/
$pn="photo_gallery/".basename($_FILES["uploadedfile"]["name"][$key]);
$ch=strtolower(substr($pn, strrpos($pn,".")+1));
if($ch=="jpg" or $ch=="png" or $ch=="jpeg" or $ch=="gif"){
/* $uploadedfile = $_FILES['uploadedfile']['tmp_name'];*/
$uploadedfile = $_FILES["uploadedfile"]["tmp_name"][$key];
if (!empty($uploadedfile)){
$info = getimagesize($uploadedfile);
if($ch=="jpg" || $ch=="jpeg" )
{
if ($info['mime'] == 'image/jpeg')
$src = imagecreatefromjpeg($uploadedfile);
}
else if($ch=="png")
{
if($info['mime'] == 'image/png')
$src = imagecreatefrompng($uploadedfile);
}
else
{
if($info['mime'] == 'image/gif')
$src = imagecreatefromgif($uploadedfile);
}
list($width,$height)=getimagesize($uploadedfile);
$newwidth=$width;
$newheight=$height;
$newwidth2=312;
$newheight2=($height/$width)*$newwidth2;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$tmp2=imagecreatetruecolor($newwidth2,$newheight2);
if(!empty($src)){
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagecopyresampled($tmp2,$src,0,0,0,0,$newwidth2,$newheight2,$width,$height);
$filename = "photo_gallery/".$tt. $_FILES["uploadedfile"]["name"][$key];
$filename2 = "photo_gallery/small/".$tt. $_FILES["uploadedfile"]["name"][$key];
imagejpeg($tmp,$filename,100);
imagejpeg($tmp2,$filename2,100);
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp2);
}
}
}
}
}
?>
<form enctype=multipart/form-data method='POST'>
<input type="file" name="uploadedfile[]" multiple accept="image/*"/>
<input type="submit" name="btn" value="upload">
</form>
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment