Newscoop\Image\RenditionService::getArticleRenditionImage PHP Method

getArticleRenditionImage() public method

Get article image rendition
public getArticleRenditionImage ( integer $articleNumber, string $renditionName, integer $width = null, integer $height = null ) : array
$articleNumber integer
$renditionName string
$width integer
$height integer
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));
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Get document representation for article
  *
  * @param Newscoop\Entity\Article $article
  * @return array
  */
 public function getDocument(DocumentInterface $article)
 {
     $image = null;
     $renditions = $this->renditionService->getRenditions();
     if (is_array($renditions) && count($renditions) > 0) {
         $image = $this->renditionService->getArticleRenditionImage($article->getNumber(), key($renditions));
     }
     $webcode = $this->webcoder->getArticleWebcode($article);
     if (strpos($webcode, 0, 1) != '+') {
         $webcode = '+' . $webcode;
     }
     $doc = array('id' => $this->getDocumentId($article), 'number' => $article->getNumber(), 'type' => $article->getType(), 'webcode' => $webcode, 'title' => $article->getTitle(), 'updated' => gmdate(self::DATE_FORMAT, $article->getDate()->getTimestamp()), 'published' => gmdate(self::DATE_FORMAT, $article->getPublishDate()->getTimestamp()), 'image' => $image ? $image['src'] : null, 'link' => $this->linkService->getLink($article), 'language' => $article->getLanguageCode(), 'language_id' => $article->getLanguageId(), 'publication_number' => $article->getPublication() ? $article->getPublication()->getId() : null, 'issue_number' => $article->getIssue() ? $article->getIssue()->getNumber() : null, 'section_number' => $article->getSection() ? $article->getSection()->getNumber() : null, 'section_id' => $article->getSection() ? $article->getSection()->getNumber() : null, 'section' => $this->linkService->getSectionShortName($article), 'section_name' => $article->getSection() ? $article->getSection()->getName() : null, 'authors' => array_map(function ($author) {
         return $author->getFullName();
     }, is_array($article->getArticleAuthors()) ? $article->getArticleAuthors() : array()), 'keywords' => explode(',', $article->getKeywords()), 'topics' => array_values($article->getTopicNames()), 'switches' => $this->getArticleSwitches($article));
     $this->addDataFields($doc, $article);
     return array_filter($doc);
 }