SPR-787 use same logo resized

This commit is contained in:
Andreas Gohr 2017-02-20 16:21:45 +01:00
commit 06cdf1484d
3 changed files with 107 additions and 20 deletions

View file

@ -30,7 +30,6 @@ class Template {
return $instance;
}
/**
* Template constructor.
*/
@ -80,4 +79,43 @@ class Template {
return $tabs;
}
/**
* Creates an image tag and includes the first found image correctly resized
*
* @param string $tag
* @param array $attributes
* @param int $w
* @param int $h
* @return string
*/
public static function getResizedImgTag($tag, $attributes, $w, $h) {
$attr = '';
$medias = array();
// the attribute having an array defines where the image goes
foreach($attributes as $attribute => $data) {
if(is_array($data)) {
$medias = $data;
$attr = $attribute;
}
}
// if the image attribute could not be found return
if(!$attr || !$medias) return '';
// try all medias until an existing one is found
$media = '';
foreach($medias as $media) {
if(file_exists(mediaFN($media))) break;
$media = '';
}
if($media === '') return '';
// replace the array
$media = ml($media, array('w' => $w, 'h' => $h, 'crop' => 1), true, '&');
$attributes[$attr] = $media;
// return the full tag
return '<' . $tag . ' ' . buildAttributes($attributes) . ' />';
}
}