<?php function img_create_small($big_img, $width, $height, $small_img) {//原始大图地址,缩略图宽度,高度,缩略图地址 $imgage = getimagesize($big_img); //得到原始大图片 switch ($imgage[2]) { // 图像类型判断 case 1: $im = imagecreatefromgif($big_img); break; case 2: $im = imagecreatefromjpeg($big_img); break; case 3: $im = imagecreatefrompng($big_img); break; } $src_W = $imgage[0]; //获取大图片宽度 $src_H = $imgage[1]; //获取大图片高度 //按比例调整图像大小 if ($src_W>=$src_H){ $height=$height*($src_H/$src_W); } if ($src_W<=$src_H){ $width=$width*($src_W/$src_H); } $tn = imagecreatetruecolor($width, $height); //创建缩略图 imagecopyresampled($tn, $im, 0, 0, 0, 0, $width, $height, $src_W, $src_H); //复制图像并改变大小 imagejpeg($tn, $small_img); //输出图像 } $base_path = "./upload/"; //存放� �� if(!is_dir($base_path)){ mkdir($base_path,0777,true); } $tmp1=basename ( $_FILES ['attach'] ['name'] ); $typeid=substr($tmp1,0,strpos($tmp1,"_"))."/"; $base_path =$base_path. $typeid; if(!is_dir($base_path)){ mkdir($base_path,0777,true); } if(!is_dir($base_path."s/")){ mkdir($base_path."s/",0777,true); } $target_path = $base_path . substr($tmp1,strpos($tmp1,"_")+1); $small_path = $base_path ."s/". substr($tmp1,strpos($tmp1,"_")+1); if (move_uploaded_file ( $_FILES ['attach'] ['tmp_name'], $target_path )) { img_create_small($target_path,300,300,$small_path); $array = array ( "status" => true, "msg" => $_FILES ['attach'] ['name'] ); echo json_encode ( $array ); } else { $array = array ( "status" => false, "msg" => "文件上传错误!" . $_FILES ['attach'] ['error'] ); echo json_encode ( $array ); } ?>
PHP 图片上传 并等比例缩小生成缩略图
2020-09-21 21:29:37
阅读 3467 次
评论 0 条
发表评论