图片上传制作缩略图--类似手机相册
Posted z329982468
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图片上传制作缩略图--类似手机相册相关的知识,希望对你有一定的参考价值。
function uploadPic()
$back_code = array();
$upTypes = array(
'image/jpg',
'image/jpeg',
'image/png',
'image/gif',
'image/bmp',
);
$temp = array(1=>'gif', 2=>'jpeg', 3=>'png');
$max_file_size = 10000000; // 文件大小限制
$upload_dir = "/var/www/html/dove.kmapp.cn/Upload/WeddingSpace/"; // 上传路径
$date_dir = date("Y-m-d",time()); // 按日期分目录存储
$update_time = date("Y-m-d H:i:s",time()); // 按日期分目录存储
$conn = connect_to_db();
// 判断文件类型和文件大小
if(in_array($_FILES['file']['type'], $upTypes) && $_FILES['file']['size'] < $max_file_size)
if($_FILES['file']['error'] > 0)
$back_code['num'] = -2;
$back_code['msg'] = $_FILES["file"]["error"] ;
else
// 获取手机照片的旋转角信息 1:0°,6:顺90°,8:逆90°,3:180°
$exif_arr = exif_read_data($_FILES['file']['tmp_name'], 'IFD0');
$Orientation = $exif_arr['Orientation'];
if($Orientation == 1)
$degrees = 0;
else if($Orientation == 6)
$degrees = 270;
else if($Orientation == 8)
$degrees = 90;
else if($Orientation == 3)
$degrees = 180;
$photo_postfix = explode('/', $_FILES['file']['type'])[1];
$randStrArr = getUrl();
$photo_name = $randStrArr['randStr'] . $randStrArr['timeStr'] . '.' . $photo_postfix; // 文件名
$thumb_name = 'thumb_' . $photo_name ;
$photo_url = $date_dir . '/' . $photo_name;
$thumb_url = $date_dir . '/' . 'thumb_' . $photo_name ;
// 判断是否存在目录并创建
if(!is_dir($upload_dir . $date_dir))
mkdir($upload_dir . $date_dir);
// 制作缩略图 *****
$size_wh = getimagesize($_FILES['file']['tmp_name']);//0: 1280, 1: 800, 2: 2, 3: "width="1280" height="800"", bits: 8, channels: 3, mime: "image/jpeg"
$thumb_width = 300;
$thumb_height = 300;
$size_width = $size_wh[0]; // 宽
$size_height = $size_wh[1]; // 高
$img_type = $size_wh[2]; // 文件类型
$create_name = 'imagecreatefrom'. $temp[$img_type];
$output_name = 'image' . $temp[$img_type];
// $cimg = imagecreatetruecolor($thumb, $thumb);
$timg = imagecreatetruecolor(300,300);
$source = $create_name($_FILES['file']['tmp_name']);
$ratio_w=1.0 * $thumb_width / $size_width;
$ratio_h=1.0 * $thumb_height / $size_height;
$ratio=1.0;
if( ($ratio_w < 1 && $ratio_h < 1) || ($ratio_w > 1 && $ratio_h > 1))
$ratio = $ratio_w < $ratio_h ? $ratio_h : $ratio_w;
$tmp_w = (int)($thumb_width / $ratio);
$tmp_h = (int)($thumb_height / $ratio);
$tmp_img=imagecreatetruecolor($tmp_w , $tmp_h);
$src_x = (int) (($size_width-$tmp_w)/2) ;
$src_y = (int) (($size_height-$tmp_h)/2) ;
imagecopy($tmp_img, $source, 0,0,$src_x,$src_y,$tmp_w,$tmp_h);
imagecopyresampled($timg,$tmp_img,0,0,0,0,$thumb_width,$thumb_height,$tmp_w,$tmp_h);
imagedestroy($tmp_img);
else
$ratio = $ratio_w < $ratio_h ? $ratio_h : $ratio_w;
$tmp_w = (int)($size_width * $ratio);
$tmp_h = (int)($size_height * $ratio);
$tmp_img=imagecreatetruecolor($tmp_w ,$tmp_h);
imagecopyresampled($tmp_img,$source,0,0,0,0,$tmp_w,$tmp_h,$size_width,$size_height);
$src_x = (int)($tmp_w - $thumb_width) / 2 ;
$src_y = (int)($tmp_h - $thumb_height) / 2 ;
imagecopy($timg, $tmp_img, 0,0,$src_x,$src_y,$thumb_width,$thumb_height);
imagedestroy($tmp_img);
$rotate = imagerotate($timg, $degrees, 0);
if($output_name($rotate , $upload_dir . $date_dir . '/' . $thumb_name , 100))
// 上传文件
if(move_uploaded_file($_FILES['file']['tmp_name'], $upload_dir . $date_dir . '/' . $photo_name))
$insert_sql = "INSERT INTO `wedding_space_photo` (oid,ID,big_photo,small_photo,update_time) VALUES ('$oid','$ID','$photo_url','$thumb_url','$update_time') ";
mysql_query($insert_sql);
$id = mysql_insert_id();
$back_code['num'] = 0;
$back_code['msg'] = "上传成功";
$back_code['img_id'] = $id;
$back_code['src'] = "http://dove/Upload/".$date_dir. '/' .$photo_name;
$back_code['thumb_src'] = "http://dove/Upload/".$date_dir. '/' .$thumb_name;
else
$back_code['num'] = -4;
$back_code['msg'] = "上传失败,请重试";
else
$back_code['num'] = -5;
$back_code['msg'] = "做略图制作失败,请重试";
else
$back_code['num'] = -3;
$back_code['msg'] = "上传文件非法";
return $back_code;
以上是关于图片上传制作缩略图--类似手机相册的主要内容,如果未能解决你的问题,请参考以下文章