/** * @param string $imagePath * @param string[]|ConfigInterface[] $versionsConfig * @param string $outputDir * @param string $imageUniqueName * * @return string[] */ public function createImageVersions($imagePath, $versionsConfig, $outputDir = null, $imageUniqueName = null) { if ($outputDir === null) { $outputDir = $this->outputDir; } if ($imageUniqueName === null) { $imageUniqueName = $this->getUniqueName($imagePath); } $versions = []; $imageExt = pathinfo($imagePath, PATHINFO_EXTENSION); foreach ($versionsConfig as $versionName => $versionConfig) { if (is_string($versionConfig)) { $config = $this->configParser->parse($versionConfig); } elseif ($versionConfig instanceof ConfigInterface) { $config = $versionConfig; } else { throw new InvalidConfigException('Config must be a string or implement happyproff\\Kartinki\\Interfaces\\ConfigInterface.'); } $versionFilename = $imageUniqueName . self::NAME_SEPARATOR . $versionName . '.' . $imageExt; $image = $this->processor->read(fopen($imagePath, 'r')); $version = $this->createImageVersion($image, $config); $version->save($outputDir . '/' . $versionFilename, ['jpeg_quality' => $config->getQuality()]); unset($version); $versions[$versionName] = $versionFilename; } return $versions; }
/** * Creates an image object * * @param array|\Imagine\Image\ImageInterface $source * @throws \InvalidArgumentException On unsupported source type * @return \Imagine\Image\ImageInterface */ public function create($source) { if ($source instanceof ImageInterface) { return $source; } if (isset($source['file'])) { return $this->imagine->open($source['file']); } if (isset($source['data'])) { return $this->imagine->load($source['data']); } if (isset($source['resource'])) { return $this->imagine->read($source['resource']); } if (isset($source['width']) && isset($source['height'])) { return $this->imagine->create(new Box($source['width'], $source['height'])); } throw new InvalidArgumentException(); }
/** * @param string $imagePath * @param string[]|PresetInterface[] $presets * @param string $outputDir * @param string $imageUniqueId * * @return Result */ public function createThumbnails($imagePath, $presets, $outputDir = null, $imageUniqueId = null) { if (!file_exists($imagePath)) { throw new FileNotFoundException("File '{$imagePath}' not exists."); } if (!is_readable($imagePath)) { throw new FileIsNotReadableException("File '{$imagePath}' is not readable."); } if (is_null($outputDir)) { $outputDir = $this->outputDir ? $this->outputDir : realpath(dirname($imagePath)); } if (!is_writable($outputDir)) { throw new DirectoryIsNotWritableException("Ouput directory '{$outputDir}' is not writable."); } if (is_null($imageUniqueId)) { $imageUniqueId = $this->getUniqueName($imagePath); } elseif (!is_string($imageUniqueId) && !(is_object($imageUniqueId) && method_exists($imageUniqueId, '__toString'))) { throw new InvalidArgumentException("{$imageUniqueId} must be a string."); } $thumbnails = []; $imageExtension = pathinfo($imagePath, PATHINFO_EXTENSION); foreach ($presets as $presetName => $preset) { if (is_string($preset)) { $parsedPreset = $this->presetParser->parse($preset); } elseif ($preset instanceof PresetInterface) { $parsedPreset = $preset; } else { throw new InvalidArgumentException('Preset must be a string or implements kartinki\\Kartinki\\Interfaces\\PresetInterface.'); } $thumbnailFilename = $imageUniqueId . self::NAME_SEPARATOR . $presetName . '.' . $imageExtension; $image = $this->processor->read(fopen($imagePath, 'r')); $thumbnail = $this->createImageThumbnail($image, $parsedPreset); $thumbnail->save($outputDir . '/' . $thumbnailFilename, ['jpeg_quality' => $parsedPreset->getQuality()]); unset($thumbnail); $thumbnails[$presetName] = $thumbnailFilename; } $result = new Result($imageUniqueId, $imageExtension, $thumbnails); return $result; }
/** * Get the size of a Flow Resource object that contains an image file. * * @param FlowResource $resource * @return array width and height as keys * @throws ImageFileException */ public function getImageSize(FlowResource $resource) { $cacheIdentifier = $resource->getCacheEntryIdentifier(); $imageSize = $this->imageSizeCache->get($cacheIdentifier); if ($imageSize !== false) { return $imageSize; } // TODO: Special handling for SVG should be refactored at a later point. if ($resource->getMediaType() === 'image/svg+xml') { $imageSize = ['width' => null, 'height' => null]; } else { try { $imagineImage = $this->imagineService->read($resource->getStream()); $sizeBox = $imagineImage->getSize(); $imageSize = array('width' => $sizeBox->getWidth(), 'height' => $sizeBox->getHeight()); } catch (\Exception $e) { throw new ImageFileException(sprintf('The given resource was not an image file your choosen driver can open. The original error was: %s', $e->getMessage()), 1336662898); } } $this->imageSizeCache->set($cacheIdentifier, $imageSize); return $imageSize; }