/** * Set article rendition * * @param int $articleNumber * @param Newscoop\Image\Rendition $rendition * @param Newscoop\Image\ImageInterface $image * @param string $imageSpecs * @return Newscoop\Image\ArticleRendition */ public function setArticleRendition($articleNumber, Rendition $rendition, ImageInterface $image, $imageSpecs = null) { if ($image->getWidth() < $rendition->getWidth() || $image->getHeight() < $rendition->getHeight()) { throw new \InvalidArgumentException("Image too small."); } $old = $this->getArticleRendition($articleNumber, $rendition); if ($old !== null) { $this->orm->remove($old); $this->orm->flush(); } $articleRendition = new ArticleRendition($articleNumber, $rendition, $image, $imageSpecs); $this->orm->persist($articleRendition); $this->orm->flush($articleRendition); return $articleRendition; }
/** * Get rendition preview * * @param Newscoop\Image\Rendition $rendition * @param int $width * @param int $height * @param Newscoop\Image\ArticleRendition $articleImageRendition * @return string */ public function rendition(Rendition $rendition, $width, $height, ArticleRendition $articleRendition = null) { if ($articleRendition !== null) { $preview = $articleRendition->getRendition()->getPreview($width, $height); $preview->setCoords($articleRendition->getImageSpecs()); $thumbnail = $rendition->fits($articleRendition->getImage()) ? $this->imageService->getThumbnail($preview, $articleRendition->getImage()) : null; } else { $preview = $rendition->getPreview($width, $height); $thumbnail = null; } $this->view->isDefault = $thumbnail === null || $articleRendition->isDefault(); $this->view->preview = $preview; $this->view->rendition = $rendition; $this->view->thumbnail = $thumbnail; return $this->view->render('image/rendition.phtml'); }
/** * Get preview * * @param int $renditionWidth * @param int $renditionHeight * @param int $previewWidth * @param int $previewHeight * @return Newscoop\Image\RenditionPreview */ private function getPreview($renditionWidth, $renditionHeight, $previewWidth, $previewHeight) { $rendition = new Rendition($renditionWidth, $renditionHeight); return $rendition->getPreview($previewWidth, $previewHeight); }
/** * Get thumbnail for given image and rendition * * @param Newscoop\Image\Rendition $rendition * @param Newscoop\Image\ImageInterface $image * @return Newscoop\Image\Thumbnail */ public function getThumbnail(Rendition $rendition, ImageInterface $image) { return $rendition->getThumbnail($image, $this); }
/** * Generate image for given src * * @param string $src * * @return void */ public function generateImageUrl($src) { $matches = array(); if (!preg_match('#^([0-9]+)x([0-9]+)/([_a-z0-9]+)/([-_.:~%|a-zA-Z0-9]+)$#', $src, $matches)) { return; } list(, $width, $height, $specs, $imagePath) = $matches; $destFolder = rtrim($this->config['cache_path'], '/') . '/' . dirname(ltrim($src, './')); if (!realpath($destFolder)) { mkdir($destFolder, 0755, true); } if (!is_dir($destFolder)) { throw new \RuntimeException("Can't create folder '{$destFolder}'."); } $rendition = new Rendition($width, $height, $specs); $image = $rendition->generateImage($this->decodePath($imagePath)); $image->save($destFolder . '/' . $imagePath); return $image; }
/** * Get article image rendition * * @param int $articleNumber * @param string $renditionName * @param int $width * @param int $height * @return array */ public function getArticleRenditionImage($articleNumber, $renditionName, $width = null, $height = null) { $renditions = $this->getRenditions(); if (!array_key_exists($renditionName, $renditions)) { return false; } $articleRenditions = $this->getArticleRenditions($articleNumber); $rendition = $articleRenditions[$renditions[$renditionName]]; if ($rendition === null) { return false; } try { if ($width !== null && $height !== null) { $preview = $rendition->getRendition()->getPreview($width, $height); $thumbnail = $preview->getThumbnail($rendition->getImage(), $this->imageService); } else { $thumbnail = $rendition->getRendition()->getThumbnail($rendition->getImage(), $this->imageService); } } catch (\Exception $e) { return null; } $originalRendition = new Rendition($rendition->getImage()->getWidth(), $rendition->getImage()->getHeight()); return array('id' => $rendition->getImage()->getId(), 'src' => $thumbnail->src, 'width' => $thumbnail->width, 'height' => $thumbnail->height, 'caption' => $rendition->getImage()->getCaption(), 'photographer' => $rendition->getImage()->getPhotographer(), 'photographer_url' => $rendition->getImage()->getPhotographerUrl(), 'original' => (object) array('width' => $rendition->getImage()->getWidth(), 'height' => $rendition->getImage()->getHeight(), 'src' => $originalRendition->getThumbnail($rendition->getImage(), $this->imageService)->src)); }