Example #1
0
 public static function cropImage($dataPathRoot, $imageFileRelativeName, $imageThumbFileRelativeName, $width, $height)
 {
     $cloudStorage = CloudHelper::getCloudModule(CloudHelper::CLOUD_MODULE_STORAGE);
     $tempSrcFilePath = $cloudStorage->createTempFileForStorageFile($dataPathRoot, $imageFileRelativeName);
     $tempDestFilePath = $cloudStorage->getTempFilePath();
     //生成缩略图
     if (extension_loaded('imagick')) {
         // 如果有 imagick 模块,优先选择 imagick 模块,因为生成的图片质量更高
         $img = new \Imagick($tempSrcFilePath);
         $img->stripimage();
         //去除图片信息
         $img->setimagecompressionquality(95);
         //保证图片的压缩质量,同时大小可以接受
         $img->cropimage($width, $height, 0, 0);
         $img->writeimage($tempDestFilePath);
         $cloudStorage->moveFileToStorage($dataPathRoot, $imageThumbFileRelativeName, $tempDestFilePath);
         //主动释放资源,防止程序出错
         $img->destroy();
         unset($img);
     } else {
         // F3 框架的 Image 类限制只能操作 UI 路径中的文件,所以我们这里需要设置 UI 路径
         global $f3;
         $f3->set('UI', dirname($tempSrcFilePath));
         $img = new \Image('/' . basename($tempSrcFilePath));
         $img->resize($width, $height, true);
         $img->dump('jpeg', $tempDestFilePath);
         $cloudStorage->moveFileToStorage($dataPathRoot, $imageThumbFileRelativeName, $tempDestFilePath);
         //主动释放资源,防止程序出错
         $img->__destruct();
         unset($img);
     }
     // 删除临时文件
     @unlink($tempSrcFilePath);
     @unlink($tempDestFilePath);
 }
Example #2
0
 /**
  * 上传文件的主处理方法
  * @param $base64
  *
  * @return mixed
  */
 private function upFile($base64)
 {
     //处理base64上传
     if ("base64" == $base64) {
         $content = $_POST[$this->fileField];
         $this->base64ToImage($content);
         return;
     }
     //处理普通上传
     $file = $this->file = $_FILES[$this->fileField];
     if (!$file) {
         $this->stateInfo = $this->getStateInfo('POST');
         return;
     }
     if ($this->file['error']) {
         $this->stateInfo = $this->getStateInfo($file['error']);
         return;
     }
     if (!is_uploaded_file($file['tmp_name'])) {
         $this->stateInfo = $this->getStateInfo("UNKNOWN");
         return;
     }
     $this->oriName = $file['name'];
     $this->fileSize = $file['size'];
     $this->fileType = $this->getFileExt();
     if (!$this->checkSize()) {
         $this->stateInfo = $this->getStateInfo("SIZE");
         return;
     }
     if (!$this->checkType()) {
         $this->stateInfo = $this->getStateInfo("TYPE");
         return;
     }
     // 图片必须是 RGB 格式,不允许上传 CMYK 格式图片
     if (in_array($this->fileType, $this->imageType) && !Image::isImageRGB($file["tmp_name"])) {
         $this->stateInfo = $this->getStateInfo("RGB");
         return;
     }
     $this->fullName = $this->config["savePath"] . '/' . $this->getFolder() . '/' . $this->getName();
     $relativePath = $this->getRelativePath();
     if ($this->stateInfo == $this->stateMap[0]) {
         if (in_array($this->fileType, $this->imageType)) {
             // 如果是图片我们需要取得图片的 宽度 和 高度
             list($this->imageWidth, $this->imageHeight) = getimagesize($file["tmp_name"]);
         }
         if (!$this->cloudStorage->uploadFile(@$this->config['pathFix'], $relativePath, $file["tmp_name"])) {
             $this->stateInfo = $this->getStateInfo("MOVE");
         } else {
             //上传成功,设置错误码
             $this->errorCode = 0;
         }
     }
 }