Image Uploading Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
class ImageUpload{ public function UploadImage($extension,$imagetag,$DirName,$ImageSize,$id){ // creating directroy path $yearDir="image/" .date('Y'); $MonthDir="image/".date('Y')."/" .date('m'); $MainDir="image/".date('Y')."/" .date('m')."/".$DirName; // checking directroy is exist or creating new directroy $this->CheckDir($yearDir); $this->CheckDir($MonthDir); $this->CheckDir($MainDir); $path = $MainDir."/".time().$id.".".$extension; $size=filesize($_FILES[$imagetag]['tmp_name']); if($extension=="jpg" || $extension=="jpeg" ) { $uploadedfile = $_FILES[$imagetag]['tmp_name']; $src = imagecreatefromjpeg($uploadedfile); } else if($extension=="png") { $uploadedfile = $_FILES[$imagetag]['tmp_name']; $src = imagecreatefrompng($uploadedfile); } else { $src = imagecreatefromgif($uploadedfile); } list($width,$height)=getimagesize($uploadedfile); if($width>=$ImageSize){ $newwidth=$ImageSize; $newheight=($height/$width)*$newwidth; $tmp=imagecreatetruecolor($newwidth,$newheight); }else{ $newwidth=$width; $newheight=($height/$width)*$newwidth; $tmp=imagecreatetruecolor($newwidth,$newheight); } if($extension=="png"){ imagealphablending($tmp, FALSE); imagesavealpha($tmp, TRUE); } imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); $filename = $path; if($extension=="png") imagepng($tmp,$filename); else if($extension=="jpg" || $extension=="jpeg" ) imagejpeg($tmp,$filename,90); else imagegif($tmp,$filename); imagedestroy($src); imagedestroy($tmp); return $filename; } public function CheckDir($Dir){ if(is_dir($Dir)); else{ mkdir($Dir); $file = fopen($Dir."/index.html","w") or die(); $txt = "Access Denied."; fwrite($file,$txt); fclose($file); } return true; } } |
Here to start image uploading
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $filename = stripslashes($_FILES['photo']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { echo "File not supported."; }else{ $ImageUpload = new ImageUpload(); $imagepath = $ImageUpload->UploadImage($extension,'photo','MyPic','600','1'); // set 600px width image echo $imagepath; } |
Leave a reply