function upload() { $path = "./uploads/"; //设置图片上传路径 $up = new FileUpload($path); //创建文件上传类对象 if ($up->upload('pic')) { //上传图片 $filename = $up->getFileName(); //获取上传后的图片名 $img = new Image($path); //创建图像处理类对象 $img->thumb($filename, 300, 300, ""); //将上传的图片都缩放至在300X300以内 $img->thumb($filename, 80, 80, "icon_"); //缩放一个80x80的图标,使用icon_作前缀 $img->watermark($filename, "logo.gif", 5, ""); //为上传的图片加上图片水印 return array(true, $filename); //如果成功返回成功状态和图片名称 } else { return array(false, $up->getErrorMsg()); //如果失败返回失败状态和错误消息 } }
public function index() { echo ROOT_PATH . '/code.jpeg'; $img = new Image('/code.jpeg'); $img->thumb(100, 200); $img->out(); }
private function save($file) { $filename = $file['savepath'] . $file['savename']; if (!$this->uploadReplace && is_file($filename)) { $this->error = LParse(L('_FILE_REPLACE_ERROR_'), array($filename)); return false; } if (!move_uploaded_file($file['tmp_name'], auto_charset($filename, 'utf-8', 'gbk'))) { $this->error = L('_FILE_MOVE_ERROR_'); return false; } if ($this->thumb) { import("ORG.Util.Image"); $image = Image::getImageInfo($filename); if (false !== $image) { $thumbWidth = explode(',', $this->thumbMaxWidth); $thumbHeight = explode(',', $this->thumbMaxHeight); $thumbPrefix = explode(',', $this->thumbPrefix); $thumbSuffix = explode(',', $this->thumbSuffix); $thumbPath = $this->thumbPath ? $this->thumbPath : $file['savepath']; for ($i = 0, $len = count($thumbWidth); $i < $len; $i++) { $thumbname = $thumbPath . $thumbPrefix[$i] . substr($file['savename'], 0, strrpos($file['savename'], '.')) . $thumbSuffix[$i] . '.' . $file['extension']; Image::thumb($filename, '', $thumbname, $thumbWidth[$i], $thumbHeight[$i], true); } } } if ($this->zipImages) { // TODO Image compression package on-line decompression } return true; }
private function thumb_href($source_path, $width, $height) { if (!file_exists($source_path)) { return null; } $name = 'thumb-' . sha1($source_path) . '-' . $width . 'x' . $height . '.jpg'; $thumb_path = $this->thumbs_path . '/' . $name; $thumb_href = $this->thumbs_href . '/' . $name; if (!file_exists($thumb_path) || filemtime($source_path) >= filemtime($thumb_path)) { $image = new Image(); $et = false; if ($this->setup->get('HAS_PHP_EXIF') && $this->context->query_option('thumbnails.exif', false) === true && $height != 0) { $et = @exif_thumbnail($source_path); } if ($et !== false) { file_put_contents($thumb_path, $et); $image->set_source($thumb_path); $image->normalize_exif_orientation($source_path); } else { $image->set_source($source_path); } $image->thumb($width, $height); $image->save_dest_jpeg($thumb_path, 80); } return file_exists($thumb_path) ? $thumb_href : null; }
private function thumb_href($source_abs_path, $mode, $width, $height) { if (!file_exists($source_abs_path)) { return null; } if (!is_dir($this->thumbs_path)) { @mkdir($this->thumbs_path, 0755, true); } $name = "thumb-" . sha1("{$source_abs_path}-{$width}-{$height}-{$mode}") . ".jpg"; $thumb_abs_path = $this->thumbs_path . "/" . $name; $thumb_abs_href = $this->thumbs_href . "/" . $name; if (!file_exists($thumb_abs_path) || filemtime($source_abs_path) >= filemtime($thumb_abs_path)) { $image = new Image(); $et = false; $opts = $this->app->get_options(); if ($opts["thumbnails"]["exif"] === true && function_exists("exif_thumbnail")) { $et = @exif_thumbnail($source_abs_path); } if ($et !== false) { file_put_contents($thumb_abs_path, $et); $image->set_source($thumb_abs_path); $image->normalize_exif_orientation($source_abs_path); } else { $image->set_source($source_abs_path); } $image->thumb($mode, $width, $height); $image->save_dest_jpeg($thumb_abs_path, 80); } return file_exists($thumb_abs_path) ? $thumb_abs_href : null; }
function Thumb($img, $width = '', $height = '') { $thumb_img = 'Public/thumb.jpg'; if ($img) { $img = substr($img, 1); $is_img = GetImageSize('./' . $img); //设置宽高 生成缩略图 if (!empty($width) && !empty($height)) { import('ORG.Util.Image'); $Image = new Image(); $filename = 'Uploads/thumb/' . $width . '_' . $height . '/'; //缩略图存储路径 $new_name = strtr($img, array('/' => '_')); if (!file_exists($filename)) { @mkdir($filename, 0755); } if ($is_img) { $is_thumb = GetImageSize('./' . $filename . $new_name); if ($is_thumb) { $thumb_img = $filename . $new_name; } else { $Image->thumb($img, $filename . $new_name, '', $width, $height); $thumb_img = $filename . $new_name; } } } else { if ($is_img) { $thumb_img = $img; } } } return '/' . $thumb_img; }
public function upload() { if (isset($_FILES['userfile']['tmp_name'])) { switch ($_POST['type']) { case 'face': $width = 99; $height = 100; $info = '头像上传成功'; break; case 'ok': $width = 300; $height = 300; $info = '图片上传成功'; break; case 'rotator': $width = 1200; $height = 530; $info = '轮播器图片上传成功'; break; default: exit('非法操作'); } $upload = new UploadFile('userfile', $_POST['MAX_FILE_SIZE']); $path = $upload->getPath(); $thumb = new Image($path); $thumb->thumb($width, $height); $thumb->outImage(); $upload->alertThumbClose($info, $path); } else { Tool::alertBack('警告:未知错误'); } }
public function down_img($url, $mid = 'video') { $chr = strrchr($url, '.'); $imgUrl = uniqid(); $imgPath = $mid . '/' . date(C('upload_style'), time()) . '/'; $imgPath_s = './' . C('upload_path') . '-s/' . $imgPath; $filename = './' . C('upload_path') . '/' . $imgPath . $imgUrl . $chr; $get_file = get_collect_file($url); if ($get_file) { write_file($filename, $get_file); //是否添加水印 if (C('upload_water')) { import('ORG.Util.Image'); Image::water($filename, C('upload_water_img'), '', C('upload_water_pct'), C('upload_water_pos')); } //是否生成缩略图 if (C('upload_thumb')) { mkdirss($imgPath_s); import('ORG.Util.Image'); Image::thumb($filename, $imgPath_s . $imgUrl . $chr, '', C('upload_thumb_w'), C('upload_thumb_h'), true); } //是否上传远程 if (C('upload_ftp')) { $this->ftp_upload($imgPath . $imgUrl . $chr); } return $imgPath . $imgUrl . $chr; } else { return $url; } }
private function thumb_href($source_path, $width, $height) { if (!file_exists($source_path)) { return null; } $name = "thumb-" . sha1("{$source_path}") . "-" . $width . "x" . $height . ".jpg"; $thumb_path = $this->thumbs_path . "/" . $name; $thumb_url = $this->thumbs_href . "/" . $name; if (!file_exists($thumb_path) || filemtime($source_path) >= filemtime($thumb_path)) { $image = new Image(); $et = false; $opts = $this->app->get_options(); if (HAS_PHP_EXIF && $opts["thumbnails"]["exif"] === true && $height != 0) { $et = @exif_thumbnail($source_path); } if ($et !== false) { file_put_contents($thumb_path, $et); $image->set_source($thumb_path); $image->normalize_exif_orientation($source_path); } else { $image->set_source($source_path); } $image->thumb($width, $height); $image->save_dest_jpeg($thumb_path, 80); } return file_exists($thumb_path) ? $thumb_url : null; }
public function uploadImg() { if (!$this->is_login()) { return; } import('ORG.Net.UploadFile'); import('ORG.Util.Image'); $where['id'] = array('in', array('1', '2', '3', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '48')); $setInfo = $this->uploadSet->where($where)->select(); $imgAllowExts = explode(',', $setInfo[2]['value']); $thumbMax = explode('x', $setInfo[5]['value']); $imageMax = explode('x', $setInfo[4]['value']); if (!file_exists('./' . $setInfo[0]['value'])) { mkdir('./' . $setInfo[0]['value']); } $savepath = './' . $setInfo[0]['value']; if ('1' == $setInfo[1]['value']) { $savepath = $savepath . '/' . date('Ymd') . '/'; } else { if (!file_exists($savepath . '/' . date('Ym'))) { mkdir($savepath . '/' . date('Ym')); } $savepath = $savepath . '/' . date('Ym') . '/' . date('d') . '/'; } if (!file_exists($savepath)) { mkdir($savepath); } $config = array('maxSize' => $setInfo[3]['value'] * 1024, 'supportMulti' => true, 'allowExts' => $imgAllowExts, 'thumb' => true, 'thumbMaxWidth' => $thumbMax[0], 'thumbMaxHeight' => $thumbMax[1], 'thumbPrefix' => 'thumb_', 'thumbSuffix' => '', 'thumbPath' => $savepath, 'thumbFile' => '', 'thumbExt' => '', 'thumbRemoveOrigin' => false, 'zipImages' => false, 'autoSub' => false, 'subType' => 'hash', 'dateFormat' => 'Ymd', 'hashLevel' => 1, 'savePath' => $savepath, 'autoCheck' => true, 'uploadReplace' => false, 'saveRule' => 'uniqid', 'hashType' => 'md5_file'); $upload = new UploadFile($config); // 实例化上传类 $image = new Image(); if (!$upload->upload()) { // 上传错误提示错误信息 $msg = $upload->getErrorMsg(); print_r("{\"status\": 0, \"msg\":\"" . $msg . "\"}"); } else { // 上传成功 获取上传文件信息 $info = $upload->getUploadFileInfo(); // $imgPath = substr($info[0]['savepath'].$info[0]['savename'],1); $imgPath = $info[0]['savepath'] . $info[0]['savename']; $thumb = $info[0]['savepath'] . 'thumb_' . $info[0]['savename']; $image->thumb($imgPath, $info[0]['savepath'] . 'headb_' . $info[0]['savename'], '', 160, 160); $image->thumb($imgPath, $info[0]['savepath'] . 'headm_' . $info[0]['savename'], '', 60, 60); print_r("{\"status\": 1,\"thumb\":\"" . substr($thumb, 1) . "\", \"path\": \"" . substr($imgPath, 1) . "\"}"); } }
public function create($force = 0) { if ($force === 2 || $force === 1 && !file_exists($this->path) || file_exists($this->path) && filemtime($this->srcAbsPath) >= filemtime($this->path)) { $image = new Image(); $image->setSource($this->srcAbsPath); $image->thumb($this->mode, $this->width, $this->height); $image->saveDest($this->path); } }
function saveAvatar($uid, $faceurl) { $this->uid = $uid; $original = $this->getSavePath() . "/original.jpg"; $big = $this->getSavePath() . "/big.jpg"; $small = $this->getSavePath() . "/small.jpg"; include SITE_PATH . '/addons/libs/Image.class.php'; Image::thumb($faceurl, $original, '', 150, 150); Image::thumb($faceurl, $big, '', 120, 120); Image::thumb($faceurl, $small, '', 50, 50); }
public function upload() { echo '<div style="font-size:12px; height:30px; line-height:30px">'; $uppath = './' . C('upload_path') . '/'; $uppath_s = './' . C('upload_path') . '-s/'; $mid = trim($_POST['mid']); $fileback = !empty($_POST['fileback']) ? trim($_POST['fileback']) : 'picurl'; if ($mid) { $uppath .= $mid . '/'; $uppath_s .= $mid . '/'; $backpath = $mid . '/'; } import("ORG.Net.UploadFile"); $up = new UploadFile(); //$up->maxSize = 3292200; $up->savePath = $uppath; $up->saveRule = uniqid; $up->uploadReplace = true; $up->allowExts = explode(',', C('cms_exts')); $up->autoSub = true; $up->subType = date; $up->dateFormat = C('upload_style'); if (!$up->upload()) { $error = $up->getErrorMsg(); if ($error == '上传文件类型不允许') { $error .= ',可上传<font color=red>' . C('cms_exts') . '</font>'; } exit($error . ' [<a href="?s=Admin/Upload/Show/mid/' . $mid . '/fileback/' . $fileback . '">重新上传</a>]'); //dump($up->getErrorMsg()); } $uploadList = $up->getUploadFileInfo(); //是否添加水印 if (C('upload_water')) { import("ORG.Util.Image"); Image::water($uppath . $uploadList[0]['savename'], C('upload_water_img'), '', C('upload_water_pct'), C('upload_water_pos')); } //是否生成缩略图 if (C('upload_thumb')) { $thumbdir = substr($uploadList[0]['savename'], 0, strrpos($uploadList[0]['savename'], '/')); mkdirss($uppath_s . $thumbdir); import("ORG.Util.Image"); Image::thumb($uppath . $uploadList[0]['savename'], $uppath_s . $uploadList[0]['savename'], '', C('upload_thumb_w'), C('upload_thumb_h'), true); } //是否远程图片 if (C('upload_ftp')) { $img = D('Down'); $img->ftp_upload($backpath . $uploadList[0]['savename']); } echo "<script type='text/javascript'>parent.document.getElementById('" . $fileback . "').value='" . $backpath . $uploadList[0]['savename'] . "';</script>"; echo '文件<a href="' . $uppath . $uploadList[0]['savename'] . '" target="_blank"><font color=red>' . $uploadList[0]['savename'] . '</font></a>上传成功 [<a href="?s=Admin/Upload/Show/mid/' . $mid . '/fileback/' . $fileback . '">重新上传</a>]'; echo '</div>'; }
public function upLoad() { if (isset($_POST['send'])) { $_fileupload = new FileUpload('pic', $_POST['MAX_FILE_SIZE']); $_path = $_fileupload->getPath(); $_img = new Image($_path); $_img->thumb(300, 300); $_img->out(); $_fileupload->alertOpenerClose('图片上传成功', '.' . $_path); } else { exit('警告:文件过大或者其他未知错误导致浏览器崩溃'); } }
function thumb_images($pic) { $array_dir = explode('/', $pic); $date_dir = $array_dir[3]; $image_name = $array_dir[5]; //组装缩略图路径 $big = C('Goods_image_path') . '/' . $date_dir . '/big/b_' . $image_name; $middle = C('Goods_image_path') . '/' . $date_dir . '/middle/m_' . $image_name; $small = C('Goods_image_path') . '/' . $date_dir . '/small/s_' . $image_name; import("ORG.Util.Image"); Image::thumb($pic, $big, '', C('Goods_big_width'), C('Goods_big_height')); Image::thumb($pic, $middle, '', C('Goods_middle_width'), C('Goods_middle_width')); Image::thumb($pic, $small, '', C('Goods_small_width'), C('Goods_small_width')); }
public function user_thumb($id, $img) { $img_path = avatar_dir($id); $avatar_size = explode(',', C('ftx_avatar_size')); $paths = C('ftx_attach_path'); foreach ($avatar_size as $size) { if ($paths . 'avatar/' . $img_path . '/' . md5($id) . '_' . $size . '.jpg') { @unlink($paths . 'avatar/' . $img_path . '/' . md5($id) . '_' . $size . '.jpg'); } !is_dir($paths . 'avatar/' . $img_path) && mkdir($paths . 'avatar/' . $img_path, 0777, true); Image::thumb($paths . 'avatar/temp/' . $img, $paths . 'avatar/' . $img_path . '/' . md5($id) . '_' . $size . '.jpg', '', $size, $size, true); } @unlink($paths . 'avatar/temp/' . $img); }
private function thumb_href($source_abs_path, $mode, $width, $height) { if (!file_exists($source_abs_path)) { return null; } $name = "thumb-" . sha1("{$source_abs_path}-{$width}-{$height}-{$mode}") . ".jpg"; $thumb_abs_path = $this->app->get_cache_abs_path() . "/" . $name; $thumb_abs_href = $this->app->get_cache_abs_href() . $name; if (!file_exists($thumb_abs_path) || filemtime($source_abs_path) >= filemtime($thumb_abs_path)) { $image = new Image(); $image->set_source($source_abs_path); $image->thumb($mode, $width, $height); $image->save_dest_jpeg($thumb_abs_path, 80); } return file_exists($thumb_abs_path) ? $thumb_abs_href : null; }
private function save($file) { $filename = $file['savepath'] . $file['savename']; @unlink($filename); if (!$this->uploadReplace && is_file($filename)) { $this->error = '文件已经存在!' . $filename; return false; } if (in_array(strtolower($file['extension']), array('gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf')) && false === getimagesize($file['tmp_name'])) { $this->error = '非法图像文件'; return false; } if (!move_uploaded_file($file['tmp_name'], $this->autoCharset($filename, 'utf-8', 'gbk'))) { $this->error = '文件上传保存错误!'; return false; } if ($this->thumb && in_array(strtolower($file['extension']), array('gif', 'jpg', 'jpeg', 'bmp', 'png'))) { $image = getimagesize($filename); if (false !== $image) { $thumbWidth = explode(',', $this->thumbMaxWidth); $thumbHeight = explode(',', $this->thumbMaxHeight); $thumbPrefix = explode(',', $this->thumbPrefix); $thumbSuffix = explode(',', $this->thumbSuffix); $thumbFile = explode(',', $this->thumbFile); $thumbPath = $this->thumbPath ? $this->thumbPath : dirname($filename) . '/'; $thumbExt = $this->thumbExt ? $this->thumbExt : $file['extension']; import($this->imageClassPath); //echo($this->imageClassPath);exit; for ($i = 0, $len = count($thumbWidth); $i < $len; $i++) { if (!empty($thumbFile[$i])) { $thumbname = $thumbFile[$i]; } else { $prefix = isset($thumbPrefix[$i]) ? $thumbPrefix[$i] : $thumbPrefix[0]; $suffix = isset($thumbSuffix[$i]) ? $thumbSuffix[$i] : $thumbSuffix[0]; $thumbname = $prefix . basename($filename, '.' . $file['extension']) . $suffix; } Image::thumb($filename, $thumbPath . $thumbname . '.' . $thumbExt, '', $thumbWidth[$i], $thumbHeight[$i], true); } if ($this->thumbRemoveOrigin) { unlink($filename); } } } if ($this->zipImags) { } return true; }
public function saveImage() { $this->allowtypes = array('jpg', 'jpeg', 'png', 'gif'); $this->maxsize = 1024 * 1024 * 5; $filename = $this->setfilename(); $filepath = date('Y') . '/' . date('m') . '/' . $filename; $thumb = 'thumb/' . $filepath; $attachment = 'photo/' . $filepath; if ($this->save(C('ATTACHDIR') . $attachment)) { $image = new Image(C('ATTACHDIR') . $attachment); $image->thumb(210, 210); $image->save(C('ATTACHDIR') . $thumb); return array('name' => $filename, 'width' => $image->width(), 'height' => $image->height(), 'type' => $image->type(), 'filesize' => $this->size(), 'attachment' => $attachment, 'thumb' => $thumb); } else { return false; } }
private function thumb_href($sourceAbsPath, $mode, $width, $height) { if (!file_exists($sourceAbsPath)) { return null; } $name = "cache/thumb-" . sha1("{$sourceAbsPath}-{$width}-{$height}-{$mode}") . ".jpg"; // $name = "cache/thumb-" . sha1("$sourceAbsPath-$width-$height-$mode") . ".png"; $thumbAbsHref = $this->h5ai->getH5aiAbsHref() . $name; $thumbAbsPath = $this->h5ai->getH5aiAbsPath() . "/" . $name; if (!file_exists($thumbAbsPath) || filemtime($sourceAbsPath) >= filemtime($thumbAbsPath)) { $image = new Image(); $image->setSource($sourceAbsPath); $image->thumb($mode, $width, $height); $image->saveDestJpeg($thumbAbsPath, 80); // $image->saveDestPng($thumbAbsPath, 9); // Magic::thumb($mode, $sourceAbsPath, $thumbAbsPath, $width, $height); } return file_exists($thumbAbsPath) ? $thumbAbsHref : null; }
static function thumb($image_url, $w = 200, $h = 200) { if (preg_match('@http://@i', $image_url)) { return $image_url; } if (func_num_args() == 2) { $h = $w; } if ($image_url == '') { return ''; } $result_url = $image_url . '__' . $w . '_' . $h . '.jpg'; if (!file_exists(APP_ROOT . $result_url)) { $image = new Image(); $image->cut = true; $image->suffix = 'f_w_h.jpg'; $result_url = $image->thumb(APP_ROOT . $image_url, $w, $h); $result_url = str_replace(APP_ROOT, '', $result_url); } return Url::urlFormat('@' . $result_url); }
function upload() { @header("Expires: 0"); @header("Cache-Control: private, post-check=0, pre-check=0, max-age=0", FALSE); @header("Pragma: no-cache"); $pic_id = time(); //使用时间来模拟图片的ID. $pic_path = $this->getSavePath() . '/original.jpg'; $pic_abs_path = __UPLOAD__ . '/avatar/' . $this->uid . '/original.jpg'; //保存上传图片. if (empty($_FILES['Filedata'])) { $return['message'] = '对不起, 图片未上传成功, 请再试一下'; $return['code'] = '0'; } else { $file = @$_FILES['Filedata']['tmp_name']; file_exists($pic_path) && @unlink($pic_path); if (@copy($_FILES['Filedata']['tmp_name'], $pic_path) || @move_uploaded_file($_FILES['Filedata']['tmp_name'], $pic_path)) { @unlink($_FILES['Filedata']['tmp_name']); /*list($width, $height, $type, $attr) = getimagesize($pic_path); if($width < 10 || $height < 10 || $width > 3000 || $height > 3000 || $type == 4) { @unlink($pic_path); return -2; }*/ include SITE_PATH . '/addons/libs/Image.class.php'; Image::thumb($pic_path, $pic_path, '', 300, 300); list($sr_w, $sr_h, $sr_type, $sr_attr) = @getimagesize($pic_path); $return['data']['picurl'] = 'data/uploads/avatar/' . $this->uid . '/original.jpg'; $return['data']['picwidth'] = $sr_w; $return['data']['picheight'] = $sr_h; $return['code'] = '1'; } else { @unlink($_FILES['Filedata']['tmp_name']); $return['message'] = '对不起, 图片未上传成功'; $return['code'] = '0'; } } return json_encode($return); }
public function action() { $options = Typecho_Widget::widget('Widget_Options'); if (!isset($options->plugins['activated']['TeThumbnail'])) { exit; } $siteUrl = $options->siteUrl; $options = $options->plugin('TeThumbnail'); $url = $this->request->get('url'); $size = $this->request->get('size'); if ($size) { $size = explode('x', $size); } else { $size = array($options->width, $options->height); } $path = __TYPECHO_ROOT_DIR__ . '/' . $url; if (!is_file($path)) { exit; } require_once 'Image.php'; $image = new Image(); $image->open($path); $type = $image->type(); $image->thumb($size[0], $size[1], 3); header('Content-Type:image/' . $type . ';'); //输出图像 if ('jpeg' == $type || 'jpg' == $type) { // 采用jpeg方式输出 imagejpeg($image->showImg()); } elseif ('gif' == $type) { imagegif($image->showImg()); } else { $fun = 'image' . $type; $fun($image->showImg()); } }
/** * @brief 生成缩略图 * @param string $imgSrc 图片路径 * @param int $width 图片宽度 * @param int $height 图片高度 * @return string WEB图片路径名称 */ public static function get($imgSrc, $width = 100, $height = 100) { if ($imgSrc == '') { return ''; } //商品物理实际路径 $imgArr = explode('@', $imgSrc); $preThumb = "{$width}_{$height}_"; if (count($imgArr) > 1) { $sourcePath = tool::getGlobalConfig(array('rootDir', $imgArr[1])) . '/' . trim($imgArr[0], '/'); $thumbFileName = $preThumb . basename($imgArr[0]); //缩略图文件名 } else { $sourcePath = trim($imgSrc, '/'); $thumbFileName = $preThumb . basename($imgSrc); } //缩略图目录 $thumbDir = self::getThumbDir() . '/'; $webThumbDir = self::$thumbDir . '/'; if (is_file($thumbDir . $thumbFileName) == false && is_file($sourcePath)) { Image::thumb($sourcePath, $width, $height, $preThumb, $thumbDir); } return url::getBaseUrl() . '/' . $webThumbDir . $thumbFileName; }
/** * 上传一个文件 * @access public * @param mixed $name 数据 * @param string $value 数据表名 * @return string */ private function save($file) { $filename = $file['savepath'] . $file['savename']; if (!$this->uploadReplace && is_file($filename)) { // 不覆盖同名文件 $this->error = '文件已经存在!' . $filename; return false; } // 如果是图像文件 检测文件格式 if (in_array(strtolower($file['extension']), array('gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf'))) { $info = getimagesize($file['tmp_name']); if (!$info && isset($file['size']) && $file['size'] > 0) { $info = $file['size']; } if (false === $info || 'gif' == strtolower($file['extension']) && empty($info['bits'])) { $this->error = '非法图像文件'; return false; } } if (!move_uploaded_file($file['tmp_name'], $this->autoCharset($filename, 'utf-8', 'gbk'))) { $this->error = '文件上传保存错误!'; return false; } if ($this->thumb && in_array(strtolower($file['extension']), array('gif', 'jpg', 'jpeg', 'bmp', 'png'))) { $image = getimagesize($filename); if (false !== $image) { //是图像文件生成缩略图 $thumbWidth = explode(',', $this->thumbMaxWidth); $thumbHeight = explode(',', $this->thumbMaxHeight); $thumbPrefix = explode(',', $this->thumbPrefix); $thumbSuffix = explode(',', $this->thumbSuffix); $thumbFile = explode(',', $this->thumbFile); $thumbPath = $this->thumbPath ? $this->thumbPath : dirname($filename) . '/'; $thumbExt = $this->thumbExt ? $this->thumbExt : $file['extension']; //自定义缩略图扩展名 // 生成图像缩略图 import($this->imageClassPath); for ($i = 0, $len = count($thumbWidth); $i < $len; $i++) { if (!empty($thumbFile[$i])) { $thumbname = $thumbFile[$i]; } else { $prefix = isset($thumbPrefix[$i]) ? $thumbPrefix[$i] : $thumbPrefix[0]; $suffix = isset($thumbSuffix[$i]) ? $thumbSuffix[$i] : $thumbSuffix[0]; $thumbname = $prefix . basename($filename, '.' . $file['extension']) . $suffix; } Image::thumb($filename, $thumbPath . $thumbname . '.' . $thumbExt, '', $thumbWidth[$i], $thumbHeight[$i], true); } if ($this->thumbRemoveOrigin) { // 生成缩略图之后删除原图 unlink($filename); } } } if ($this->zipImags) { // TODO 对图片压缩包在线解压 } return true; }
public function uploadImg() { import('ORG.Net.UploadFile'); import('ORG.Util.Image'); $where['id'] = array('in', array('1', '2', '3', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '48', '52', '53')); $setInfo = $this->uploadSet->where($where)->select(); $imgAllowExts = explode(',', $setInfo[2]['value']); $thumbMax = explode('x', $setInfo[5]['value']); $imageMax = explode('x', $setInfo[4]['value']); $bthumbMax = explode('x', $setInfo[14]['value']); $sthumbMax = explode('x', $setInfo[15]['value']); if (!file_exists('./' . $setInfo[0]['value'])) { mkdir('./' . $setInfo[0]['value']); } $savepath = './' . $setInfo[0]['value']; if ('1' == $setInfo[1]['value']) { $savepath = $savepath . '/' . date('Ymd') . '/'; } else { if (!file_exists($savepath . '/' . date('Ym'))) { mkdir($savepath . '/' . date('Ym')); } $savepath = $savepath . '/' . date('Ym') . '/' . date('d') . '/'; } if (!file_exists($savepath)) { mkdir($savepath); } $config = array('maxSize' => $setInfo[3]['value'] * 1024, 'supportMulti' => true, 'allowExts' => $imgAllowExts, 'thumb' => true, 'thumbMaxWidth' => $thumbMax[0], 'thumbMaxHeight' => $thumbMax[1], 'thumbPrefix' => 'thumb_', 'thumbSuffix' => '', 'thumbPath' => $savepath, 'thumbFile' => '', 'thumbExt' => '', 'thumbRemoveOrigin' => false, 'zipImages' => false, 'autoSub' => false, 'subType' => 'hash', 'dateFormat' => 'Ymd', 'hashLevel' => 1, 'savePath' => $savepath, 'autoCheck' => true, 'uploadReplace' => false, 'saveRule' => 'uniqid', 'hashType' => 'md5_file'); $upload = new UploadFile($config); // 实例化上传类 $image = new Image(); if (!$upload->upload()) { // 上传错误提示错误信息 $msg = $upload->getErrorMsg(); print_r("{\"status\": 0, \"msg\":\"" . $msg . "\"}"); } else { // 上传成功 获取上传文件信息 $info = $upload->getUploadFileInfo(); // $imgPath = substr($info[0]['savepath'].$info[0]['savename'],1); $imgPath = $info[0]['savepath'] . $info[0]['savename']; $imgInfo = $image->getImageInfo($imgPath); $thumb = $info[0]['savepath'] . 'thumb_' . $info[0]['savename']; if ($imageMax[0] < $imgInfo['width'] || $imageMax[1] < $imgInfo['height']) { $image->thumb($imgPath, $imgPath, '', $imageMax[0], $imageMax[1]); } $image->thumb($imgPath, $info[0]['savepath'] . 'bthumb_' . $info[0]['savename'], '', $bthumbMax[0], $bthumbMax[1]); $image->thumb($imgPath, $info[0]['savepath'] . 'sthumb_' . $info[0]['savename'], '', $sthumbMax[0], $sthumbMax[1]); if (2 == $setInfo[6]['value']) { import(APP_NAME . ".Action.WaterMask"); $water = new WaterMask($imgPath); $water->waterType = 0; //水印类型:0为文字水印、1为图片水印 $water->pos = $setInfo[7]['value']; //水印位置 $water->transparent = $setInfo[10]['value'] * 10; //水印透明度 $water->waterStr = $setInfo[11]['value']; //水印文字 $water->fontFile = "./Public/font/" . $setInfo[12]['value'] . ".ttf"; //字体名称 $water->fontSize = $setInfo[13]['value']; //字体大小 $water->fontColor = array(255, 0, 255); //水印文字颜色(RGB值) $water->output(); //输出水印图片 } elseif (3 == $setInfo[6]['value']) { import(APP_NAME . ".Action.WaterMask"); $water = new WaterMask($imgPath); $water->waterType = 1; //水印类型:0为文字水印、1为图片水印 $water->pos = $setInfo[7]['value']; //水印位置 $water->transparent = $setInfo[10]['value'] * 10; //水印透明度 $water->waterStr = $setInfo[11]['value']; //水印文字 $water->fontFile = "./Public/font/" . $setInfo[12]['value'] . ".ttf"; //字体名称 $water->fontSize = $setInfo[13]['value']; //字体大小 $water->fontColor = array(255, 0, 255); //水印文字颜色(RGB值) $water->waterImg = "./Public/waterImg/" . $setInfo[9]['value']; //水印图片 $water->output(); //输出水印图片 } else { } //写入登录日志 $l = M('managerlog'); $data['user_id'] = $_SESSION['userID']; $data['user_name'] = $_SESSION['userName']; $data['action_type'] = "uploadImg"; $data['remark'] = "上传图片"; $data['user_ip'] = $_SERVER["REMOTE_ADDR"]; $data['add_time'] = date('y-m-d h:i:s', time()); $data['time'] = time(); $flag = $l->add($data); if ($flag) { print_r("{\"status\": 1,\"thumb\":\"" . substr($thumb, 1) . "\", \"path\": \"" . substr($imgPath, 1) . "\"}"); } else { $this->error('用户日志操作失败,请重试'); } } }
/** * Add a group */ public function add($params) { $this->setView('add.php'); $this->setTitle(__('GROUP_ADD_TITLE')); $is_logged = isset(User_Model::$auth_data); $is_admin = $is_logged && User_Model::$auth_data['admin'] == '1'; // Authorization if (!$is_admin) { throw new ActionException('Page', 'error404'); } $group = array(); // Saving data if (isset($_POST['name']) && isset($_POST['creation_date']) && isset($_POST['mail']) && isset($_POST['description'])) { $uploaded_files = array(); try { // Members $members = array(); if (isset($_POST['members_ids']) && is_array($_POST['members_ids'])) { foreach ($_POST['members_ids'] as $id) { if (ctype_digit($id)) { $id = (int) $id; $members[$id] = array('title' => isset($_POST['member_title_' . $id]) ? $_POST['member_title_' . $id] : '', 'admin' => isset($_POST['member_admin_' . $id])); } } } // Other info $data = array('name' => $_POST['name'], 'creation_date' => $_POST['creation_date'], 'mail' => $_POST['mail'], 'description' => $_POST['description'], 'members' => $members); // Avatar if (isset($_FILES['avatar']) && !is_array($_FILES['avatar']['name'])) { if ($_FILES['avatar']['size'] > Config::UPLOAD_MAX_SIZE_PHOTO) { throw new FormException('avatar'); } if ($avatarpath = File::upload('avatar')) { $uploaded_files[] = $avatarpath; try { $img = new Image(); $img->load($avatarpath); $type = $img->getType(); if ($type == IMAGETYPE_JPEG) { $ext = 'jpg'; } else { if ($type == IMAGETYPE_GIF) { $ext = 'gif'; } else { if ($type == IMAGETYPE_PNG) { $ext = 'png'; } else { throw new Exception(); } } } if ($img->getWidth() > 800) { $img->setWidth(800, true); } $img->setType(IMAGETYPE_JPEG); $img->save($avatarpath); // Thumb $avatarthumbpath = $avatarpath . '.thumb'; $img->thumb(Config::$AVATARS_THUMBS_SIZES[0], Config::$AVATARS_THUMBS_SIZES[1]); $img->setType(IMAGETYPE_JPEG); $img->save($avatarthumbpath); unset($img); $uploaded_files[] = $avatarthumbpath; $data['avatar_path'] = $avatarthumbpath; $data['avatar_big_path'] = $avatarpath; } catch (Exception $e) { throw new FormException('avatar'); } } } $url_name = $this->model->create($data); Routes::redirect('group', array('group' => $url_name)); } catch (FormException $e) { foreach ($uploaded_files as $uploaded_file) { File::delete($uploaded_file); } foreach ($data as $key => $value) { $group[$key] = $value; } $group['members'] = Student_Model::getInfoByUsersIds(array_keys($members)); foreach ($group['members'] as &$member) { if (isset($members[(int) $member['user_id']])) { $member['title'] = $members[(int) $member['user_id']]['title']; $member['admin'] = $members[(int) $member['user_id']]['admin'] ? '1' : '0'; } } $this->set('form_error', $e->getError()); } } $this->set('group', $group); $this->addJSCode('Group.initEdit();'); }
public function uploadFace() { //关闭水印 C('WATER_ON', false); $dir = 'upload/user/' . date("Y/m/d/"); $upload = new Upload($dir); $file = $upload->upload(); if (empty($file)) { $this->ajax(array('status' => 0, 'error' => $file->error)); } else { $file = $file[0]; $img = new Image(); $img->thumb($file['path'], $file['path'], 250, 250, 6); $this->ajax(array('status' => 1, 'url' => $file['url'], 'path' => $file['path'])); } }
$data['stat'] = 1; $data['url'] = __ROOT__ . '/' . $file[0]['path']; $data['path'] = $file[0]['path']; $data['isimage'] = in_array(strtolower($file[0]['ext']), array("gif", "png", "jpeg", "jpg")) ? 1 : 0; $data['thumb'] = array(); //缩略图文件 if (isset($_POST['hdphp_upload_thumb'])) { $size = explode(",", $_POST['hdphp_upload_thumb']); //获得缩略数数量 $fileInfo = pathinfo($data['path']); //获得文件名信息 $image = new Image(); $id = 0; //缩略图ID $thumbFile = array(); //缩略图 $saveDir = dirname($data['path']); for ($i = 0, $total = count($size); $i < $total;) { $toFile = $fileInfo['filename'] . '_' . $size[$i] . 'x' . $size[$i + 1] . '.' . $fileInfo['extension']; $thumbFile[] = $saveDir . '/' . $toFile; $image->thumb($data['path'], $toFile, "", $size[$i], $size[$i + 1]); $i += 2; } $data['thumb'] = $thumbFile; } } else { $data['stat'] = 0; $data['msg'] = $upload->error; } echo json_encode($data); exit;
/** +---------------------------------------------------------- * 上传一个文件 +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param mixed $name 数据 * @param string $value 数据表名 +---------------------------------------------------------- * @return string +---------------------------------------------------------- * @throws ThinkExecption +---------------------------------------------------------- */ private function save($file) { $filename = $file['savepath'] . $file['savename']; if (!$this->uploadReplace && is_file($filename)) { // 不覆盖同名文件 $this->error = '文件已经存在!' . $filename; return false; } if (!move_uploaded_file($file['tmp_name'], auto_charset($filename, 'utf-8', 'gbk'))) { $this->error = '文件上传保存错误!'; return false; } if ($this->thumb) { // 生成图像缩略图 import("@.ORG.Image"); $image = Image::getImageInfo($filename); if (false !== $image) { //是图像文件生成缩略图 $thumbWidth = explode(',', $this->thumbMaxWidth); $thumbHeight = explode(',', $this->thumbMaxHeight); $thumbPrefix = explode(',', $this->thumbPrefix); $thumbSuffix = explode(',', $this->thumbSuffix); $thumbFile = explode(',', $this->thumbFile); $thumbPath = $this->thumbPath ? $this->thumbPath : $file['savepath']; for ($i = 0, $len = count($thumbWidth); $i < $len; $i++) { $thumbname = $thumbPath . $thumbPrefix[$i] . substr($file['savename'], 0, strrpos($file['savename'], '.')) . $thumbSuffix[$i] . '.' . $file['extension']; Image::thumb($filename, $thumbname, '', $thumbWidth[$i], $thumbHeight[$i], true); } if ($this->thumbRemoveOrigin) { // 生成缩略图之后删除原图 unlink($filename); } } } if ($this->zipImags) { // TODO 对图片压缩包在线解压 } return true; }