<?php defined('BASEPATH') OR exit('No direct script access allowed'); include_once (APPPATH . "controllers/admin/Auth.php"); include_once (APPPATH.'libraries/PicUpload.php'); class goodsInfo extends Auth { //产品修改 public function edit(){ $tid=$this->newrow['tid']; //获取待修改的商品详细图片信息 $ree['info']=$this->db->select('*')->from('goods_info')->where(array('tid'=>$tid))->get()->result_array(); //根据id从硬盘上删除产品原图及缩略图和产品成分图片 $pic=$this->db->select('ingredients,origin_pic,home_pic,product_cat_pic,detail_pic,cart_pic') ->from('goods_info') ->where(array('tid'=>$tid)) ->get() ->result_array(); $picture_upload=new PicUpload(); if(!empty($this->newrow['origin_pic'])){ unlink(OMG.ORIGIN.$pic[0]['origin_pic']); unlink(OMG.HOME.$pic[0]['home_pic']); unlink(OMG.PRODUCT.$pic[0]['product_cat_pic']); unlink(OMG.DETAIL.$pic[0]['detail_pic']); unlink(OMG.CART.$pic[0]['cart_pic']); $pic=$_POST['origin_pic']; //文件上传保存路径 $image_file=OMG.ORIGIN; $upload_url=$picture_upload->pUpload($pic,'pic2',$image_file); $origin_pic=$upload_url['url_pic']; $origin_thumb_pic=OMG.ORIGIN.$origin_pic; $thumb_name=$upload_url['pri']; if(!$upload_url){ responseMsg ( 1, "图片上传失败!", null, 1, $this->prefix ); $this->db->trans_rollback(); return FALSE; } for($i=1;$i<5;$i++){ if($i==1){ $width = 408; $height = 408; //缩略后的保存路径 $save_home_pic=OMG.HOME."{$thumb_name}.png"; $home_pic="{$thumb_name}_thumb.png"; //生成缩略图1 //$origin_thumb_pic 要缩略的原图路径 $this->dealthumb($origin_thumb_pic,$save_home_pic,$width,$height); }elseif($i==2){ $width = 270; $height = 270; //缩略后的保存路径 $save_product_cat_pic=OMG.PRODUCT."{$thumb_name}.png"; $product_cat_pic="{$thumb_name}_thumb.png"; //生成缩略图2 //$origin_thumb_pic 要缩略的原图路径 $this->dealthumb($origin_thumb_pic,$save_product_cat_pic,$width,$height); }elseif($i==3){ $width = 680; $height = 680; //缩略后的保存路径 $save_detail_pic=OMG.DETAIL."{$thumb_name}.png"; $detail_pic="{$thumb_name}_thumb.png"; //生成缩略图3 //$origin_thumb_pic 要缩略的原图路径 $this->dealthumb($origin_thumb_pic,$save_detail_pic,$width,$height); }else{ $width = 170; $height= 170; //缩略后的保存路径 $save_cart_pic=OMG.CART."{$thumb_name}.png"; $cart_pic="{$thumb_name}_thumb.png"; //生成缩略图4 //$origin_thumb_pic 要缩略的原图路径 $this->dealthumb($origin_thumb_pic,$save_cart_pic,$width,$height); } } }else { $origin_pic = $ree['info'][0]['origin_pic']; $home_pic = $ree['info'][0]['home_pic']; $product_cat_pic = $ree['info'][0]['product_cat_pic']; $detail_pic = $ree['info'][0]['detail_pic']; $cart_pic = $ree['info'][0]['cart_pic']; //添加图片1 $pic = $_POST['ingredients']; //文件上传保存路径 $image_file = OMG . INGREDIENTS; } responseMsg ( 0, "success",null, 1,$this->prefix ); } /* * 生成缩略图 * @param $path 原图的本地路径 * @param $newPath 缩略图存储路径 */ private function dealthumb($path,$newPath,$width,$height){ $config['image_library'] = 'gd2'; $config['source_image'] = $path; $config['new_image'] = $newPath; $config['create_thumb'] = TRUE; //生成的缩略图将在保持纵横比例 在宽度和高度上接近所设定的width和height $config['maintain_ratio'] = TRUE; $config['width'] = $width; $config['height'] = $height; //载入图像处理类库 $this->load->library('Image_lib'); $this->image_lib->initialize($config); $this->image_lib->resize(); $this->image_lib->clear(); } }
gmapiframeworkapplicationlibrariesPicUpload.php
<?php $file = substr( $_SERVER["SCRIPT_FILENAME"], 0, stripos($_SERVER["SCRIPT_FILENAME"], "framework") ); require_once("{$file}/config.php"); class PicUpload{ /** * $pic $name $image_file */ public function pUpload($pic,$name, $image_file){ $url_pic=array(); //上传图片 if(!empty($pic)){ $url_pic=$this->upload($pic,$name,$image_file); if(empty($url_pic)){ return false; } } $url=array( url_pic=>$url_pic['image_name'], pri=>$url_pic['pri'] ); return $url; } //字符串转图片 private function upload($base64_image,$name,$image_file){ $file_size=strlen($base64_image); //4.8kb 4925 8k // 验证文件条件是否符合 if($file_size<= 0){ # 未上传文件 return false; } if($file_size>= 4088000){ # 图片大于400KB,结束传输 return false; } $base64_image = str_replace(' ', '+',$base64_image);//post if (preg_match('/^(data:s*image/(w+);base64,)/', $base64_image, $result)){ //匹配成功 if($result[2] == 'jpeg'){ $pri=date(YmdHis).$name; $image_name =$pri .'.png'; }else{ $pri=date(YmdHis).$name; $image_name =$pri.'.'.$result[2]; } $image_file_1="{$image_file}{$image_name}"; //服务器文件存储路径 //$image_file_1 要写入数据的文件。如果文件不存在,则创建一个新文件。 //base64_decode(str_replace($result[1], '', $base64_image)) 要写入文件的数据。可以是字符串、数组或数据流。 if(file_put_contents($image_file_1, base64_decode(str_replace($result[1], '', $base64_image)))){ $data['image_name'] = $image_name; $data['pri']=$pri; return $data; }else{ return false; } }else{ return false; } } }