PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 4.6.1
Admin Columns v4.6.1
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 3 years ago Arrays.php 3 years ago Date.php 3 years ago File.php 3 years ago Html.php 3 years ago Icon.php 3 years ago Image.php 3 years ago Media.php 3 years ago Menu.php 3 years ago Network.php 3 years ago Post.php 3 years ago Strings.php 3 years ago Taxonomy.php 3 years ago User.php 3 years ago
Image.php
368 lines
1 <?php
2
3 namespace AC\Helper;
4
5 use DOMDocument;
6 use DOMElement;
7
8 class Image {
9
10 /**
11 * Resize image
12 *
13 * @param string $file
14 * @param int $max_w
15 * @param int $max_h
16 * @param bool $crop
17 * @param null|string $suffix
18 * @param null|string $dest_path
19 * @param int $jpeg_quality
20 *
21 * @return false|string
22 */
23 public function resize(
24 $file,
25 $max_w,
26 $max_h,
27 $crop = false,
28 $suffix = null,
29 $dest_path = null,
30 $jpeg_quality = 90
31 ) {
32 $editor = wp_get_image_editor( $file );
33
34 if ( is_wp_error( $editor ) ) {
35 return false;
36 }
37
38 $editor->set_quality( $jpeg_quality );
39
40 $resized = $editor->resize( $max_w, $max_h, $crop );
41
42 if ( is_wp_error( $resized ) ) {
43 return false;
44 }
45
46 $dest_file = $editor->generate_filename( $suffix, $dest_path );
47 $saved = $editor->save( $dest_file );
48
49 if ( is_wp_error( $saved ) ) {
50 return false;
51 }
52
53 return $dest_file;
54 }
55
56 /**
57 * @param int[]|int $ids
58 * @param array|string $size
59 *
60 * @return string HTML Images
61 */
62 public function get_images_by_ids( $ids, $size ) {
63 $images = [];
64
65 $ids = is_array( $ids ) ? $ids : [ $ids ];
66 foreach ( $ids as $id ) {
67 $images[] = $this->get_image_by_id( $id, $size );
68 }
69
70 return implode( $images );
71 }
72
73 /**
74 * @param int $id
75 * @param string|array $size
76 *
77 * @return string|false
78 */
79 public function get_image_by_id( $id, $size ) {
80 if ( ! is_numeric( $id ) ) {
81 return false;
82 }
83
84 $attributes = wp_get_attachment_image_src( $id, $size );
85
86 // Is Image
87 if ( $attributes ) {
88 [ $src, $width, $height ] = $attributes;
89
90 if ( is_array( $size ) ) {
91 $image = $this->markup_cover( $src, $size[0], $size[1], $id );
92 } else {
93 // In case of SVG
94 if ( 1 === (int) $width && 1 === (int) $height ) {
95 $_size = $this->get_image_sizes_by_name( $size );
96 $width = $_size['width'];
97 $height = $_size['height'];
98 }
99
100 $image = $this->markup( $src, $width, $height, $id );
101 }
102
103 return $image;
104 }
105
106 $attributes = wp_get_attachment_image_src( $id, $size, true );
107
108 // Is File, use icon
109 if ( $attributes ) {
110 return $this->markup( $attributes[0], $this->scale_size( $attributes[1], 0.8 ), $this->scale_size( $attributes[2], 0.8 ), $id, true );
111 }
112
113 return false;
114 }
115
116 /**
117 * @param $size
118 * @param int $scale
119 *
120 * @return float
121 */
122 private function scale_size( $size, $scale = 1 ): float {
123 return round( absint( $size ) * $scale );
124 }
125
126 private function is_resized_image( $path ) {
127 $fileinfo = pathinfo( $path );
128
129 return preg_match( '/-[0-9]+x[0-9]+$/', $fileinfo['filename'] );
130 }
131
132 /**
133 * @param string $url
134 * @param array|string $size
135 *
136 * @return string
137 */
138 public function get_image_by_url( $url, $size ) {
139 $dimensions = [ 60, 60 ];
140
141 if ( is_string( $size ) && ( $sizes = $this->get_image_sizes_by_name( $size ) ) ) {
142 $dimensions = [ $sizes['width'], $sizes['height'] ];
143 } else if ( is_array( $size ) ) {
144 $dimensions = $size;
145 }
146
147 $image_path = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $url );
148
149 if ( is_file( $image_path ) ) {
150 // try to resize image if it is not already resized
151 if ( ! $this->is_resized_image( $image_path ) && ( $resized = $this->resize( $image_path, $dimensions[0], $dimensions[1], true ) ) ) {
152 $src = str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $resized );
153
154 return $this->markup( $src, $dimensions[0], $dimensions[1] );
155 }
156
157 return $this->markup( $url, $dimensions[0], $dimensions[1] );
158 }
159
160 // External image
161 return $this->markup_cover( $image_path, $dimensions[0], $dimensions[1] );
162 }
163
164 /**
165 * @param mixed $images
166 * @param array|string $size
167 * @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).
168 *
169 * @return array
170 */
171 public function get_images( $images, $size = 'thumbnail', $skip_image_check = false ) {
172 $thumbnails = [];
173
174 foreach ( (array) $images as $value ) {
175 if ( $skip_image_check && $value && is_string( $value ) ) {
176 $thumbnails[] = $this->get_image_by_url( $value, $size );
177 } else if ( ac_helper()->string->is_image( $value ) ) {
178 $thumbnails[] = $this->get_image_by_url( $value, $size );
179 } // Media Attachment
180 else if ( is_numeric( $value ) && wp_get_attachment_url( $value ) ) {
181 $thumbnails[] = $this->get_image_by_id( $value, $size );
182 }
183 }
184
185 return $thumbnails;
186 }
187
188 /**
189 * @param int|string $image ID of Url
190 * @param string $size
191 * @param bool $skip_image_check
192 *
193 * @return string
194 */
195 public function get_image( $image, $size = 'thumbnail', $skip_image_check = false ) {
196 return implode( $this->get_images( $image, $size, $skip_image_check ) );
197 }
198
199 /**
200 * @param string $name
201 *
202 * @return array Image sizes
203 */
204 public function get_image_sizes_by_name( $name ) {
205 $available_sizes = wp_get_additional_image_sizes();
206
207 $defaults = [ 'thumbnail', 'medium', 'large' ];
208 foreach ( $defaults as $key ) {
209 $available_sizes[ $key ] = [
210 'width' => get_option( $key . '_size_w' ),
211 'height' => get_option( $key . '_size_h' ),
212 ];
213 }
214
215 $sizes = false;
216
217 if ( is_scalar( $name ) && isset( $available_sizes[ $name ] ) ) {
218 $sizes = $available_sizes[ $name ];
219 }
220
221 return $sizes;
222 }
223
224 /**
225 * @param int $attachment_id
226 *
227 * @return bool|string
228 */
229 public function get_file_name( $attachment_id ) {
230 $file = get_post_meta( $attachment_id, '_wp_attached_file', true );
231
232 if ( ! $file ) {
233 return false;
234 }
235
236 return basename( $file );
237 }
238
239 /**
240 * @param int $attachment_id
241 *
242 * @return string File extension
243 */
244 public function get_file_extension( $attachment_id ) {
245 return pathinfo( $this->get_file_name( $attachment_id ), PATHINFO_EXTENSION );
246 }
247
248 private function get_file_tooltip_attr( $media_id ) {
249 return ac_helper()->html->get_tooltip_attr( $this->get_file_name( $media_id ) );
250 }
251
252 private function markup_cover( $src, $width, $height, $media_id = null ) {
253 ob_start(); ?>
254
255 <span class="ac-image -cover" data-media-id="<?= esc_attr( $media_id ); ?>">
256 <img style="width:<?= esc_attr( $width ); ?>px;height:<?= esc_attr( $height ); ?>px;" src="<?= esc_attr( $src ); ?>" alt="">
257 </span>
258
259 <?php
260 return ob_get_clean();
261 }
262
263 private function markup( $src, $width, $height, $media_id = null, $add_extension = false, $class = '' ) {
264 if ( $media_id && ! wp_attachment_is_image( $media_id ) ) {
265 $class = ' ac-icon';
266 }
267
268 ob_start(); ?>
269 <span class="ac-image <?= esc_attr( $class ); ?>" data-media-id="<?= esc_attr( $media_id ); ?>" <?= $this->get_file_tooltip_attr( $media_id ); ?>>
270 <img style="max-width:<?= esc_attr( $width ); ?>px;max-height:<?= esc_attr( $height ); ?>px;" src="<?= esc_attr( $src ); ?>" alt="">
271
272 <?php if ( $add_extension ) : ?>
273 <span class="ac-extension"><?= esc_attr( $this->get_file_extension( $media_id ) ); ?></span>
274 <?php endif; ?>
275
276 </span>
277
278 <?php
279 return ob_get_clean();
280 }
281
282 /**
283 * Return dimensions and file type
284 *
285 * @param string $url
286 *
287 * @return false|array
288 * @see filesize
289 */
290 public function get_local_image_info( $url ) {
291 $path = $this->get_local_image_path( $url );
292
293 if ( ! $path ) {
294 return false;
295 }
296
297 return getimagesize( $path );
298 }
299
300 /**
301 * @param string $url
302 *
303 * @return false|string
304 */
305 public function get_local_image_path( $url ) {
306 $path = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $url );
307
308 if ( ! file_exists( $path ) ) {
309 return false;
310 }
311
312 return $path;
313 }
314
315 /**
316 * @param string $url
317 *
318 * @return false|int
319 */
320 public function get_local_image_size( $url ) {
321 $path = $this->get_local_image_path( $url );
322
323 if ( ! $path ) {
324 return false;
325 }
326
327 return filesize( $path );
328 }
329
330 /**
331 * @param string $string
332 *
333 * @return array
334 */
335 public function get_image_urls_from_string( $string ) {
336 if ( ! $string ) {
337 return [];
338 }
339
340 if ( false === strpos( $string, '<img' ) ) {
341 return [];
342 }
343
344 if ( ! class_exists( 'DOMDocument' ) ) {
345 return [];
346 }
347
348 $dom = new DOMDocument;
349
350 libxml_use_internal_errors( true );
351 $dom->loadHTML( $string );
352 $dom->preserveWhiteSpace = false;
353 libxml_clear_errors();
354
355 $urls = [];
356
357 $images = $dom->getElementsByTagName( 'img' );
358
359 foreach ( $images as $img ) {
360
361 /** @var DOMElement $img */
362 $urls[] = $img->getAttribute( 'src' );
363 }
364
365 return $urls;
366 }
367
368 }