Exemplo n.º 1
0
 /**
  * Upload Image from POST
  * 
  * @param array $file - Part of $_FILES like $_FILES['photo']
  * @param string $fileName - Put image with this filename
  * @param Config $imageUploaderConfig
  * @throws RuntimeException
  * @throws InvalidArgumentException
  * @throws ImageException
  * @throws ImageUploaderException
  * @return Image
  */
 public static function upload($file, $fileName = null, Config $imageUploaderConfig = null)
 {
     $imageUploaderConfig = ConfigManager::mergeConfigs($imageUploaderConfig, ConfigManager::getConfig("Image", "ImageUploader")->AuxConfig);
     $uploadDir = $imageUploaderConfig->uploadDir;
     if (empty($uploadDir)) {
         throw new RuntimeException("Unable to get any appropriate uploadDir!");
     }
     if (!file_exists($uploadDir)) {
         throw new InvalidArgumentException("Upload directory {$uploadDir} doesn't exists.");
     }
     ensurePathLastSlash($uploadDir);
     if (!in_array($file["type"], $imageUploaderConfig->acceptedMimeTypes->toArray())) {
         throw new ImageException("Unsupported file uploaded!");
     }
     // Check if we have enough memory to open this file as image
     $info = getimagesize($file['tmp_name']);
     if ($info != false && !Image::checkMemAvailbleForResize($info[0], $info[1])) {
         throw new ImageUploaderException("Not enough memory to open image", static::EXCEPTION_IMAGE_IS_BIG);
     }
     // Check if we are able to create image resource from this file.
     $image = new Image($file['tmp_name']);
     $format = null;
     if (isset($imageUploaderConfig->saveFormat) and $imageUploaderConfig->saveFormat != null) {
         $format = $imageUploaderConfig->saveFormat;
     } else {
         $format = $image->getType();
     }
     if ($fileName === null) {
         $fileName = static::findNewFileName($uploadDir, $format);
     }
     $savePath = $uploadDir . $fileName;
     if (isset($imageUploaderConfig->minimumSize)) {
         $checkResult = $image->isSizeMeetRequirements($imageUploaderConfig->minimumSize->largeSideMinSize, $imageUploaderConfig->minimumSize->smallSideMinSize);
         if (!$checkResult) {
             throw new ImageUploaderException("Given image is smaller than specified minimum size.", static::EXCEPTION_IMAGE_IS_SMALL);
         }
     }
     switch ($format) {
         case Image::IMAGE_TYPE_JPEG:
             $image->writeJpeg($savePath);
             break;
         case Image::IMAGE_TYPE_PNG:
             $image->writePng($savePath);
             break;
         case Image::IMAGE_TYPE_GIF:
             $image->writeGif($savePath);
             break;
     }
     return $image;
 }
Exemplo n.º 2
0
 public static function upload($file, $fileName = null, $uploadDir = null)
 {
     if ($uploadDir === null) {
         $fileUploaderConfig = ConfigManager::getConfig("FileUploader", "FileUploader")->AuxConfig;
         if (isset($fileUploaderConfig->uploadDir)) {
             $uploadDir = $fileUploaderConfig->uploadDir;
         }
     }
     if (empty($uploadDir)) {
         throw new RuntimeException("Unable to get any appropriate uploadDir!");
     }
     if (!file_exists($uploadDir)) {
         throw new InvalidArgumentException("Upload directory {$uploadDir} doesn't exists.");
     }
     ensurePathLastSlash($uploadDir);
     if ($file["error"] == UPLOAD_ERR_OK) {
         $tmpName = $file["tmp_name"];
         if ($fileName === null) {
             $fileName = $file["name"];
         }
         return move_uploaded_file($tmpName, $uploadDir . basename($fileName));
     }
     return $fileName;
 }
Exemplo n.º 3
0
 public function __construct(Config $config)
 {
     $this->config = $config;
     ensurePathLastSlash($this->config->cacheDir);
 }