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