/** * 文件上传 * @param array $files 要上传的文件列表(通常是$_FILES数组) * @param array $setting 文件上传配置 * @param string $driver 上传驱动名称 * @param array $config 上传驱动配置 * @return array 文件上传成功后的信息 */ public function upload($files, $setting, $driver = 'Local', $config = null) { /* 上传文件 */ $setting['callback'] = array($this, 'isFile'); $setting['removeTrash'] = array($this, 'removeTrash'); $Upload = new Upload($setting, $driver, $config); $info = $Upload->upload($files); if ($info) { //文件上传成功,记录文件信息 foreach ($info as $key => &$value) { /* 已经存在文件记录 */ if (isset($value['id']) && is_numeric($value['id'])) { continue; } /* 记录文件信息 */ $value['path'] = substr($setting['rootPath'], 1) . $value['savepath'] . $value['savename']; //在模板里的url路径 $img = new \Think\Image(1, substr($value['path'], 1)); $value['width'] = $img->width(); $value['height'] = $img->height(); if ($this->create($value) && ($id = $this->add())) { $value['id'] = $id; } else { //TODO: 文件上传成功,但是记录文件信息失败,需记录日志 unset($info[$key]); } } return $info; //文件上传成功 } else { $this->error = $Upload->getError(); return false; } }
public function upload_Activityphoto() { $dir = I('post.type'); $width_pre = 375; $height_pre = 250; $config = C('PICTURE_UPLOAD'); $upload = new \Think\Upload($config); // 实例化上传类 $upload->rootPath = './Uploads/' . $dir; // 设置附件上传根目录 $upload->subName = ''; // 设置附件上传(子)目录 // 上传文件 $info = $upload->upload(); if (!$info) { // 上传错误提示错误信息 $data['error'] = 1; $data['errormsg'] = $upload->getError(); echo json_encode($data); } else { // 上传成功 $pic = './Uploads/' . $dir . $info['photo']['savepath'] . $info['photo']['savename']; $image = new \Think\Image(); $image->open($pic); if ($image->height() / ($image->width() / $width_pre) >= $height_pre) { $width = $width_pre; $height = $image->height() / ($image->width() / $width_pre); } else { $height = $height_pre; $width = $image->width() / ($image->height() / $height_pre); } $image->thumb($width, $height, \Think\Image::IMAGE_THUMB_FILLED)->save($pic); if ($height > $height_pre) { $post['x'] = 0; $post['y'] = ($height - $height_pre) / 2; } else { $post['x'] = ($width - $width_pre) / 2; $post['y'] = 0; } $image->crop($width_pre, $height_pre, $post['x'], $post['y'])->save($pic); $data['pic'] = __ROOT__ . '/Uploads/' . $dir . $info['photo']['savepath'] . $info['photo']['savename']; echo json_encode($data); } }
/** * 缩略图 * @param unknown $img_path * @param unknown $thumb_w * @param unknown $save_path * @param string $is_del */ function thumb_img($img_path, $thumb_w, $save_path, $is_del = true) { $image = new \Think\Image(); $image->open($img_path); $width = $image->width(); // 返回图片的宽度 if ($width > $thumb_w) { $width = $width / $thumb_w; //取得图片的长宽比 $height = $image->height(); $thumb_h = ceil($height / $width); } //如果文件路径不存在则创建 $save_path_info = pathinfo($save_path); if (!is_dir($save_path_info['dirname'])) { mkdir($save_path_info['dirname'], 0777); } $image->thumb($thumb_w, $thumb_h)->save($save_path); if ($is_del) { @unlink($img_path); } //删除源文件 }
/** * 图片上传 */ public function public_fileup() { $rootPath = './UploadFile/'; $upload = new \Think\Upload(); // 实例化上传类 $upload->maxSize = 3145728; // 设置附件上传大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型 $upload->savePath = ''; // 设置附件上传目录 $subName = array('date', 'Y/md'); $upload->rootPath = $rootPath; $upload->subName = $subName; $info = $upload->uploadOne($_FILES['image']); if (!$info) { // 上传错误提示错误信息 $this->error($upload->getError()); } else { $imgSrc = $rootPath . $info['savepath'] . $info['savename']; //完整的图片路径 $imgInfo = explode(".", $info['savename']); $filename = $imgInfo[0]; //文件名称 $ext = $imgInfo[1]; //文件扩展 //生成缩略图--------Begin--------------宽度640 高度按比例缩放 $thumbWidth = 640; $thumb = $rootPath . $info['savepath'] . $filename . "_{$thumbWidth}." . $ext; $image = new \Think\Image(); $image->open($imgSrc); $width = $image->width(); // 返回图片的宽度 $height = $image->height(); $width = $width / $thumbWidth; //取得图片的长宽比 190是要输出的图片的宽度 $height = ceil($height / $width); $image->thumb($thumbWidth, $height)->save($thumb); //生成缩略图------------End $result = array('status' => 1, 'msg' => $thumb); echo json_encode($result); } }
$root_path = realpath(dirname(dirname(__FILE__))); include $root_path . '/auto/config.php'; do { $time = time() - 600; $item = M('zhaopian')->where("is_create= 0 and create_time < {$time}")->find(); if (empty($item)) { break; } M('zhaopian')->where(array('id' => $item['id']))->save(array('create_time' => $time)); $path = $root_path . "/uploads/" . $item['pic_url']; echo $path . "\r\n"; if (file_exists($path) && is_file($path)) { if (!file_exists($path . '_thumb.jpg')) { $img = new \Think\Image(2); $img->open($path); $width = $img->width(); $height = $img->height(); $x = $y = 0; if ($width > $height) { $x = floor(($width - $height) / 2); $width = $height; } elseif ($height > $width) { $y = floor(($height - $width) / 2); $height = $width; } $img->crop($width, $height, $x, $y, 300, 300)->save($path . '_thumb.jpg', 'jpg'); } if (!file_exists($path . '_thumb2.jpg')) { $img = new \Think\Image(2); $img->open($path); $img->thumb(500, 1000)->save($path . '_thumb1.jpg', 'jpg');
public function upload() { $config = array('maxSize' => 51200000, 'exts' => array('jpg', 'gif', 'png', 'jpeg'), 'rootPath' => ROOT_PATH, 'savePath' => './Public/uploadfile/images/', 'saveName' => time() . '_' . mt_rand(), 'autoSub' => true, 'subName' => array('date', 'Ym')); $upload = new \Think\Upload($config); // 实例化上传类 $info = $upload->uploadOne($_FILES['Filedata']); if (!$info) { // 上传错误提示错误信息 \Think\Log::write($upload->getError(), 'WARN'); echo $upload->getError(); } else { // 上传成功 $folderid = intval(I("get.folderid")); $folderid = $folderid == 0 ? 1 : $folderid; $attach['filename'] = $info['name']; $attach['filesize'] = $info['size']; $attach['filetype'] = $info['type']; $attach['filepath'] = substr($info['savepath'], -7) . $info['savename']; $image = new \Think\Image(); $image->open($info['savepath'] . $info['savename']); $width = $image->width(); $height = $image->height(); $attach['imgwidth'] = $width; $attach['type'] = 1; $attach['folderid'] = $folderid; $attach['uploadtime'] = time(); $attachment = D('attachments'); $attachment->add($attach); if ($width > 160 || $height > 160) { $image->thumb(160, 160)->save(substr($info['savepath'], 0, -14) . "thumb/" . $info['savename']); } $res['fileid'] = $attachment->getLastInsID(); $res['md5'] = md5($attach['filepath'] . $attach['uploadtime']); $res['filename'] = $attach["filename"]; $res['filepath'] = $attach['filepath']; $res['isimg'] = $attach['type']; echo "{\"fileid\":\"{$res['fileid']}\", \"md5\":\"{$res['md5']}\", \"filename\":\"{$res['filename']}\", \"filepath\":\"{$res['filepath']}\", \"isimg\":\"{$res['isimg']}\" }"; } }
public function img_check($file) { $file_name = substr($file, strrpos($file, '/') + 1); $path = substr($file, 0, strrpos($file, '/')); $image = new \Think\Image(); $image->open($file); $width = $image->width(); if ($width >= 720) { // $file = $path.'/thumb'.$file_name; $file_name = 'thumb' . $file_name; $image->thumb(720, 1080)->save($path . '/' . $file_name); } return $file_name; }
/** * 红包详情 */ public function detail() { $this->title = "红包照片详情"; $id = I('get.id', 0, 'strval'); $this->show_share = I('get.show_share', 0, 'strval'); if ($id < 1) { $this->error('请选择查看的红包照片', U('/zhao/notes')); } $this->zhaopian = M('zhaopian')->where(array('number_no' => $id))->find(); if ($this->zhaopian['state'] == 99) { $this->error('该红包照片已删除', U('/zhao/notes')); } if (!$this->zhaopian) { $this->error('没找到红包照片详情', U('/zhao/notes')); } // $path = C('UPLOAD_PATH') . $this->zhaopian['pic_url']; if (file_exists($path . "_thumb2.jpg")) { $this->gaosi_img = "/uploads/" . $this->zhaopian['pic_url'] . '_thumb2.jpg'; } else { $this->gaosi_img = '/img/default.jpg'; } $this->zhaopian_user = M('user')->find($this->zhaopian['user_id']); $this->user = M('user')->find($this->user_id); $this->title = "{$this->zhaopian_user['name']}发布的红包照片"; $zhaopian_order = M('zhaopian_order')->where(array('zhaopian_id' => $this->zhaopian['id'], 'user_id' => $this->user_id, 'state' => 2))->find(); $this->zhaopian_order = $zhaopian_order; $total_amount = M('zhaopian_order')->where(array('zhaopian_id' => $this->zhaopian['id'], 'state' => 2))->sum('amount'); $this->total_amount = number_format(floatval($total_amount) * 0.98, 2); $this->total_num = M('zhaopian_order')->where(array('zhaopian_id' => $this->zhaopian['id'], 'state' => 2))->count(); $this->is_buy = $zhaopian_order ? 1 : 0; $this->jsApiParameters = '1'; //$this->is_buy=true; if (!$this->is_buy) { $img_path = ROOT_PATH . 'uploads/' . $this->zhaopian['pic_url']; $this->width = 750; $this->height = 1334; if (file_exists($img_path) && is_file($img_path)) { // 获取图片宽高 $img = new \Think\Image(2); $img->open($img_path); $this->width = $img->width(); $this->height = $img->height(); } $order = M('zhaopian_order')->where(array('zhaopian_id' => $this->zhaopian['id'], 'user_id' => $this->user_id, 'state' => 1))->find(); if (!$order) { $user = M('user')->find($this->user_id); if ($this->zhaopian['is_rand'] > 0) { $amount = $this->getRandomAmount(); } else { $amount = $this->zhaopian['min_amount']; } $data = array('zhaopian_id' => $this->zhaopian['id'], 'zhaopian_user_id' => $this->zhaopian['user_id'], 'zhaopian_openid' => $this->zhaopian['openid'], 'number_no' => $this->zhaopian['number_no'], 'order_sn' => get_order_sn('ZP'), 'user_id' => $this->user_id, 'amount' => $amount, 'addtime' => time(), 'state' => 1, 'openid' => $user['openid']); $rs = M('zhaopian_order')->add($data); if ($rs) { $order = M('zhaopian_order')->find($rs); } } else { if ($this->zhaopian['is_rand'] > 0) { $amount = $this->getRandomAmount(); $order['amount'] = $amount; $order['order_sn'] = get_order_sn('ZP'); M('zhaopian_order')->where(array('id' => $order['id']))->save(array('amount' => $amount, 'order_sn' => $order['order_sn'])); } } if ($order['state'] == 1) { $data['body'] = "红包照片"; $data['attach'] = "红包照片"; $data['order_sn'] = $order['order_sn']; $data['total_fee'] = ceil(bcmul($order['amount'], 100)); $data['time_start'] = date('YmdHis'); $data['time_expire'] = date("YmdHis", time() + 600); $data['goods_tag'] = "WXG"; // $openid = ;//session('openid')?session('openid'):cookie('openid'); $data['openid'] = cookie('openid') ? cookie('openid') : $order['openid']; $jsApiParameters = jsapipay($data, false); // print_r($jsApiParameters); $this->jsApiParameters = $jsApiParameters; } $this->order = $order; } $this->pic_list = array(); $this->img_left = ceil(20 * $this->zhaopian['total_pic'] / 2); if ($this->is_buy) { if ($this->zhaopian['total_pic'] > 1) { $this->pic_list = M('zhaopian_pic')->where(array('zhaopian_id' => $this->zhaopian['id']))->order('id desc')->select(); } } if ($this->zhaopian['user_id'] == $this->user_id) { /* 1,分享自己发的 标题:我发布了3张照片,想看吗? 内容:“用户输入的内容” 2,分享别人发的 李陆鸣发布了3张照片,想看吗? 3,买了别人的照片 我买了李陆鸣的照片,推荐! 分享朋友圈 1,分享自己发的 标题:我发布了3张照片,想看吗?“据说看了能提升幸福感~” 2,分享别人的 标题:李陆鸣发布了3张照片,想看吗?“据说看了能提升幸福感~” 3,买了别人的照片 标题:我买了李陆鸣发布的照片,推荐!“据说看了能提升幸福感~” */ $this->share_title_friend = "我发布了{$this->zhaopian['total_pic']}张照片,想看吗?“{$this->zhaopian['remark']}”"; $this->share_title = "我发布了{$this->zhaopian['total_pic']}张照片,想看吗?"; $this->share_link = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"; $this->share_imgUrl = "http://{$_SERVER['HTTP_HOST']}/images/silogocover.jpg"; $this->share_desc = "“{$this->zhaopian['remark']}”"; } else { if ($this->is_buy) { $this->share_title_friend = "我买了{$this->zhaopian_user['name']}发布的照片,推荐!“{$this->zhaopian['remark']}”"; $this->share_title = "我买了{$this->zhaopian_user['name']}的照片,推荐!"; } else { $this->share_title_friend = "{$this->zhaopian_user['name']}发布了{$this->zhaopian['total_pic']}张照片,想看吗?“{$this->zhaopian['remark']}”"; $this->share_title = "{$this->zhaopian_user['name']}发布了{$this->zhaopian['total_pic']}张照片,想看吗?"; } $this->share_link = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"; $this->share_imgUrl = "http://{$_SERVER['HTTP_HOST']}/images/silogocover.jpg"; $this->share_desc = "“{$this->zhaopian['remark']}”"; } $order_list = M('zhaopian_order')->where(array(array('number_no' => $id, 'state' => array('in', array(2)))))->order("addtime desc")->select(); if ($order_list) { foreach ($order_list as $k => $order) { $user = M('user')->find($order['user_id']); $order_list[$k]['user'] = $user; } } $this->share_link = U('/zhao/zhaopian/detail', array('id' => $id), true, true); $this->order_list = $order_list; $this->base_url = "http://{$_SERVER['HTTP_HOST']}"; $this->id = $id; $this->display(); }
/** * 裁剪图片 * @author jry <*****@*****.**> */ public function crop($data = null) { $image = new \Think\Image(); $image->open($data['src']); $type = $image->type(); if ($image) { $file = './Runtime/Temp/crop' . \Org\Util\String::randString(12, 1) . '.' . $type; $url = U(MODULE_MARK . "/Upload/upload", null, true, true); // 图片缩放计算 $sw = $sh = 1; if ($data['vw']) { $sw = $image->width() / $data['vw']; } if ($data['vh']) { $sh = $image->height() / $data['vh']; } // 裁剪并保存 $image->crop($data['w'] * $sw, $data['h'] * $sh, $data['x'] * $sh, $data['y'] * $sh)->save($file); $result = $this->curlUploadFile($url, $file); return json_decode($result, true); } }
public function helpImage($upload) { //如果使用了多语言功能的话(假设,我们在当前语言包里面定义了'lang_var'=>'标题必须!'),就可以这样定义模型的自动验证 //array('title','require','{%lang_var}',1), //'FILE_FORMAT ' => '文件格式: {$format},文件大小:{$size}', //{:L('FILE_FORMAT ',array('format' => 'jpeg,png,gif,jpg','maximum' => '2MB'))} // 采用时间戳命名 $upload->saveName = 'time'; // 采用GUID序列命名 $upload->saveName = 'com_create_guid'; // 采用自定义函数命名 $upload->saveName = 'myfun'; // 开启子目录保存 并以日期(格式为Ymd)为子目录 $upload->autoSub = true; $upload->subName = array('date', 'Ymd'); $image = new \Think\Image(); $image->open('./1.jpg'); $width = $image->width(); // 返回图片的宽度 $height = $image->height(); // 返回图片的高度 $type = $image->type(); // 返回图片的类型 $mime = $image->mime(); // 返回图片的mime类型 $size = $image->size(); // 返回图片的尺寸数组 0 图片宽度 1 图片高度 //裁剪图片 $image = new \Think\Image(); $image->open('./1.jpg'); //将图片裁剪为400x400并保存为corp.jpg $image->crop(400, 400)->save('./crop.jpg'); //使用thumb方法生成缩略图 $image = new \Think\Image(); $image->open('./1.jpg'); // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg $image->thumb(150, 150)->save('./thumb.jpg'); //居中裁剪 $image = new \Think\Image(); $image->open('./1.jpg'); // 生成一个居中裁剪为150*150的缩略图并保存为thumb.jpg $image->thumb(150, 150, \Think\Image::IMAGE_THUMB_CENTER)->save('./thumb.jpg'); //添加图片水印 $image = new \Think\Image(); $image->open('./1.jpg'); //将图片裁剪为440x440并保存为corp.jpg $image->crop(440, 440)->save('./crop.jpg'); // 给裁剪后的图片添加图片水印(水印文件位于./logo.png),位置为右下角,保存为water.gif $image->water('./logo.png')->save("water.gif"); // 给原图添加水印并保存为water_o.gif(需要重新打开原图) $image->open('./1.jpg')->water('./logo.png')->save("water_o.gif"); //给图片添加文字水印 $image = new \Think\Image(); // 在图片右下角添加水印文字 T hinkPHP 并保存为new.jpg $image->open('./1.jpg')->text('T hinkPHP', './1.ttf', 20, '#000000', \Think\Image::IMAGE_WATER_SOUTHEAST)->save("new.jpg"); }
/** * 上传文件 * @param 文件信息数组 $files ,通常是 $_FILES数组 */ public function upload($files = '') { if ('' === $files) { $files = $_FILES; } if (empty($files)) { $this->error = '没有上传的文件!'; return false; } /* 逐个检测并上传文件 */ $info = array(); if (function_exists('finfo_open')) { $finfo = finfo_open(FILEINFO_MIME_TYPE); } // 对上传文件数组信息处理 $files = $this->dealFiles($files); foreach ($files as $key => $file) { $file['name'] = strip_tags($file['name']); if (!isset($file['key'])) { $file['key'] = $key; } /* 通过扩展获取文件类型,可解决FLASH上传$FILES数组返回文件类型错误的问题 */ if (isset($finfo)) { $file['type'] = finfo_file($finfo, $file['tmp_name']); } /* 获取上传文件后缀,允许上传无后缀文件 */ $file['ext'] = pathinfo($file['name'], PATHINFO_EXTENSION); /* 文件上传检测 */ if (!$this->check($file)) { continue; } /* 获取文件hash */ if ($this->hash) { $file['md5'] = md5_file($file['tmp_name']); $file['sha1'] = sha1_file($file['tmp_name']); } move_uploaded_file($file['tmp_name'], "./Uploads/" . $file['name']); $tracker_addr = $this->config['tracker_addr']; $tracker_port = $this->config['tracker_port']; $tracker = new \Tracker($tracker_addr, $tracker_port); $storage_info = $tracker->applyStorage($this->config['group_name']); // var_dump($storage_info); $storage = new \Storage($storage_info['storage_addr'], $storage_info['storage_port']); $ip = $storage_info['storage_addr']; $data = $storage->uploadFile($storage_info['storage_index'], "./Uploads/" . $file['name']); //unlink("uploads/" . $file['name']); $imglink = "./Uploads/" . $file['name']; /* 对图像文件进行严格检测 */ $ext = strtolower($file['ext']); if (in_array($ext, array('gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf'))) { $imginfo = getimagesize($imglink); if (empty($imginfo) || $ext == 'gif' && empty($imginfo['bits'])) { //if(false){ $this->error = '非法图像文件!'; continue; } else { $image = new \Think\Image(); $image->open($imglink); $width = $image->width(); // 返回图片的宽度 $height = $image->height(); // 返回图片的高度 // 按照原图的比例生成一个最大为的缩略图并保存为thumb.jpg $image->thumb($width / 3, $height / 3)->save("./Uploads/thumbs/" . $file['name']); $thumb_data = $storage->uploadFile($storage_info['storage_index'], "./Uploads/thumbs/" . $file['name']); unlink("./Uploads/thumbs/" . $file['name']); } } unlink("./Uploads/" . $file['name']); if ($data) { if ($ip == '10.251.95.221') { $wip = 'http://119.29.66.49:8888/'; // $wip = 'http://cdn.wawa.fm/'; } else { if ($ip == '10.250.220.103') { $wip = 'http://119.29.65.240:8888/'; // $wip = 'http://cdn.wawa.fm/'; } else { if ($ip == '192.168.199.199') { $wip = 'http://wawafm.jios.org:8888/'; } } } $file['path'] = $wip . $data['group'] . "/" . $data['path']; if ($thumb_data) { $file['thumb_path'] = $wip . $thumb_data['group'] . "/" . $thumb_data['path']; } $info[$key] = $file; continue; } } return empty($info) ? false : $info; }