/** * Validates file size * @param Entity\File $file */ public function validateFile(Entity\File $file, $sourceFilePath = null) { if (!$file instanceof Entity\Image) { return; } if (is_null($sourceFilePath)) { return; } $info = new ImageInfo($sourceFilePath); if ($info->hasError()) { throw new Exception\RuntimeException($info->getError()); } // Assumes memory_limit is in MB // "-1" means no limit $memoryLimit = (int) ini_get('memory_limit'); if ($memoryLimit < 0) { return; } $memoryLeft = (int) $memoryLimit * 1024 * 1024 - memory_get_usage(); // Should use real usage or not? // Read data from image info, default bitsPerChannel to 8, channel count to 4 $memoryRequired = $info->getWidth() * $info->getHeight() * ($info->getBits() ?: 8) * ($info->getChannels() ?: 4) * 2; if ($memoryRequired > $memoryLeft) { $message = "Not enough memory [{$memoryLeft} bytes] to resize the image, required amount [{$memoryRequired} bytes]"; throw new Exception\InsufficientSystemResources(self::EXCEPTION_MESSAGE_KEY, $message); } }
/** * @param string $fileName * @return ImageInfo * @throws ImageProcessorException */ protected function getImageInfo($fileName) { $info = new ImageInfo($fileName); if ($info->hasError()) { throw new ImageProcessorException('File ' . $fileName . ' not found or is not readable. ' . $info->getError()); } return $info; }
/** * Get full image info (dimesions, mime-type etc) * * @param string $filename * @return ImageInfo */ public function getImageInfo($filename) { if ($filename instanceof Image) { $filename = $this->adapter->getFileStorage()->getImagePath($filename); } $info = new ImageInfo($filename); if ($info->hasError()) { throw new ImageProcessorException('File ' . $filename . ' not found or is not readable. ' . $info->getError()); } return $info; }
/** * Is current image format supported by image processor * @param string $filename Path to file */ public function isSupportedImageFormat($filename) { $info = new ImageInfo($filename); if ($info->hasError()) { $this->container->getLogger()->error($info->getError()); return false; } if (!$this->isMimeTypeImage($info->getMime())) { return false; } return ImageProcessor\ImageProcessor::isSupportedImageType($info->getType()); }