/** * @param Temp|UploadedFile|File $file * @param array $options * @return array * @throws \Exception */ public function manipulateImage($file, array $options) { // Detect file type if ($file instanceof Temp) { $sanitizedFilename = $this->sanitizeFilename($file->filename); } elseif ($file instanceof UploadedFile) { $sanitizedFilename = $this->sanitizeFilename($file->getClientOriginalName()); } elseif ($file instanceof File) { $actualFilename = str_replace('_source.', '.', $file->getFilename()); $sanitizedFilename = $this->sanitizeFilename($actualFilename); $sourceFile = clone $file; } else { throw new \Exception('Unexpected file of class ' . get_class($file)); } $images = []; $pathParts = pathinfo($sanitizedFilename); // Move uploaded file and rename it as source file if this is needed. // We need to generate unique name if the name is already in use. if (!isset($sourceFile)) { $filename = $pathParts['filename'] . '_source.' . $pathParts['extension']; $sourceFile = $file->move($this->getThumbnailPath(), $filename); } $images['original']['source'] = $sourceFile; // If we have retina factor if ($this->getRetinaFactor()) { // Generate retina image by just copying the source. $retinaFilename = $this->generateRetinaName($sanitizedFilename); FileFacade::copy($sourceFile->getRealPath(), $this->getThumbnailPath() . $retinaFilename); $images['original']['retina'] = Image::make($this->getThumbnailPath() . $retinaFilename); // The original image is scaled down version of the source. $originalImage = Image::make($sourceFile->getRealPath()); $width = round($originalImage->getWidth() / $this->getRetinaFactor()); $height = round($originalImage->getHeight() / $this->getRetinaFactor()); $originalImage->resize($width, $height); $images['original']['original_file'] = $originalImage->save($this->getThumbnailPath() . $sanitizedFilename); // Generate thumbs foreach ($options['thumbnails'] as $thumbnailName => $thumbnailOptions) { // Create retina thumb $images['thumbnails'][$thumbnailName]['retina'] = $this->createThumbnail($sourceFile->getRealPath(), $thumbnailName, $this->generateRetinaName($sanitizedFilename), $thumbnailOptions['width'] * $this->getRetinaFactor(), $thumbnailOptions['height'] * $this->getRetinaFactor(), $thumbnailOptions['type'], array_get($thumbnailOptions, 'color')); // Create original thumb $images['thumbnails'][$thumbnailName]['original'] = $this->createThumbnail($sourceFile->getRealPath(), $thumbnailName, $sanitizedFilename, $thumbnailOptions['width'], $thumbnailOptions['height'], $thumbnailOptions['type'], array_get($thumbnailOptions, 'color')); } } else { // Copy source file. $filename = $this->sanitizeFilename($file->getFilename()); FileFacade::copy($sourceFile->getRealPath(), $this->getThumbnailPath() . $filename); $images['original']['original_file'] = Image::make($this->getThumbnailPath() . $filename); // Generate thumbs foreach ($options['thumbnails'] as $thumbnailName => $thumbnailOptions) { // Create original thumb $images['thumbnails'][$thumbnailName]['original'] = $this->createThumbnail($sourceFile->getRealPath(), $thumbnailName, $sanitizedFilename, $thumbnailOptions['width'], $thumbnailOptions['height'], $thumbnailOptions['type']); } } return $images; }