Friday, February 14, 2014

compress image during upload

function compress_image($source_url, $destination_url, $quality) { $info = getimagesize($source_url); if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source_url); elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source_url); elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source_url); imagejpeg($image, $destination_url, $quality); return $destination_url; } if(isset($_FILES['add_image1'])){ $tt=time(); $target_path = "../product_image/".$tt; $target_path = $target_path . basename( $_FILES["add_image1"]['name']); $pn="product_image/".basename( $_FILES["add_image1"]['name']); $ch=strtolower(substr($pn, strrpos($pn,".")+1)); if($ch=="jpg" or $ch=="png" or $ch=="jpeg" or $ch=="gif"){ if(move_uploaded_file($_FILES['add_image1']['tmp_name'], $target_path)) { mysqli_query($db_con,"update table_question set image='".$target_path."' where question_id=".$pro_id); $destination=$target_path; $t=compress_image($target_path,$destination, 50); if($t!=''){ mysqli_query($db_con,"update table_question set image='".$filename."' where question_id=".$pro_id); } } } }

No comments:

Post a Comment