/
home
/
assocoweys
/
avexp
/
plugins
/
system
/
articlesanywhere
/
src
/
Output
/
Data
/
Upload File
HOME
<?php /** * @package Articles Anywhere * @version 8.5.2 * * @author Peter van Westen <info@regularlabs.com> * @link http://www.regularlabs.com * @copyright Copyright © 2018 Regular Labs All Rights Reserved * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL */ namespace RegularLabs\Plugin\System\ArticlesAnywhere\Output\Data; defined('_JEXEC') or die; use Joomla\CMS\Uri\Uri as JUri; use RegularLabs\Library\ArrayHelper as RL_Array; use RegularLabs\Library\File as RL_File; use RegularLabs\Library\HtmlTag as RL_HtmlTag; use RegularLabs\Library\Image as RL_Image; use RegularLabs\Library\RegEx as RL_RegEx; use RegularLabs\Plugin\System\ArticlesAnywhere\Params; class Images extends Data { public function get($key, $attributes) { $regex = '^image(?<separator>[-_])(?<type>intro|fulltext)([-_](?<data>[a-z][a-z0-9-_]*))?$'; RL_RegEx::match($regex, $key, $image_tag); if (empty($image_tag)) { return null; } $data = isset($image_tag['data']) ? $image_tag['data'] : ''; if ($image_tag['separator'] == '_' && empty($data)) { $data = 'url'; } $data = str_replace( [ 'thumbnail', ], [ 'thumb', ], $data ); $data = RL_RegEx::replace('-?(tag|img)$', '', $data); return $this->getByType($image_tag['type'], $data, $attributes); } protected function getByType($type, $data, $attributes) { switch ($type) { case 'intro': return $this->getIntroImage($data, $attributes); case 'fulltext': return $this->getFulltextImage($data, $attributes); default: return ''; } } public function getImageByUrl($url, &$attributes) { $image = ['url' => $url]; $this->prepareImageUrl($image['url'], $attributes); $this->setResizedImage($image, $attributes); return $image; } protected function prepareImageUrl(&$url, $attributes) { if (isset($attributes->suffix)) { $url = RL_RegEx::replace( '\.[a-z]*$', $attributes->suffix . '\0', $url ); unset($attributes->suffix); } } protected function getIntroImage($data, $attributes) { return $this->getArticleImageDataByType('intro', $data, $attributes); } protected function getFulltextImage($data, $attributes) { return $this->getArticleImageDataByType('fulltext', $data, $attributes); } protected function getArticleImageDataByType($type = 'intro', $data, $attributes) { $type = $type == 'fulltext' ? 'fulltext' : 'intro'; switch ($data) { case 'url': return $this->getArticleImageUrlByType($type, $attributes); case 'alt': return $this->item->getFromGroup('images', 'image_' . $type . '_alt'); case 'caption': return $this->item->getFromGroup('images', 'image_' . $type . '_caption'); case 'class': return 'img-' . $type . '-' . $this->item->getFromGroup('images', 'float_' . $type); case '': return $this->getArticleImageTagByType($type, $attributes); default: return $this->getArticleImageAttributeByType($data, $type, $attributes); } } protected function getArticleImageUrlByType($type = 'intro', &$attributes) { $url = $this->item->getFromGroup('images', 'image_' . $type); if (empty($url)) { return ''; } $image = ['url' => $url]; $this->prepareImageUrl($image['url'], $attributes); return $image['url']; } protected function getArticleImageTagByType($type = 'intro', $attributes) { $url = $this->getArticleImageUrlByType($type, $attributes); if (empty($url)) { return ''; } $class = 'img-intro-' . $this->item->getFromGroup('images', 'float_' . $type); $alt = $this->item->getFromGroup('images', 'image_' . $type . '_alt'); $caption = $this->item->getFromGroup('images', 'image_' . $type . '_caption'); $title = $caption ?: $alt; return $this->getImageHtml( $url, $alt, $title, $class, $attributes, ! empty($caption) ); } protected function getArticleImageAttributeByType($key, $type = 'intro', $attributes) { $new_tag = $this->getArticleImageTagByType($type, $attributes); return $this->getImageAttribute($key, $new_tag); } protected function getImageAttribute($key, $html) { $tag_attributes = RL_HtmlTag::getAttributes($html); if (isset($tag_attributes[$key])) { return $tag_attributes[$key]; } if ( ! in_array($key, ['width', 'height'])) { return ''; } $url = $tag_attributes['src']; if (RL_File::isExternal($url)) { return ''; } $dimensions = RL_Image::getDimensions($url); return isset($dimensions->{$key}) ? $dimensions->{$key} : ''; } public static function getImageHtml( $url, $alt = '', $title = '', $class = '', $attributes = null, $caption = false ) { $attributes = (object) $attributes; $img_class = $caption ? 'caption' : ''; $title = $title ? ' title="' . htmlspecialchars($title) . '"' : ''; $img_class = trim($img_class . ' ' . htmlspecialchars($class)); if ( ! isset($attributes->{'outer-class'})) { $attributes->{'outer-class'} = $class; } $tag = '<img src="' . htmlspecialchars($url) . '" alt="' . htmlspecialchars($alt) . '"' . $title . ' class="' . $img_class . '">'; $image = self::getImageHtmlWithAttributes($tag, $attributes); return $image; } static protected function getImageHtmlWithAttributes($tag, $attributes) { if (empty($attributes)) { return $tag; } $attributes = (object) $attributes; $outer_class = isset($attributes->{'outer-class'}) ? $attributes->{'outer-class'} : ''; unset($attributes->{'outer-class'}); $tag_attributes = RL_HtmlTag::getAttributes($tag); $tag_attributes = array_merge($tag_attributes, (array) $attributes); $image = '<img ' . RL_HtmlTag::flattenAttributes($tag_attributes) . ' />'; if ( ! $outer_class) { return $image; } return '<div class="' . htmlspecialchars($outer_class) . '">' . $image . '</div>'; } }