-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatchimage.php
215 lines (184 loc) · 10 KB
/
catchimage.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
// The extension is named »CatchImage« because these images should catch the user and bring them to the post.
// Datenstrom Yellow shows the title of the post and a small excerpt of the post on the start page of the blog »start-blog.html«.
// If you want to create a portfolio page, you often need an image on the start page of the blog »start-blog.html«.
// This can be implemented with »CatchImage«. In addition, the full range of functions of »ImageFilter« can be used.
// ImageFilter extension is required.
class YellowCatchImage
{
const VERSION = '0.9.4';
public $yellow; // access to API
// Handle initialisation
public function onLoad($yellow)
{
$this->yellow = $yellow;
$this->yellow->system->setDefault('catchImageDefaultAltText', 'Lesen Sie den Beitrag: ');
}
// Start with main method
public function getCatchImage($filenametype, $baseUrl, $catchImageAltText, $catchImageFilter, $catchImageTitle)
{
$output = null;
$srcNew = null;
$srcNewInside = null;
$srcOriginal = $this->yellow->lookup->findMediaDirectory('coreImageLocation') . $filenametype;
// Check if »webp« should use
$useWebp = $this->yellow->system->get('imageFilterUseWebp');
// Get image width and height
list($width, $height, $type, $widthAndHeight) = getimagesize($srcOriginal);
// Split filename into name and type
$splitFilenameType = explode('.', $filenametype);
$originalFilename = $splitFilenameType[0];
$originalType = $splitFilenameType[1];
$isTypeAllowed = $this->yellow->extension->get('imagefilter')->checkIfTypeIsAllowed($originalType);
// Check if filter is given in posts header
// Get it without »imfi-« and eliminate wrong choosed »imfi-webp«
if (!empty($catchImageFilter)) {
$catchImageFilter = $this->getFilter($catchImageFilter);
if ($catchImageFilter === 'webp') {
$catchImageFilter = '';
}
}
// Get »$defaultFilter« and set it to »$choosedFilter«
// Eliminate wrong choosed »imfi-webp« in »system.ini«
$defaultFilter = $this->getFilter($this->yellow->system->get('imageFilterDefaultImfi'));
if ($defaultFilter === 'webp') {
$defaultFilter = 'original';
}
$choosedFilter = $defaultFilter;
// Check if »$defaultFilter« and »$catchImageFilter« have insert »original«
$compareDefaultCatchImageOriginal = $this->checkIfTwoOriginal($defaultFilter, $catchImageFilter);
// Check if filter is available in extension »ImageFilter« or »ImageFilterCollection«
$isFilterCatchAvailableExternal = $this->checkIfFilterIsAvailableExternal($catchImageFilter);
$isFilterCatchAvailableInternal = $this->checkIfFilterIsAvailableInternal($catchImageFilter);
$isFilterDefaultAvailableExternal = $this->checkIfFilterIsAvailableExternal($defaultFilter);
$isFilterDefaultAvailableInternal = $this->checkIfFilterIsAvailableInternal($defaultFilter);
$isFilterDefaultAvailable = $this->checkIfFilterDefaultIsAvailable($defaultFilter);
$isFilterAvailable = $this->checkIfFilterIsAvailable($isFilterCatchAvailableExternal, $isFilterCatchAvailableInternal, $isFilterDefaultAvailable);
if ($isFilterAvailable && !empty($catchImageFilter)) {
$choosedFilter = $catchImageFilter;
}
// If »defaultFilter« is not available
if ($isFilterDefaultAvailable === false) {
$choosedFilter = 'original';
$isFilterAvailable = true;
}
// If filter is available or »$catchImageFilter == 'original'« set it as choosed.
// If not -> »choosedFilter« stay »$defaultFilter«
if ($isFilterAvailable && !empty($catchImageFilter)) {
$choosedFilter = $catchImageFilter;
}
// Check if »webp« should use
if ($choosedFilter == 'original' && $useWebp == true && $isTypeAllowed === true ) {
$choosedFilter = 'webp';
$isFilterAvailable = true;
}
// Generate new Filename
$newFilename = '/' . $originalFilename . '-' . $choosedFilter;
// Use original image without »webp«
if ((($choosedFilter == 'original' || $choosedFilter == 'webp') && $useWebp == false) || $isTypeAllowed === false || $compareDefaultCatchImageOriginal === true ) {
$choosedFilter = '';
$newFilename = $originalFilename;
}
// Generate new src to image. Inside to convert image
// If filter is available and »webp« should use
if ($useWebp == true && $isFilterAvailable) {
$srcNewInside = $this->yellow->lookup->findMediaDirectory('coreImageLocation') . $choosedFilter . $newFilename . '.webp';
$srcNew = $baseUrl . $this->yellow->system->getHTML('CoreImageLocation') . $choosedFilter . $newFilename . '.webp';
}
// If filter is available, use without »webp« or type is not allowed (svg)
if (($useWebp != true && ($isFilterAvailable || empty($choosedFilter))) || $isTypeAllowed == false || $compareDefaultCatchImageOriginal === true ){
$srcNewInside = $this->yellow->lookup->findMediaDirectory('coreImageLocation') . $choosedFilter . $newFilename . '.' . $originalType;
$srcNew = $baseUrl . $this->yellow->system->getHTML('CoreImageLocation') . $choosedFilter . $newFilename . '.' . $originalType;
}
// gernerate/convert new directory and image if nessesary
if (($isFilterAvailable || empty($choosedFilter)) || $choosedFilter == 'webp') {
$pathNewInside = $this->yellow->lookup->findMediaDirectory('coreImageLocation') . $choosedFilter . '/';
if (!is_dir($pathNewInside)) {
mkdir($pathNewInside);
}
if ($isFilterCatchAvailableInternal || $isFilterDefaultAvailableInternal || $choosedFilter == 'webp') {
$this->yellow->extension->get('imagefilter')->generateNewImageInternal($choosedFilter, $srcOriginal, $srcNewInside, $originalType, $isTypeAllowed);
}
if ($isFilterCatchAvailableExternal || $isFilterDefaultAvailableExternal) {
$this->yellow->extension->get('imagefilter')->generateNewImageExternal($choosedFilter, $srcOriginal, $srcNewInside, $originalType, $isTypeAllowed);
}
}
// Generate Output
$output = '<img src="';
$output .= htmlspecialchars($srcNew);
if ($originalType === 'svg') {
$output .= '"' . ' ' . 'alt="';
} else{
$output .= '"' . ' ' . $widthAndHeight . ' ' . 'alt="';
}
if (empty($catchImageAltText)) {
$output .= $this->yellow->system->get('catchImageDefaultAltText') . ' ' . htmlspecialchars($catchImageTitle);
} else {
$output .= htmlspecialchars($catchImageAltText);
}
$output .= "\">\n";
return $output;
}
// Compare »defaultFilter« with »$catchImageFilter«
public function checkIfTwoOriginal($defaultFilter, $catchImageFilter)
{
if ($defaultFilter === 'original' && $catchImageFilter === 'original'){
$compareDefaultCatchImageOriginal = true;
}else{
$compareDefaultCatchImageOriginal = false;
}
return $compareDefaultCatchImageOriginal;
}
// Check if filter is available
public function checkIfFilterIsAvailable($isFilterCatchAvailableExternal, $isFilterCatchAvailableInternal, $isFilterDefaultAvailable)
{
if ($isFilterCatchAvailableExternal || $isFilterCatchAvailableInternal || $isFilterDefaultAvailable){
$isFilterAvailable = true;
} else {
$isFilterAvailable = false;
}
return $isFilterAvailable;
}
// Check if filter is available in »imagefilter.php«
public function checkIfFilterDefaultIsAvailable($toCheckFilter)
{
$isFilterCatchAvailableExternal = $this->checkIfFilterIsAvailableExternal($toCheckFilter);
$isFilterCatchAvailableInternal = $this->checkIfFilterIsAvailableInternal($toCheckFilter);
if (($isFilterCatchAvailableExternal || $isFilterCatchAvailableInternal)) {
$isFilterAvailable = true;
} else {
$isFilterAvailable = false;
}
return $isFilterAvailable;
}
// Check if the extension »imageFilter« exist and if filter is available in »imagefilter.php«
public function checkIfFilterIsAvailableInternal($toCheckFilter)
{
if ($this->yellow->extension->isExisting('imagefilter')) {
$isFilterCatchAvailableInternal = method_exists($this->yellow->extension->get('imagefilter'), $toCheckFilter);
} else {
$isFilterCatchAvailableInternal = false;
}
return $isFilterCatchAvailableInternal;
}
// Check if the extension »imageFillterCollection« exist and if filter is available in »imagefiltercollection.php«
public function checkIfFilterIsAvailableExternal($toCheckFilter)
{
if ($this->yellow->extension->isExisting('imagefiltercollection')) {
$isFilterCatchAvailableExternal = method_exists($this->yellow->extension->get('imagefiltercollection'), $toCheckFilter);
} else {
$isFilterCatchAvailableExternal = false;
}
return $isFilterCatchAvailableExternal;
}
// Check if Filter contain »imfi« and return filter without »imfi«
public function getFilter($toCheckImageFilter)
{
if ((strpos($toCheckImageFilter, 'imfi') !== false)) {
$toCheckImageFilter = strtolower($toCheckImageFilter);
$toCheckImageFilter = explode('-', $toCheckImageFilter);
$toCheckImageFilter = preg_replace('/\s+/', '', $toCheckImageFilter[1]);
}
return $toCheckImageFilter;
}
}