basically creating thumbnail of uploaded image. want store image name , thumbnail image inserted id. here code :
//query of insert database $pid = mysql_insert_id(); $newname = "$pid.jpg";
by above code can image id. following code creating thumbnail.
function cwupload($field_name = '', $target_folder = '', $file_name = '', $thumb = false, $thumb_folder = '', $thumb_width = '', $thumb_height = ''){ //folder path setup $target_path = $target_folder; $thumb_path = $thumb_folder; //file name setup /*echo $_files[$field_name][$newname]; die();*/ $filename_err = explode(".",$_files[$field_name]['name']); $filename_err_count = count($filename_err); $file_ext = $filename_err[$filename_err_count-1]; if($file_name != '') { $filename = $newname .'.'.$file_ext; } else { $filename = $_files[$field_name]['name']; } //upload image path $upload_image = $target_path.basename($filename); //upload image if(move_uploaded_file($_files[$field_name]['tmp_name'],$upload_image)) { //thumbnail creation if($thumb == true) { $thumbnail = $thumb_path.$filename; list($width,$height) = getimagesize($upload_image); $thumb_create = imagecreatetruecolor($thumb_width,$thumb_height); switch($file_ext){ case 'jpg': $source = imagecreatefromjpeg($upload_image); break; case 'jpeg': $source = imagecreatefromjpeg($upload_image); break; case 'png': $source = imagecreatefrompng($upload_image); break; case 'gif': $source = imagecreatefromgif($upload_image); break; default: $source = imagecreatefromjpeg($upload_image); } imagecopyresized($thumb_create,$source,0,0,0,0,$thumb_width,$thumb_height,$width,$height); switch($file_ext){ case 'jpg' || 'jpeg': imagejpeg($thumb_create,$thumbnail,100); break; case 'png': imagepng($thumb_create,$thumbnail,100); break; case 'gif': imagegif($thumb_create,$thumbnail,100); break; default: imagejpeg($thumb_create,$thumbnail,100); } } return $filename; } else { return false; } } if(!empty($_files['image']['name'])){ //call thumbnail creation function , store thumbnail name $upload_img = cwupload('image','uploads/','',true,'uploads/thumbs/','200','160'); //full path of thumbnail image $thumb_src = 'uploads/thumbs/'.$upload_img; //set success , error messages $message = $upload_img?"<span style='color:#008000;'>image thumbnail have been created successfully.</span>":"<span style='color:#f00000;'>some error occurred, please try again.</span>"; }else{ //if form not submitted, below variable should blank $thumb_src = ''; $message = ''; } header("location: product_listing.php"); exit(); } ?>
how can insert , save image , thumbnail image inserted id?
you want add name when moving folder
$pid = mysql_insert_id(); $newname = $pid.'.jpg'; //upload image path $upload_image = $target_path.$newname; //upload image if(move_uploaded_file($_files[$field_name]['tmp_name'],$upload_image))
Comments
Post a Comment