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