Image.php
8.19 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
namespace AC\Helper;
use DOMDocument;
use DOMElement;
class Image {
/**
* Resize image
*
* @param string $file
* @param int $max_w
* @param int $max_h
* @param bool $crop
* @param null|string $suffix
* @param null|string $dest_path
* @param int $jpeg_quality
*
* @return false|string
*/
public function resize(
$file,
$max_w,
$max_h,
$crop = false,
$suffix = null,
$dest_path = null,
$jpeg_quality = 90
) {
$editor = wp_get_image_editor( $file );
if ( is_wp_error( $editor ) ) {
return false;
}
$editor->set_quality( $jpeg_quality );
$resized = $editor->resize( $max_w, $max_h, $crop );
if ( is_wp_error( $resized ) ) {
return false;
}
$dest_file = $editor->generate_filename( $suffix, $dest_path );
$saved = $editor->save( $dest_file );
if ( is_wp_error( $saved ) ) {
return false;
}
return $dest_file;
}
/**
* @param int[]|int $ids
* @param array|string $size
*
* @return string HTML Images
*/
public function get_images_by_ids( $ids, $size ) {
$images = [];
$ids = is_array( $ids ) ? $ids : [ $ids ];
foreach ( $ids as $id ) {
$images[] = $this->get_image_by_id( $id, $size );
}
return implode( $images );
}
/**
* @param int $id
* @param string|array $size
*
* @return string
*/
public function get_image_by_id( $id, $size ) {
$image = false;
if ( ! is_numeric( $id ) ) {
return false;
}
// Is Image
if ( $attributes = wp_get_attachment_image_src( $id, $size ) ) {
list( $src, $width, $height ) = $attributes;
if ( is_array( $size ) ) {
$image = $this->markup_cover( $src, $size[0], $size[1], $id );
} else {
// In case of SVG
if ( 1 === (int) $width && 1 === (int) $height ) {
$_size = $this->get_image_sizes_by_name( $size );
$width = $_size['width'];
$height = $_size['height'];
}
$image = $this->markup( $src, $width, $height, $id );
}
} // Is File, use icon
else if ( $attributes = wp_get_attachment_image_src( $id, $size, true ) ) {
$image = $this->markup( $attributes[0], $this->scale_size( $attributes[1], 0.8 ), $this->scale_size( $attributes[2], 0.8 ), $id, true );
}
return $image;
}
/**
* @param $size
* @param int $scale
*
* @return float
*/
private function scale_size( $size, $scale = 1 ) {
return round( absint( $size ) * $scale );
}
private function is_resized_image( $path ) {
$fileinfo = pathinfo( $path );
return preg_match( '/-[0-9]+x[0-9]+$/', $fileinfo['filename'] );
}
/**
* @param string $url
* @param array|string $size
*
* @return string
*/
public function get_image_by_url( $url, $size ) {
$dimensions = [ 60, 60 ];
if ( is_string( $size ) && ( $sizes = $this->get_image_sizes_by_name( $size ) ) ) {
$dimensions = [ $sizes['width'], $sizes['height'] ];
} else if ( is_array( $size ) ) {
$dimensions = $size;
}
$image_path = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $url );
if ( is_file( $image_path ) ) {
// try to resize image if it is not already resized
if ( ! $this->is_resized_image( $image_path ) && $resized = $this->resize( $image_path, $dimensions[0], $dimensions[1], true ) ) {
$src = str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $resized );
$image = $this->markup( $src, $dimensions[0], $dimensions[1] );
} else {
$image = $this->markup( $url, $dimensions[0], $dimensions[1] );
}
} // External image
else {
$image = $this->markup_cover( $image_path, $dimensions[0], $dimensions[1] );
}
return $image;
}
/**
* @param mixed $images
* @param array|string $size
* @param bool $skip_image_check Skips image check. Useful when the url does not have an image extension like jpg or gif (e.g. gravatar).
*
* @return array
*/
public function get_images( $images, $size = 'thumbnail', $skip_image_check = false ) {
$thumbnails = [];
foreach ( (array) $images as $value ) {
if ( $skip_image_check && $value && is_string( $value ) ) {
$thumbnails[] = $this->get_image_by_url( $value, $size );
} else if ( ac_helper()->string->is_image( $value ) ) {
$thumbnails[] = $this->get_image_by_url( $value, $size );
} // Media Attachment
else if ( is_numeric( $value ) && wp_get_attachment_url( $value ) ) {
$thumbnails[] = $this->get_image_by_id( $value, $size );
}
}
return $thumbnails;
}
/**
* @param int|string $image ID of Url
* @param string $size
* @param bool $skip_image_check
*
* @return string
*/
public function get_image( $image, $size = 'thumbnail', $skip_image_check = false ) {
return implode( $this->get_images( $image, $size, $skip_image_check ) );
}
/**
* @param string $name
*
* @return array Image sizes
*/
public function get_image_sizes_by_name( $name ) {
$available_sizes = wp_get_additional_image_sizes();
$defaults = [ 'thumbnail', 'medium', 'large' ];
foreach ( $defaults as $key ) {
$available_sizes[ $key ] = [
'width' => get_option( $key . '_size_w' ),
'height' => get_option( $key . '_size_h' ),
];
}
$sizes = false;
if ( is_scalar( $name ) && isset( $available_sizes[ $name ] ) ) {
$sizes = $available_sizes[ $name ];
}
return $sizes;
}
/**
* @param int $attachment_id
*
* @return bool|string
*/
public function get_file_name( $attachment_id ) {
$file = get_post_meta( $attachment_id, '_wp_attached_file', true );
if ( ! $file ) {
return false;
}
return basename( $file );
}
/**
* @param int $attachment_id
*
* @return string File extension
*/
public function get_file_extension( $attachment_id ) {
return pathinfo( $this->get_file_name( $attachment_id ), PATHINFO_EXTENSION );
}
private function get_file_tooltip_attr( $media_id ) {
return ac_helper()->html->get_tooltip_attr( $this->get_file_name( $media_id ) );
}
private function markup_cover( $src, $width, $height, $media_id = null ) {
ob_start(); ?>
<span class="ac-image -cover" data-media-id="<?= esc_attr( $media_id ); ?>">
<img style="width:<?= esc_attr( $width ); ?>px;height:<?= esc_attr( $height ); ?>px;" src="<?= esc_attr( $src ); ?>" alt="">
</span>
<?php
return ob_get_clean();
}
private function markup( $src, $width, $height, $media_id = null, $add_extension = false, $class = '' ) {
if ( $media_id && ! wp_attachment_is_image( $media_id ) ) {
$class = ' ac-icon';
}
ob_start(); ?>
<span class="ac-image <?= esc_attr( $class ); ?>" data-media-id="<?= esc_attr( $media_id ); ?>" <?= $this->get_file_tooltip_attr( $media_id ); ?>>
<img style="max-width:<?= esc_attr( $width ); ?>px;max-height:<?= esc_attr( $height ); ?>px;" src="<?= esc_attr( $src ); ?>" alt="">
<?php if ( $add_extension ) : ?>
<span class="ac-extension"><?= esc_attr( $this->get_file_extension( $media_id ) ); ?></span>
<?php endif; ?>
</span>
<?php
return ob_get_clean();
}
/**
* Return dimensions and file type
*
* @param string $url
*
* @return false|array
* @see filesize
*/
public function get_local_image_info( $url ) {
$path = $this->get_local_image_path( $url );
if ( ! $path ) {
return false;
}
return getimagesize( $path );
}
/**
* @param string $url
*
* @return false|string
*/
public function get_local_image_path( $url ) {
$path = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $url );
if ( ! file_exists( $path ) ) {
return false;
}
return $path;
}
/**
* @param string $url
*
* @return false|int
*/
public function get_local_image_size( $url ) {
$path = $this->get_local_image_path( $url );
if ( ! $path ) {
return false;
}
return filesize( $path );
}
/**
* @param string $string
*
* @return array
*/
public function get_image_urls_from_string( $string ) {
if ( ! $string ) {
return [];
}
if ( false === strpos( $string, '<img' ) ) {
return [];
}
if ( ! class_exists( 'DOMDocument' ) ) {
return [];
}
$dom = new DOMDocument;
libxml_use_internal_errors( true );
$dom->loadHTML( $string );
$dom->preserveWhiteSpace = false;
libxml_clear_errors();
$urls = [];
$images = $dom->getElementsByTagName( 'img' );
foreach ( $images as $img ) {
/** @var DOMElement $img */
$urls[] = $img->getAttribute( 'src' );
}
return $urls;
}
}