Plugin Directory

source: codepress-admin-columns/trunk/classes/Helper/Image.php @ 2293580

Last change on this file since 2293580 was 2293580, checked in by tschutter, 5 years ago

trunk

File size: 8.0 KB
Line 
1<?php
2
3namespace AC\Helper;
4
5use DOMDocument;
6use DOMElement;
7use WP_Error;
8
9class Image {
10
11        /**
12         * Resize image
13         *
14         * @param string      $file
15         * @param int         $max_w
16         * @param int         $max_h
17         * @param bool        $crop
18         * @param null|string $suffix
19         * @param null|string $dest_path
20         * @param int         $jpeg_quality
21         *
22         * @return bool|string|WP_Error
23         */
24        public function resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) {
25                $editor = wp_get_image_editor( $file );
26
27                if ( is_wp_error( $editor ) ) {
28                        return false;
29                }
30
31                $editor->set_quality( $jpeg_quality );
32
33                $resized = $editor->resize( $max_w, $max_h, $crop );
34
35                if ( is_wp_error( $resized ) ) {
36                        return false;
37                }
38
39                $dest_file = $editor->generate_filename( $suffix, $dest_path );
40                $saved = $editor->save( $dest_file );
41
42                if ( is_wp_error( $saved ) ) {
43                        return false;
44                }
45
46                return $dest_file;
47        }
48
49        /**
50         * @param int[]|int    $ids
51         * @param array|string $size
52         *
53         * @return string HTML Images
54         */
55        public function get_images_by_ids( $ids, $size ) {
56                $images = [];
57
58                $ids = is_array( $ids ) ? $ids : [ $ids ];
59                foreach ( $ids as $id ) {
60                        $images[] = $this->get_image_by_id( $id, $size );
61                }
62
63                return implode( $images );
64        }
65
66        /**
67         * @param int          $id
68         * @param string|array $size
69         *
70         * @return string
71         */
72        public function get_image_by_id( $id, $size ) {
73                $image = false;
74
75                if ( ! is_numeric( $id ) ) {
76                        return false;
77                }
78
79                // Is Image
80                if ( $attributes = wp_get_attachment_image_src( $id, $size ) ) {
81                        $src = $attributes[0];
82
83                        if ( is_array( $size ) ) {
84                                $image = $this->markup_cover( $src, $size[0], $size[1], $id );
85                        } else {
86                                $image = $this->markup( $src, $attributes[1], $attributes[2], $id );
87                        }
88                } // Is File, use icon
89                else if ( $attributes = wp_get_attachment_image_src( $id, $size, true ) ) {
90                        $image = $this->markup( $attributes[0], $this->scale_size( $attributes[1], 0.8 ), $this->scale_size( $attributes[2], 0.8 ), $id, true );
91                }
92
93                return $image;
94        }
95
96        /**
97         * @param     $size
98         * @param int $scale
99         *
100         * @return float
101         */
102        private function scale_size( $size, $scale = 1 ) {
103                return round( absint( $size ) * $scale );
104        }
105
106        private function is_resized_image( $path ) {
107                $fileinfo = pathinfo( $path );
108
109                return preg_match( '/-[0-9]+x[0-9]+$/', $fileinfo['filename'] );
110        }
111
112        /**
113         * @param string       $url
114         * @param array|string $size
115         *
116         * @return string
117         */
118        public function get_image_by_url( $url, $size ) {
119                $dimensions = [ 60, 60 ];
120
121                if ( is_string( $size ) && ( $sizes = $this->get_image_sizes_by_name( $size ) ) ) {
122                        $dimensions = [ $sizes['width'], $sizes['height'] ];
123                } else if ( is_array( $size ) ) {
124                        $dimensions = $size;
125                }
126
127                $image_path = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $url );
128
129                if ( is_file( $image_path ) ) {
130                        // try to resize image if it is not already resized
131                        if ( ! $this->is_resized_image( $image_path ) && $resized = $this->resize( $image_path, $dimensions[0], $dimensions[1], true ) ) {
132                                $src = str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $resized );
133
134                                $image = $this->markup( $src, $dimensions[0], $dimensions[1] );
135                        } else {
136
137                                $image = $this->markup( $url, $dimensions[0], $dimensions[1] );
138                        }
139                } // External image
140                else {
141                        $image = $this->markup_cover( $image_path, $dimensions[0], $dimensions[1] );
142                }
143
144                return $image;
145        }
146
147        /**
148         * @param mixed        $images
149         * @param array|string $size
150         * @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).
151         *
152         * @return array
153         */
154        public function get_images( $images, $size = 'thumbnail', $skip_image_check = false ) {
155                $thumbnails = [];
156
157                foreach ( (array) $images as $value ) {
158                        if ( $skip_image_check && $value && is_string( $value ) ) {
159                                $thumbnails[] = $this->get_image_by_url( $value, $size );
160                        } else if ( ac_helper()->string->is_image( $value ) ) {
161                                $thumbnails[] = $this->get_image_by_url( $value, $size );
162                        } // Media Attachment
163                        else if ( is_numeric( $value ) && wp_get_attachment_url( $value ) ) {
164                                $thumbnails[] = $this->get_image_by_id( $value, $size );
165                        }
166                }
167
168                return $thumbnails;
169        }
170
171        /**
172         * @param int|string $image ID of Url
173         * @param string     $size
174         * @param bool       $skip_image_check
175         *
176         * @return string
177         */
178        public function get_image( $image, $size = 'thumbnail', $skip_image_check = false ) {
179                return implode( $this->get_images( $image, $size, $skip_image_check ) );
180        }
181
182        /**
183         * @param string $name
184         *
185         * @return array Image sizes
186         */
187        public function get_image_sizes_by_name( $name ) {
188                $available_sizes = wp_get_additional_image_sizes();
189
190                $defaults = [ 'thumbnail', 'medium', 'large' ];
191                foreach ( $defaults as $key ) {
192                        $available_sizes[ $key ] = [
193                                'width'  => get_option( $key . '_size_w' ),
194                                'height' => get_option( $key . '_size_h' ),
195                        ];
196                }
197
198                $sizes = false;
199
200                if ( is_scalar( $name ) && isset( $available_sizes[ $name ] ) ) {
201                        $sizes = $available_sizes[ $name ];
202                }
203
204                return $sizes;
205        }
206
207        /**
208         * @param int $attachment_id
209         *
210         * @return bool|string
211         */
212        public function get_file_name( $attachment_id ) {
213                $file = get_post_meta( $attachment_id, '_wp_attached_file', true );
214
215                if ( ! $file ) {
216                        return false;
217                }
218
219                return basename( $file );
220        }
221
222        /**
223         * @param int $attachment_id
224         *
225         * @return string File extension
226         */
227        public function get_file_extension( $attachment_id ) {
228                return pathinfo( $this->get_file_name( $attachment_id ), PATHINFO_EXTENSION );
229        }
230
231        private function get_file_tooltip_attr( $media_id ) {
232                return ac_helper()->html->get_tooltip_attr( $this->get_file_name( $media_id ) );
233        }
234
235        private function markup_cover( $src, $width, $height, $media_id = null ) {
236                ob_start(); ?>
237                <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>
238
239                <?php
240                return ob_get_clean();
241        }
242
243        private function markup( $src, $width, $height, $media_id = null, $add_extension = false ) {
244                $class = false;
245
246                if ( $media_id && ! wp_attachment_is_image( $media_id ) ) {
247                        $class = ' ac-icon';
248                }
249
250                ob_start(); ?>
251                <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 ); ?>>
252                        <img style="max-width:<?php echo esc_attr( $width ); ?>px;max-height:<?php echo esc_attr( $height ); ?>px;" src="<?php echo esc_attr( $src ); ?>" alt="">
253
254                        <?php if ( $add_extension ) : ?>
255                                <span class="ac-extension"><?php echo 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 ( ! class_exists( 'DOMDocument' ) ) {
323                        return [];
324                }
325
326                $dom = new DOMDocument;
327                @$dom->loadHTML( $string );
328                $dom->preserveWhiteSpace = false;
329
330                $urls = [];
331
332                $images = $dom->getElementsByTagName( 'img' );
333
334                foreach ( $images as $img ) {
335
336                        /** @var DOMElement $img */
337                        $urls[] = $img->getAttribute( 'src' );
338                }
339
340                return $urls;
341        }
342
343}
Note: See TracBrowser for help on using the repository browser.