1.接口形式上传图片
采用的是AJAX的原生形式,formdata传递表单数据
/*上传点击*/
/*获取表单的值*/
window.add_goods.onsubmit=function () {
var msg=[];
if(is_null($('#uploading-file').val()) =="" ||is_null($('#goods-names').val()) =="" ||is_null($('#goods_price').val()) =="" ) {
alert('请完善信息');
return false;
}
var fd = new FormData(this);
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.readyState==4){
msg = eval("("+xhr.responseText+")");
if(msg.code == 200){
// alert('上传成功')
$('.add-goods-wrapper').addClass('displayNone');
$('.push-goods-wrapper').removeClass('displayNone');
data={
datas:msg.datas[0]
}
$('.push-goods-wrapper').html(template('show_tf_goods',data))
}else{
alert('商品上传失败!');
}
}
};
xhr.open('post','/phone/phone.php?r=Live/admin_add_goods');
xhr.send(fd);
return false;
}
2.后台接收数据
public function test(){
/*第一种:不作修改的上传图片*/
//先判断是否上传成功
if($_FILES['one']['error'] !=0 ){
output_error('上传图片失败');
}
//创建新文件夹
$new_file='../images/test/new_img/'.date('Y-m-d',time());
//如果文件夹不存在就新建一个
if(!file_exists($new_file)){
mkdir($new_file,0777,true);
}
//拼接图片文件的路径和文件名
$new_file .='/'.time().mt_rand(1000,9999).strrchr($_FILES['one']['name'],'.');
//保存图片到文件夹(将内存中的图片复制到服务器硬盘中)
if(!move_uploaded_file($_FILES['one']['tmp_name'],$new_file)){output_error('上传封面失败');}
//销毁内存中的图片
unset($new_file,$_FILES['one']['tmp_name']);
output_data('上传成功');
}
3.
public function new_upload($file_name){
/*第二种:修改缩放图片后再上传*/
//先判断上传是否成功
foreach ($_FILES as $k => $v){
if ($v['error'] != 0){ //错误,退出方法
continue;
}
//1、解析图片格式
$ext='.'.array_pop(explode('/',$v['type']));//删除数组中的最后一个元素
//2、创建存放图片的文件夹和路径
if($k==$file_name){
//文件夹路径
$new_file='../images/test/new_img/'.date('Y-m-d',time());
//创建文件夹
if(!file_exists($new_file)){
mkdir($new_file);
}
//拼接新的文件路径
$new_file .='/new'.time().mt_rand(1000,9999).$ext;
//3、把内存中的图片复制存储到硬盘中
move_uploaded_file($v['tmp_name'],$new_file);
//把路径存下来用于存数据库
$this->img=$new_file;
}
}
//生成缩略图
$this->new_get_thumImge(800,800);
}
/*缩略图*/
public function new_get_thumImge($new_width,$new_height){
//1.创建新大小的图片
//参数 x ,y 分别为要创建图像的宽度和高度像素值,返回一个图像资源。
$new_img=imagecreatetruecolor($new_width,$new_height);
//2.获取原图的信息
list($old_width,$old_height,$type)=getimagesize($this->img) ;
//3.判断图片的类型
$type_array=array(1=>'gif',2=>'jpeg',3=>'png');
$type_str=$type_array[$type];
//4.imagecreatefrom 系列函数用于从文件或 URL 载入一幅图像,成功返回图像资源,失败则返回一个空字符串。
$fun="imagecreatefrom" . $type_str;
$old_image=$fun($this->img);
//5.定坐标
$des_x = 0; //目标 X 坐标点。
$des_y = 0; //目标 Y 坐标点。
$src_x = 0; //源的 X 坐标点。
$src_y = 0; //源的 Y 坐标点。
//6.处理图片(核心)
imagecopyresampled($new_img,$old_image,$des_x,$des_y,$src_x,$src_y,$new_width,$new_height,$old_width,$old_height);
//7.创建新的路径存储图片
//创建文件夹
$the_file='../images/test/thumimge/';
if(!file_exists($the_file)){
mkdir($the_file);
}
//8.拼接完整的文件路径
$the_file .='/'.time().mt_rand(10000,99999).'.'.$type_str;
//9.复制新图片到硬盘中
imagejpeg($new_img,$the_file);
//10.把路劲存在数据库
$this->thumb_img=$the_file;
}
总结:图片上传无非就是用好那几个GD库的函数
imagecreatetruecolor() 创建新画布
getimagesize() 获取原图的参数
imagecreatefrom 获取原图的源码
imagecopyresampled() 图片处理函数,核心
在保存图片的问题上
move_uploaded_file() 把内存中的图片存在硬盘中 ,直接转存。不能缩小存
imagejpeg($new_img,$the_file);
这个可以缩小图片后存
private function _get_thumImge($filename,$des_width,$des_height){
//$des_img 目标图片的资源
$des_img = imagecreatetruecolor($des_width,$des_height);
//获取原图片的信息
list($src_width,$src_height,$type) = getimagesize($filename);
$type_array = array(1=>"gif",2=>"jpeg",3=>"png");
//图片类型的字符串格式
$type_str = $type_array[$type];
//拼装获取图片资源的函数
$fun="imagecreatefrom".$type_str;
//原图片的资源
$src_img = $fun($filename);
$des_x = 0;
$des_y = 0;
$src_x = 0;
$src_y = 0;
imagecopyresampled(
$des_img,$src_img,
$des_x,$des_y,
$src_x,$src_y,
$des_width,$des_height,
$src_width,$src_height
);
$thumImg_file = str_replace('major/'.date('Y-m-d',time()).'/major_','thum/'.date('Y-m-d',time()).'/thum_',$filename);
$arr = explode('/',$thumImg_file);
$file_name = array_pop($arr);
$thumImg_file = implode('/', $arr);
if(!file_exists($thumImg_file)){
mkdir($thumImg_file,0777,true);
}
imagejpeg($des_img,$thumImg_file.'/'.$file_name);
return str_replace('../','',$thumImg_file.'/'.$file_name);
}
实例:
/*
* sku的颜色图片
* 两个方面:把图片上传到oss中,把地址上传到数据库中,同时添加到memcached上
*/
public function upsku_img(){
var_dump($_FILES,$_POST);
date_default_timezone_set("Asia/Shanghai");
/*第二种:修改缩放图片后再上传*/
//先判断上传是否成功
foreach ($_FILES as $k => $v) {
if ($v['error'] != 0) { //错误,退出方法
continue;
}
//1、解析图片格式
$ext = '.' . array_pop(explode('/', $v['type']));//删除数组中的最后一个元素
//2、创建存放图片的文件夹和路径
if ($k == "file") {
//文件夹路径
$new_file = '../images/goods/img/' . date('Y-m-d', time());
//创建文件夹
if (!file_exists($new_file)) {
mkdir($new_file, 0777, true);
}
//拼接新的文件路径
$new_file .= '/new' . time() . mt_rand(1000, 9999) . $ext;
//3、把内存中的图片复制存储到硬盘中
move_uploaded_file($v['tmp_name'], $new_file);
//把路径存下来用于存数据库
$this->img = $new_file;
}
}
//生成缩略图
//大图
$this->new_get_thumImge(800, 800, 'big');
//中图
$this->new_get_thumImge(400, 400, 'middle');
//小图
$this->new_get_thumImge(200, 200, 'thumb');
unset($_SESSION['image'],$_SESSION['image_middle'],$_SESSION['image_thumb']);
$_SESSION['image']=$this->big_img;
$_SESSION['image_middle']=$this->middle_img;
$_SESSION['image_thumb']=$this->thumb_img;
output_data("上传文件成功");
}
/*缩略图*/ public function new_get_thumImge($new_width, $new_height, $style) { //1.创建新大小的图片 //参数 x ,y 分别为要创建图像的宽度和高度像素值,返回一个图像资源。 $new_img = imagecreatetruecolor($new_width, $new_height); //2.获取原图的信息 list($old_width, $old_height, $type) = getimagesize($this->img); //3.判断图片的类型 $type_array = [1 => 'gif', 2 => 'jpeg', 3 => 'png']; $type_str = $type_array[$type]; //4.imagecreatefrom 系列函数用于从文件或 URL 载入一幅图像,成功返回图像资源,失败则返回一个空字符串。 $fun = "imagecreatefrom" . $type_str; $old_image = $fun($this->img); //5.定坐标 $des_x = 0; //目标 X 坐标点。 $des_y = 0; //目标 Y 坐标点。 $src_x = 0; //源的 X 坐标点。 $src_y = 0; //源的 Y 坐标点。 //6.处理图片(核心) imagecopyresampled($new_img, $old_image, $des_x, $des_y, $src_x, $src_y, $new_width, $new_height, $old_width, $old_height); //7.创建新的路径存储图片 if ($style == "big") { //创建文件夹 $the_file = '../images/goods/big_img'; if (!file_exists($the_file)) { mkdir($the_file, 0777, true); } //8.拼接完整的文件路径 $the_file .= '/' . time() . mt_rand(10000, 99999) . '.' . $type_str; //9.复制新图片到硬盘中 imagejpeg($new_img, $the_file); unset($this->big_img); //10.把路劲存在数据库 $this->big_img = str_replace('..', '', $the_file); } elseif ($style == "middle") { //创建文件夹 $the_file = '../images/goods/middle_img'; if (!file_exists($the_file)) { mkdir($the_file, 0777, true); } //8.拼接完整的文件路径 $the_file .= '/' . time() . mt_rand(10000, 99999) . '.' . $type_str; //9.复制新图片到硬盘中 imagejpeg($new_img, $the_file); //10.把路劲存在数据库 unset($this->middle_img); $this->middle_img = str_replace('..', '', $the_file); return $this->middle_img; } elseif ($style == "thumb") { //创建文件夹 $the_file = '../images/goods/thumimge'; if (!file_exists($the_file)) { mkdir($the_file, 0777, true); } //8.拼接完整的文件路径 $the_file .= '/' . time() . mt_rand(10000, 99999) . '.' . $type_str; //9.复制新图片到硬盘中 imagejpeg($new_img, $the_file); //10.把路劲存在数据库 unset($this->thumb_img); $this->thumb_img = str_replace('..', '', $the_file); } }