/** * {@inheritDoc} */ public function generateName(MediaInterface $media, VariantInterface $variant, Filesystem $filesystem) { $name = trim($media->getName()); if ($name == '') { throw new InvalidArgumentException('The given media has no name'); } $suffix = uniqid('-') . '_' . $variant->getName(); if ($this->maxLength && function_exists('mb_strlen')) { $nameMaxLength = $this->maxLength - mb_strlen($suffix); if (mb_strlen($name . $suffix) > $this->maxLength) { $name = mb_substr($name, 0, $nameMaxLength); } } $name = self::urlize($name); return $name . $suffix; }
/** * {@inheritDoc} */ public function process(MediaInterface $media, VariantInterface $variant, \SplFileInfo $source = NULL) { $options = $variant->getOptions(); $result = $source; list($originalWidth, $originalHeight) = getimagesize($source->getPathName()); $width = $originalWidth; $height = $originalHeight; if (is_array($options) && !empty($options)) { if ($this->imagine == NULL) { throw new ProviderProcessException(sprintf('Cannot process image "%s": Imagine library not installed or misconfigured', $media), $this, $media, $variant); } $options = $this->processOptions($options, $variant->getName(), $media->getContext()); $destFile = sprintf('%s%s-temp-%s.%s', $this->tempDir, date('Y-m-d-h-i-s'), $source->getBasename('.' . $source->getExtension()), $options['format']); /** * @var \Imagine\Image\ImageInterface $image */ $image = $this->imagine->open($source); if ($options['enlarge'] === TRUE || $originalWidth >= $options['width'] && $originalHeight >= $options['height']) { $width = $options['width']; $height = $options['height']; if ($options['resize'] == 'proportional') { //calculate missing dimension if ($width === NULL) { $width = round($originalWidth * $height / $originalHeight); } elseif ($height === NULL) { $height = round($width * $originalHeight / $originalWidth); } } $box = new \Imagine\Image\Box($width, $height); if ($options['resize'] == 'proportional' || $options['resize'] == 'stretch') { $image->resize($box); } elseif ($options['resize'] == 'crop') { $image = $image->thumbnail($box, \Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND); } } $image->save($destFile, array('quality' => $options['quality'])); $this->addTempFile($destFile); $result = new \SplFileInfo($destFile); } //set variant metadata $variant->setMetaValue('size', $result->getSize()); $variant->setMetaValue('width', $width); $variant->setMetaValue('height', $height); return $result; }
/** * {@inheritDoc} */ public function addVariant(VariantInterface $variant) { $this->variants[$variant->getName()] = $variant->toArray(); }