public static function setUserPicPath() { // 根据ID计算出目标路径 //$uploaddirPre = self::$_config['IMAGE_UPLOAD']['FILE_PATH'] .self::$_config['SHOP_UPLOAD']['FILE_URL']; self::$_uploadDir = IdPath3($_POST['id'], self::$_config['IMAGE_UPLOAD']['FILE_PATH'] . self::$_config['SHOP_UPLOAD_USER']['FILE_URL']); // 如果目录不存在,则创建目录 if (!is_dir(self::$_uploadDir)) { MkBaseDir(self::$_uploadDir); } }
/** * 异步上传图片imagick处理 */ public function imagickUpload() { // 大尺寸图片上传时内存需要增大 ini_set("memory_limit", "100M"); // 配置文件 global $uploadConfig; // 图片上传全局配置 $imageUpload = $uploadConfig['IMAGE_UPLOAD']; // 上传错误 $uploadError = $uploadConfig['UPLOAD_ERROR']; // 文件信息 $_FILENAME = $_FILES[$imageUpload['FILE_ID']]["name"]; //文件名 $_FILESIZE = $_FILES[$imageUpload['FILE_ID']]["size"]; //文件大小 $_FILE = $_FILES[$imageUpload['FILE_ID']]["tmp_name"]; //临时文件名 $_FILEERROR = $_FILES[$imageUpload['FILE_ID']]["error"]; //上传错误 // 判断是否是上传的文件 if (!is_uploaded_file($_FILE)) { callBack(array('status' => 'error', 'tip' => $uploadError['MSG_FILEPATHERROR'])); } // 上传模块类型 $type = isset($_POST['subtype']) ? $_POST['subtype'] : ''; if (empty($type)) { callBack(array('status' => 'error', 'tip' => $uploadError['MSG_MODULETYPRERROR'])); //模块类型缺失 } // 图片类型 switch ($type) { //百科文章封面图片 case "article": $moduleUpload = $uploadConfig['BAIKE_ARTICLE_COVER_UPLOAD']; break; //百科分类封面图片 //百科分类封面图片 case "category": $moduleUpload = $uploadConfig['BAIKE_CATEGORY_COVER_UPLOAD']; break; //百科小组头像 //百科小组头像 case "team": $moduleUpload = $uploadConfig['BAIKE_TEAM_COVER_UPLOAD']; break; //百科专家头像 //百科专家头像 case "expert": $moduleUpload = $uploadConfig['BAIKE_EXPERT_UPLOAD']; break; //个人空间后台推送图片 //个人空间后台推送图片 case "push": $moduleUpload = $uploadConfig['BAIKE_PUSH_UPLOAD']; break; //生活馆后台推送图片 //生活馆后台推送图片 case "vetIndAd_20000": $moduleUpload = $uploadConfig['VET_PUSH_UPLOAD0']; break; //个人空间后台推送图片 //个人空间后台推送图片 case "vetIndAd_20001": $moduleUpload = $uploadConfig['VET_PUSH_UPLOAD1']; break; //个人空间后台推送图片 //个人空间后台推送图片 case "vetIndAd_20002": $moduleUpload = $uploadConfig['VET_PUSH_UPLOAD2']; break; //个人空间后台推送图片 //个人空间后台推送图片 case "vetIndAd_20003": $moduleUpload = $uploadConfig['VET_PUSH_UPLOAD3']; break; //主站首页推送图片 //主站首页推送图片 case "indexpush": $moduleUpload = $uploadConfig['WWW_PUSH_UPLOAD']; break; //宠物种类 //宠物种类 case "pettype": $moduleUpload = $uploadConfig['BAIKE_PETTYPE_UPLOAD']; break; //百科广告图片 //百科广告图片 case "ads": $moduleUpload = $uploadConfig['BAIKE_ADS_UPLOAD']; break; //个人空间相册 //个人空间相册 case "album": $moduleUpload = $uploadConfig['ALBUM_UPLOAD']; break; //个人中心用户头像 //个人中心用户头像 case "useravatar": $moduleUpload = $uploadConfig['USER_AVATAR_UPLOAD']; break; //个人空间宠物头像 //个人空间宠物头像 case "petavatar": $moduleUpload = $uploadConfig['PET_AVATAR_UPLOAD']; break; //移动个人空间宠物头像 //移动个人空间宠物头像 case "mpetavatar": $moduleUpload = $uploadConfig['MOBILE_PET_AVATAR_UPLOAD']; break; //个人空间微博图片 //个人空间微博图片 case "weibo": $moduleUpload = $uploadConfig['WEIBO_UPLOAD']; break; //商家封面图片 //商家封面图片 case "business": $moduleUpload = $uploadConfig['BUSINESS_COVER_UPLOAD']; break; //商家人员图片 //商家人员图片 case "business_expert": $moduleUpload = $uploadConfig['BUSINESS_EXPERT_UPLOAD']; break; //商家环境图片 //商家环境图片 case "business_environment": $moduleUpload = $uploadConfig['BUSINESS_ENVIRONMENT_UPLOAD']; break; //商家作品图片 //商家作品图片 case "business_writing": $moduleUpload = $uploadConfig['BUSINESS_WRITING_UPLOAD']; break; //商家图片 //商家图片 case "business_pic": $moduleUpload = $uploadConfig['BUSINESS_PIC_UPLOAD']; break; //商家优惠券图片 //商家优惠券图片 case "business_coupon": $moduleUpload = $uploadConfig['BUSINESS_COUPON_UPLOAD']; break; //商家资质图片 //商家资质图片 case "business_ualification": $moduleUpload = $uploadConfig['VET_BUSINESS_UALIFICATION_UPLOAD']; break; //商家合同图片 //商家合同图片 case "business_contract": $moduleUpload = $uploadConfig['VET_BUSINESS_CONTRACT_UPLOAD']; break; //店员图片 //店员图片 case "business_member": $moduleUpload = $uploadConfig['BUSINESS_MEMBER_UPLOAD']; break; //生活馆广告图片 //生活馆广告图片 case "vet_ads": $moduleUpload = $uploadConfig['VET_ADS_UPLOAD']; break; // 移动论坛帖子模块 // 移动论坛帖子模块 case "bbs": $moduleUpload = $uploadConfig['BBS_POST_UPLOAD']; break; //版块图标 //版块图标 case "bbs_forum": $moduleUpload = $uploadConfig['BBS_FORUM_UPLOAD']; break; //版块推送 //版块推送 case "bbs_push": $moduleUpload = $uploadConfig['BBS_PUSH_UPLOAD']; break; //图片帖封面 //图片帖封面 case "bbs_picthread": $moduleUpload = $uploadConfig['BBS_PICTHREAD_UPLOAD']; break; //奖品图片 //奖品图片 case "bet": $moduleUpload = $uploadConfig['WWW_BET_UPLOAD']; break; //主站专题图片 //主站专题图片 case "subject": $moduleUpload = $uploadConfig['WWW_SUBJECT_UPLOAD']; break; //个人签名图片 //个人签名图片 case "sightml": $moduleUpload = $uploadConfig['SIGHTML_UPLOAD']; break; //资讯图片 //资讯图片 case "news": $moduleUpload = $uploadConfig['VET_NEWS_UPLOAD']; break; //专题图片 //专题图片 case "subtem": $moduleUpload = $uploadConfig['UC_SUBTEM_UPLOAD']; break; // app问答图片 // app问答图片 case "apppost": $moduleUpload = $uploadConfig['APP_BAIKE_POST_UPLOAD']; break; // app宠物头像 // app宠物头像 case "apppet": $moduleUpload = $uploadConfig['APP_PET_AVATAR_UPLOAD']; break; // app服务券评论 // app服务券评论 case "appcomment": $moduleUpload = $uploadConfig['APP_COUPON_COMMENT_UPLOAD']; break; // 主站意见反馈图片 // 主站意见反馈图片 case "complaints": $moduleUpload = $uploadConfig['WWW_COMPLAINTS_UPLOAD']; break; // 标签图标 // 标签图标 case "tag": $moduleUpload = $uploadConfig['WWW_TAG_UPLOAD']; break; default: callBack(array('status' => 'error', 'tip' => $uploadError['MSG_MODULETYPRERROR'])); //模块类型缺失 break; } if (isset($moduleUpload['FILE_SIZE'])) { // 限制文件大小 if ($_FILEERROR == 1 || $_FILEERROR == 2 || $_FILESIZE > $moduleUpload['FILE_SIZE']) { callBack(array('status' => 'error', 'tip' => $uploadError['MSG_FILESIZE_ERROR'])); //请保持图片不超过指定大小 } } else { // 限制文件大小 if ($_FILEERROR == 1 || $_FILEERROR == 2 || $_FILESIZE > $imageUpload['FILE_SIZE']) { callBack(array('status' => 'error', 'tip' => $uploadError['MSG_FILESIZEBIGERROR'])); //请保持图片不超过2M } } // 图片过大时会导致POST为空(POST为空时默认为图片超过指定大小的错误) if (empty($_POST)) { callBack(array('status' => 'error', 'tip' => $uploadError['MSG_FILESIZEBIGERROR'])); //请保持图片不超过2M } // 保存路径 // 使用绝对地址 $path = $imageUpload['FILE_PATH'] . $moduleUpload['FILE_URL'] . "/"; //物理地址 //图片URL路径 $fileUrl = $moduleUpload['FILE_URL'] . "/"; //相对路径 // 路径扩展方式(TIME:时间;UID:用户id;YM:年月;NO:不需要) $pathExt = isset($moduleUpload['PATH_EXT']) && !empty($moduleUpload['PATH_EXT']) ? $moduleUpload['PATH_EXT'] : 'NO'; // 时间扩展 if ($pathExt == 'TIME') { $path .= date("ym") . "/" . date("j") . "/"; $fileUrl .= date("ym") . "/" . date("j") . "/"; } elseif ($pathExt == 'UID') { // 图片文件保存目录 $path = IdPath3($_POST['id'], $path); $fileUrl = IdPath3($_POST['id'], $fileUrl); } elseif ($pathExt == 'YM') { $path .= date("Ym") . "/"; $fileUrl .= date("Ym") . "/"; } // 如果目录不存在,则创建目录 if (!is_dir($path)) { MkBaseDir($path); } // 图片命名 switch ($type) { case "useravatar": $timeName = "avatar" . time(); break; case "petavatar": $timeName = "petavatar" . time(); break; case "mpetavatar": $timeName = "petavatar" . time(); break; case "apppet": $timeName = "petavatar" . time(); break; case "expert": $timeName = "eavatar" . time(); break; case "indexpush": $timeName = date('YmdHis', time()); break; default: $timeName = "imagick" . rand(1, 10000) . time(); break; } // 引入图片处理类 require_once 'ImagickService.php'; $imagick = new ImagickService(); // 载入上传图片 $imagick->open($_FILE); // 原图宽度 $imageInfo[0] = $imagick->get_width(); // 原图高度 $imageInfo[1] = $imagick->get_height(); // 判断是否图片 if (!$imagick->is_image()) { callBack(array('status' => 'error', 'tip' => $uploadError['MSG_FILEPATHERROR'])); //文件源地址错误,获取不到图片 } // 判断图片类型 $imgtype = $imagick->get_type(); switch ($imgtype) { case 'gif': self::$_imageExt = "gif"; break; case 'jpeg': self::$_imageExt = "jpg"; break; case 'jpg': self::$_imageExt = "jpg"; break; case 'png': self::$_imageExt = "png"; break; case 'bmp': self::$_imageExt = "bmp"; break; default: callBack(array('status' => 'error', 'tip' => $imgtype . $uploadError['MSG_FILETYPEERROR'])); //文件类型错误 } if (isset($moduleUpload['DOMAIN'])) { $domain = $moduleUpload['DOMAIN'] . '/'; } else { $domain = "http://" . $_SERVER['HTTP_HOST'] . "/"; } // 保存文件名 $ofilepath = $path . $timeName . '.' . self::$_imageExt; // 生成原图 $oimage = move_uploaded_file($_FILE, $ofilepath); if (!$oimage) { callBack(array('status' => 'error', 'tip' => $uploadError['MSG_UPLOADFALSEERROR'])); //文件上传错误(服务器故障上传失败,请稍后再试!) } // bmp图片 if ($imgtype == 'bmp') { $returnUrl = $fileUrl . $timeName . '.' . self::$_imageExt; } elseif ($imgtype == 'gif') { $returnUrl = $fileUrl . $timeName . '.' . self::$_imageExt; } else { // 是否需要图片截取 if (isset($moduleUpload['INTERCEPT']) && $moduleUpload['INTERCEPT'] == 1) { $returnUrl = $fileUrl . $timeName . '.' . self::$_imageExt; } else { // 原图宽度 $image2X = $imagick->get_width(); // 原图高度 $image2Y = $imagick->get_height(); //是否需要限制上传图片固定宽高 if (isset($moduleUpload['UPLOAD_WH_LIMIT']) && $moduleUpload['UPLOAD_WH_LIMIT'] == 2) { // 宽度不对 if ($image2X != $moduleUpload['UPLOAD_WIDTH_LIMIT']) { callBack(array('status' => 'error', 'tip' => $uploadError['MSG_UPLOAD_WIDTH_FALSE'] . '应为' . $moduleUpload['UPLOAD_WIDTH_LIMIT'] . 'px!')); } // 高度不对 if ($image2Y != $moduleUpload['UPLOAD_HEIGHT_LIMIT']) { callBack(array('status' => 'error', 'tip' => $uploadError['MSG_UPLOAD_HEIGHT_FALSE'] . '应为' . $moduleUpload['UPLOAD_HEIGHT_LIMIT'] . 'px!')); } } if (isset($moduleUpload['IMAGE_SUFFIX'])) { // 水印图片路径 $waterImage = $imageUpload['FILE_PATH'] . "Data/Public/watermark_new.png"; // 水印配置项 $watermarkConfig = $uploadConfig['WATERMARK']; // 图片后缀 $imageSuffixs = explode(",", $moduleUpload['IMAGE_SUFFIX']); foreach ($imageSuffixs as $key => $val) { // 重新读入原图文件,防止原有信息对压缩图影响 $imagick->reload($ofilepath); //图片需要压缩 if (isset($moduleUpload['COMPRESS']) && $moduleUpload['COMPRESS'] == 1) { $cFilepath = $path . $timeName . $val . '.' . self::$_imageExt; $cMaxwidth = isset($moduleUpload['UPLOAD_WIDTH' . strtoupper($val)]) ? $moduleUpload['UPLOAD_WIDTH' . strtoupper($val)] : $image2X; $cMaxheight = isset($moduleUpload['UPLOAD_HEIGHT' . strtoupper($val)]) ? $moduleUpload['UPLOAD_HEIGHT' . strtoupper($val)] : $image2Y; // 是否最小边压缩 if (isset($moduleUpload['IAMGE_COMPRESS']) && $moduleUpload['IAMGE_COMPRESS'] == 'MIN_EDGE') { list($cx, $cy) = getallsizebymin($image2X, $image2Y, $cMaxwidth, $cMaxheight); } else { list($cx, $cy) = getallsize($image2X, $image2Y, $cMaxwidth, $cMaxheight); } } elseif (isset($moduleUpload['COMPRESS']) && $moduleUpload['COMPRESS'] == 2) { // 是否需要超过指定尺寸拉伸图片处理 if (isset($moduleUpload['UPLOAD_WIDTH_COMPRESS']) && $val == '_o') { $cFilepath = $path . $timeName . $val . '.' . self::$_imageExt; // 小于指定尺寸,则显示默认图片 TODO if ($image2X < $moduleUpload['UPLOAD_WIDTH_COMPRESS']) { $isCoverDefault = 1; //$returnUrl = $moduleUpload['IMAGE_PATH_DEFAULT']; // O图宽度(原图尺寸) $imageInfo[0] = $cx = $image2X; // O图高度(原图尺寸) $imageInfo[1] = $cy = $image2Y; } else { // O图宽度(设定宽度) $imageInfo[0] = $cx = $image2X; // O图高度(等比压缩) $imageInfo[1] = $cy = $image2Y; } } else { $cFilepath = $path . $timeName . $val . '.' . self::$_imageExt; if ($val == '_y') { continue; } else { $cx = $moduleUpload['UPLOAD_WIDTH' . strtoupper($val)]; $cy = $moduleUpload['UPLOAD_HEIGHT' . strtoupper($val)]; } } } if ($cx && $cy) { // 压缩 $imagick->resize($cx, $cy); // 保存图片 $imagick->save_to($cFilepath); } //图片需要加水印 if (isset($moduleUpload['WATERMARK']) && $moduleUpload['WATERMARK'] == 1 && $watermarkConfig['WATERMARK_IMAGE_WIDTH']) { if ($cx >= $watermarkConfig['WATERMARK_IMAGE_WIDTH'] && $cy >= $watermarkConfig['WATERMARK_IMAGE_HEIGHT']) { // 加水印 $imagick->add_watermark($waterImage, $cx, $cy); // 保存图片 $imagick->save_to($cFilepath); } } } //是否需要限制上传图片最低宽高 if (isset($moduleUpload['UPLOAD_WH_LIMIT']) && $moduleUpload['UPLOAD_WH_LIMIT'] == 1) { // 宽度不够 if ($image2X < $moduleUpload['UPLOAD_WIDTH_LIMIT']) { callBack(array('status' => 'error', 'tip' => $uploadError['MSG_UPLOAD_WIDTH_LIMIT_FALSE'])); //宽度不足 } // 高度不够 if ($image2Y < $moduleUpload['UPLOAD_HEIGHT_LIMIT']) { callBack(array('status' => 'error', 'tip' => $uploadError['MSG_UPLOAD_HEIGHT_LIMIT_FALSE'])); //高度不足 } // 比例不对 if (floatval($image2X / $image2Y) > floatval($moduleUpload['UPLOAD_RATIO_LIMIT'] + 0.0015) || floatval($image2X / $image2Y) < floatval($moduleUpload['UPLOAD_RATIO_LIMIT'] - 0.0015)) { callBack(array('status' => 'error', 'tip' => $uploadError['MSG_UPLOAD_RATIO_LIMIT_FALSE'])); //图片宽高比不对 } } //是否需要App端压缩 if ($moduleUpload['APP_IMAGE_SUFFIX']) { $appImageSuffixs = explode(",", $moduleUpload['APP_IMAGE_SUFFIX']); foreach ($appImageSuffixs as $key => $val) { $cFilepath = $path . $timeName . $val . '.' . self::$_imageExt; //压缩图宽 $twidth = isset($moduleUpload['UPLOAD_WIDTH' . $val]) ? $moduleUpload['UPLOAD_WIDTH' . $val] : $image2X; //压缩图高 $theight = isset($moduleUpload['UPLOAD_HEIGHT' . $val]) ? $moduleUpload['UPLOAD_HEIGHT' . $val] : $image2Y; //实际图宽 $width = $image2X; //实际图高 $height = $image2Y; //计算截图坐标 //目标图片比源图片更宽更高 if ($twidth > $width || $theight > $height) { $t = round($twidth / $theight, 2); //目标图片宽高比 $s = round($width / $height, 2); //原图宽高比 $cimg = $fileUrl . $timeName . '.' . self::$_imageExt; //以图片高为标准,截取宽度 if ($t < $s) { $realw = ceil($width * round($twidth / $theight, 2)); $realh = $height; $data['px'] = floor(($width - $realw) / 2); //截图X坐标为px $data['py'] = 0; //截图Y坐标为py $data['pw'] = $width - $realw; //截图宽度为pw $data['ph'] = $realh; //截图高度为ph } else { $realw = $width; $realh = ceil($width * round($theight / $twidth, 2)); $data['px'] = 0; $data['py'] = ceil(($height - $realh) / 2); $data['pw'] = $realw; $data['ph'] = $realh; } } else { //图片先压缩 $t = round($twidth / $theight, 2); //目标图片宽高比 $s = round($width / $height, 2); //原图宽高比 if ($t < $s) { $cx = ceil($theight * round($width / $height, 2)); $cy = $theight; $data['px'] = ceil(($cx - $twidth) / 2); $data['pw'] = $twidth; $data['py'] = 0; $data['ph'] = $cy; } else { $cx = $twidth; $cy = ceil($twidth * round($height / $width, 2)); $data['px'] = 0; $data['pw'] = $cx; $data['py'] = ceil(($cy - $theight) / 2); $data['ph'] = $theight; } // imagick //$imagick->clear(); // 重新载入图片 //$imagick->open($oFilepath); $imagick->reload($oFilepath); // 压缩图片 $imagick->resize($cx, $cy); // 保存图片 $imagick->save_to($cFilepath); $cimg = $domain . $fileUrl . $timeName . $val . '.' . self::$_imageExt; } $imagick->cut($data['pw'], $data['ph'], $data['px'], $data['py']); // 保存图片 $imagick->save_to($cFilepath); } } if ($returnUrl) { } elseif (strpos($moduleUpload['IMAGE_SUFFIX'], '_o') !== false) { $returnUrl = $fileUrl . $timeName . '_o' . '.' . self::$_imageExt; } elseif (strpos($moduleUpload['IMAGE_SUFFIX'], '_y') !== false) { $returnUrl = $fileUrl . $timeName . '_y' . '.' . self::$_imageExt; } elseif (strpos($moduleUpload['IMAGE_SUFFIX'], '_b') !== false) { $returnUrl = $fileUrl . $timeName . '_b' . '.' . self::$_imageExt; if ($type == 'news') { $imageInfo[0] = $moduleUpload['UPLOAD_WIDTH_B']; $imageInfo[1] = $moduleUpload['UPLOAD_HEIGHT_B']; } } else { $returnUrl = $fileUrl . $timeName . '.' . self::$_imageExt; } } else { $returnUrl = $fileUrl . $timeName . '.' . self::$_imageExt; } } } //上传成功,返回访问url以及图片目录 callBack(array('status' => 'ok', 'imgpath' => $returnUrl, 'domain' => $domain, 'width' => $imageInfo[0], 'height' => $imageInfo[1], "size" => $_FILESIZE, 'is_cover_default' => $isCoverDefault)); }