/
Image.php
82 lines (74 loc) · 2.4 KB
/
Image.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
namespace Concrete\Core\Html;
use Concrete\Core\Entity\File\File;
use PageTheme;
class Image
{
protected $usePictureTag = false;
protected $tag;
protected $theme;
protected function loadPictureSettingsFromTheme()
{
$c = \Page::getCurrentPage();
if (is_object($c)) {
$th = PageTheme::getByHandle($c->getPageController()->getTheme());
if (is_object($th)) {
$this->theme = $th;
$this->usePictureTag = count($th->getThemeResponsiveImageMap()) > 0;
}
}
}
/**
* @param \File $f
* @param null $usePictureTag
*/
public function __construct(File $f = null, $usePictureTag = null)
{
if (!is_object($f)) {
return false;
}
if (isset($usePictureTag)) {
$this->usePictureTag = $usePictureTag;
} else {
$this->loadPictureSettingsFromTheme();
}
if ($this->usePictureTag) {
if (!isset($this->theme)) {
$c = \Page::getCurrentPage();
$this->theme = $c->getCollectionThemeObject();
}
$sources = array();
$fallbackSrc = $f->getRelativePath();
if (!$fallbackSrc) {
$fallbackSrc = $f->getURL();
}
foreach ($this->theme->getThemeResponsiveImageMap() as $thumbnail => $width) {
$type = \Concrete\Core\File\Image\Thumbnail\Type\Type::getByHandle($thumbnail);
if ($type != null) {
$src = $f->getThumbnailURL($type->getBaseVersion());
$sources[] = array('src' => $src, 'width' => $width);
if ($width == 0) {
$fallbackSrc = $src;
}
}
}
$this->tag = \Concrete\Core\Html\Object\Picture::create($sources, $fallbackSrc);
} else {
// Return a simple image tag.
$path = $f->getRelativePath();
if (!$path) {
$path = $f->getURL();
}
$this->tag = \HtmlObject\Image::create($path);
$this->tag->width((string) $f->getAttribute('width'));
$this->tag->height((string) $f->getAttribute('height'));
}
}
/**
* @return \HTMLObject\Element\Tag
*/
public function getTag()
{
return $this->tag;
}
}