PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 3.0
Admin Columns v3.0
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 9 years ago Date.php 9 years ago File.php 9 years ago Html.php 9 years ago Icon.php 9 years ago Image.php 9 years ago Network.php 9 years ago Post.php 9 years ago String.php 9 years ago Taxonomy.php 9 years ago User.php 9 years ago
Image.php
247 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( 80, 80 );
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 *
143 * @return array
144 */
145 public function get_images( $images, $size = 'thumbnail' ) {
146 $thumbnails = array();
147
148 foreach ( (array) $images as $value ) {
149 if ( ac_helper()->string->is_image( $value ) ) {
150 $thumbnails[] = $this->get_image_by_url( $value, $size );
151 } // Media Attachment
152 else if ( is_numeric( $value ) && wp_get_attachment_url( $value ) ) {
153 $thumbnails[] = $this->get_image_by_id( $value, $size );
154 }
155 }
156
157 return $thumbnails;
158 }
159
160 /**
161 * @param int|string $image ID of Url
162 * @param string $size
163 *
164 * @return string
165 */
166 public function get_image( $image, $size = 'thumbnail' ) {
167 return implode( $this->get_images( $image, $size ) );
168 }
169
170 /**
171 * @param string $name
172 *
173 * @return array Image sizes
174 */
175 public function get_image_sizes_by_name( $name ) {
176 global $_wp_additional_image_sizes;
177
178 $sizes = false;
179
180 if ( is_scalar( $name ) && isset( $_wp_additional_image_sizes[ $name ] ) ) {
181 $sizes = $_wp_additional_image_sizes[ $name ];
182 }
183
184 return $sizes;
185 }
186
187 /**
188 * @param int $attachment_id
189 *
190 * @return bool|string
191 */
192 public function get_file_name( $attachment_id ) {
193 $file = get_post_meta( $attachment_id, '_wp_attached_file', true );
194
195 if ( ! $file ) {
196 return false;
197 }
198
199 return basename( $file );
200 }
201
202 /**
203 * @param int $attachment_id
204 *
205 * @return string File extension
206 */
207 public function get_file_extension( $attachment_id ) {
208 return pathinfo( $this->get_file_name( $attachment_id ), PATHINFO_EXTENSION );
209 }
210
211 // Helpers
212
213 private function get_file_tooltip_attr( $media_id ) {
214 return ac_helper()->html->get_tooltip_attr( $this->get_file_name( $media_id ) );
215 }
216
217 private function markup_cover( $src, $width, $height, $media_id = null ) {
218 ob_start(); ?>
219 <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>
220
221 <?php
222 return ob_get_clean();
223 }
224
225 private function markup( $src, $width, $height, $media_id = null, $add_extension = false ) {
226 $class = false;
227
228 if ( $media_id && ! wp_attachment_is_image( $media_id ) ) {
229 $class = ' ac-icon';
230 }
231
232 ob_start(); ?>
233 <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 ); ?>>
234 <img style="max-width:<?php echo esc_attr( $width ); ?>px;max-height:<?php echo esc_attr( $height ); ?>px;" src="<?php echo esc_attr( $src ); ?>">
235
236 <?php if ( $add_extension ) : ?>
237 <span class="ac-extension"><?php echo esc_attr( $this->get_file_extension( $media_id ) ); ?></span>
238 <?php endif; ?>
239
240 </span>
241
242 <?php
243 return ob_get_clean();
244 }
245
246 }
247