PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 7.0.19
Admin Columns v7.0.19
7.0.19 2.3.5 2.4 2.4.1 2.4.10 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.6.1 2.5.6.2 2.5.6.3 2.5.6.4 3.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.7 3.1 3.1.1 3.1.10 3.1.2 3.1.3 3.1.5 3.2.3 3.2.7 3.3.1 3.4.1 3.4.6 3.4.8 4.0.1 4.0.3 4.1.6 4.2.2 4.2.5 4.3 4.3.2 4.4.1 4.4.4 4.4.5 4.5.5 4.6.1 4.7.18 4.7.19 4.7.20 4.7.7 7.0.13 7.0.14 7.0.16 trunk 1.0 1.1 1.1.3 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.5.1 1.4.6 1.4.6.1 1.4.6.2 1.4.6.3 1.4.6.4 1.4.7 1.4.8 1.4.9 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.1.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.5.1 2.2.6 2.2.6.1 2.2.6.2 2.2.6.3 2.2.6.4 2.2.7 2.2.8 2.2.8.1 2.2.9 2.3.1 2.3.2 2.3.3
codepress-admin-columns / classes / Helper / Image.php
codepress-admin-columns / classes / Helper Last commit date
Select 1 month ago Arrays.php 1 month ago Creatable.php 1 month ago Date.php 1 month ago File.php 1 month ago Html.php 1 month ago Icon.php 1 month ago Image.php 1 month ago Mbstring.php 1 month ago Media.php 1 month ago Menu.php 1 month ago Network.php 1 month ago Post.php 1 month ago Strings.php 1 month ago Taxonomy.php 1 month ago Translations.php 1 month ago User.php 1 month ago UserRoles.php 1 month ago
Image.php
342 lines
1 <?php
2
3 namespace AC\Helper;
4
5 use DOMDocument;
6 use DOMElement;
7
8 class Image extends Creatable
9 {
10
11 public function resize(string $file, int $max_w, int $max_h, bool $crop = false): ?string
12 {
13 $editor = wp_get_image_editor($file);
14
15 if (is_wp_error($editor)) {
16 return null;
17 }
18
19 $editor->set_quality(90);
20
21 $resized = $editor->resize($max_w, $max_h, $crop);
22
23 if (is_wp_error($resized)) {
24 return null;
25 }
26
27 $filename = $editor->generate_filename();
28 $saved = $editor->save($filename);
29
30 if (is_wp_error($saved)) {
31 return null;
32 }
33
34 return $filename;
35 }
36
37 public function get_image_by_id(int $id, $size): ?string
38 {
39 if ( ! wp_get_attachment_url($id)) {
40 return null;
41 }
42
43 $attributes = wp_get_attachment_image_src($id, $size);
44
45 // Is Image
46 if ($attributes) {
47 [$src, $width, $height] = $attributes;
48
49 if (
50 is_array($size)
51 && isset($size[0], $size[1])
52 && is_numeric($size[0])
53 && is_numeric($size[1])
54 && $size[0] > 0
55 && $size[1] > 0
56 ) {
57 return $this->markup_cover($src, (int)$size[0], (int)$size[1], $id);
58 }
59
60 // In case of SVG
61 if (is_string($size) && 'svg' === pathinfo($src, PATHINFO_EXTENSION) && 'full' !== $size) {
62 $_size = $this->get_image_sizes_by_name($size);
63
64 $width = (int)($_size['width'] ?? 0);
65 $height = (int)($_size['height'] ?? 0);
66 }
67
68 return $this->markup($src, $width, $height, $id);
69 }
70
71 // Is File - render as pill instead of mime-type icon
72 if (wp_get_attachment_image_src($id, $size, true)) {
73 return $this->markup_file_pill($id);
74 }
75
76 return null;
77 }
78
79 private function markup_file_pill(int $media_id): string
80 {
81 $filename = $this->get_file_name($media_id) ?? '';
82 $extension = (string)pathinfo($filename, PATHINFO_EXTENSION);
83
84 return sprintf(
85 '<span class="ac-file-pill" data-media-id="%s">%s</span>',
86 esc_attr((string)$media_id),
87 Html::create()->file_pill($extension, $filename)
88 );
89 }
90
91 private function scale_size(int $size, float $scale = 1): int
92 {
93 return (int)round($size * $scale);
94 }
95
96 private function is_resized_image(string $path): bool
97 {
98 $fileinfo = pathinfo($path);
99
100 return (bool)preg_match('/-[0-9]+x[0-9]+$/', $fileinfo['filename']);
101 }
102
103 private function get_dimensions_by_sizename($size): array
104 {
105 if (is_string($size)) {
106 $sizes = $this->get_image_sizes_by_name($size);
107
108 if ($sizes) {
109 return [
110 (int)$sizes['width'],
111 (int)$sizes['height'],
112 ];
113 }
114 }
115
116 if (
117 is_array($size)
118 && isset($size[0], $size[1])
119 && is_numeric($size[0])
120 && is_numeric($size[1])
121 && $size[0] > 0
122 && $size[1] > 0
123 ) {
124 return [
125 (int)$size[0],
126 (int)$size[1],
127 ];
128 }
129
130 return [60, 60];
131 }
132
133 public function get_image_by_url(string $url, $size): string
134 {
135 $dimensions = $this->get_dimensions_by_sizename($size);
136
137 $width = $dimensions[0] ?? 0;
138 $height = $dimensions[1] ?? 0;
139
140 $image_path = (string)str_replace(WP_CONTENT_URL, WP_CONTENT_DIR, $url);
141
142 if (is_file($image_path)) {
143 // try to resize image if it is not already resized
144 if ( ! $this->is_resized_image($image_path)) {
145 $resized = $this->resize(
146 $image_path,
147 $width,
148 $height,
149 true
150 );
151
152 if ($resized) {
153 $src = str_replace(WP_CONTENT_DIR, WP_CONTENT_URL, $resized);
154
155 return $this->markup($src, $width, $height);
156 }
157 }
158
159 return $this->markup($url, $width, $height);
160 }
161
162 // External image
163 return $this->markup_cover($image_path, $width, $height);
164 }
165
166 public function get_image(string $image_id_or_url, $size = 'thumbnail'): ?string
167 {
168 if ( ! $image_id_or_url) {
169 return null;
170 }
171
172 if (is_numeric($image_id_or_url)) {
173 return $this->get_image_by_id((int)$image_id_or_url, $size);
174 }
175
176 if (Strings::create()->is_image($image_id_or_url)) {
177 return $this->get_image_by_url($image_id_or_url, $size);
178 }
179
180 return null;
181 }
182
183 private function get_image_sizes_by_name(string $name): array
184 {
185 $available_sizes = wp_get_additional_image_sizes();
186
187 foreach (['thumbnail', 'medium', 'large'] as $key) {
188 $available_sizes[$key] = [
189 'width' => (int)get_option($key . '_size_w'),
190 'height' => (int)get_option($key . '_size_h'),
191 ];
192 }
193
194 return $available_sizes[$name] ?? [];
195 }
196
197 public function get_file_name(int $attachment_id): ?string
198 {
199 $file = get_post_meta($attachment_id, '_wp_attached_file', true);
200
201 if ( ! $file) {
202 return null;
203 }
204
205 return basename($file);
206 }
207
208 public function get_file_extension(int $attachment_id): string
209 {
210 return (string)pathinfo($this->get_file_name($attachment_id), PATHINFO_EXTENSION);
211 }
212
213 private function get_file_tooltip_attr(int $media_id): string
214 {
215 return Html::create()->get_tooltip_attr($this->get_file_name($media_id));
216 }
217
218 private function markup_cover(string $src, int $width, int $height, ?int $media_id = null)
219 {
220 ob_start(); ?>
221
222 <span class="ac-image -cover" data-media-id="<?= esc_attr((string)$media_id); ?>">
223 <img style="width:<?= esc_attr((string)$width); ?>px;height:<?= esc_attr((string)$height); ?>px;" src="<?= esc_attr(
224 $src
225 ); ?>" alt="">
226 </span>
227
228 <?php
229 return ob_get_clean();
230 }
231
232 private function markup(string $src, int $width, int $height, ?int $media_id = null, ?bool $add_extension = false)
233 {
234 $class = '';
235
236 if ($media_id && ! wp_attachment_is_image($media_id)) {
237 $class = ' ac-icon';
238 }
239
240 $image_attributes = [
241 'max-width' => esc_attr((string)$width) . 'px',
242 'max-height' => esc_attr((string)$height) . 'px',
243 ];
244
245 if (pathinfo($src, PATHINFO_EXTENSION) === 'svg') {
246 $image_attributes['width'] = esc_attr((string)$width) . 'px';
247 $image_attributes['height'] = esc_attr((string)$height) . 'px';
248 }
249
250 $tooltip_attr = $media_id
251 ? $this->get_file_tooltip_attr($media_id)
252 : '';
253
254 ob_start(); ?>
255 <span class="ac-image<?= $class ?>" data-media-id="<?= esc_attr((string)$media_id); ?>" <?= $tooltip_attr ?>>
256 <img style="<?= Html::create()->get_style_attributes_as_string($image_attributes) ?>"
257 src="<?= esc_attr($src) ?>" alt="">
258
259 <?php
260 if ($add_extension) : ?>
261 <span class="ac-extension"><?= esc_attr($this->get_file_extension($media_id)) ?></span>
262 <?php
263 endif; ?>
264
265 </span>
266
267 <?php
268 return ob_get_clean();
269 }
270
271 public function get_local_image_info(string $url): ?array
272 {
273 $path = $this->get_local_image_path($url);
274
275 if ( ! $path) {
276 return null;
277 }
278
279 return getimagesize($path);
280 }
281
282 public function get_local_image_path(string $url): ?string
283 {
284 $url = set_url_scheme($url, wp_parse_url(WP_CONTENT_URL, PHP_URL_SCHEME));
285 $path = str_replace(WP_CONTENT_URL, WP_CONTENT_DIR, $url);
286
287 if ( ! file_exists($path)) {
288 return null;
289 }
290
291 return $path;
292 }
293
294 public function get_local_image_size(string $url): ?int
295 {
296 $path = $this->get_local_image_path($url);
297
298 return $path
299 ? filesize($path)
300 : null;
301 }
302
303 public function get_image_urls_from_string(string $string): array
304 {
305 if ( ! $string) {
306 return [];
307 }
308
309 if (false === strpos($string, '<img')) {
310 return [];
311 }
312
313 if ( ! class_exists('DOMDocument')) {
314 return [];
315 }
316
317 $dom = new DOMDocument();
318
319 libxml_use_internal_errors(true);
320 $dom->loadHTML($string);
321 $dom->preserveWhiteSpace = false;
322 libxml_clear_errors();
323
324 $urls = [];
325
326 $images = $dom->getElementsByTagName('img');
327
328 foreach ($images as $img) {
329 /** @var DOMElement $img */
330 $src = $img->getAttribute('src');
331
332 if ( ! $src) {
333 continue;
334 }
335
336 $urls[] = $src;
337 }
338
339 return $urls;
340 }
341
342 }