1 | <?php |
---|
2 | /** |
---|
3 | * File contains all the administration image manipulation functions. |
---|
4 | * |
---|
5 | * @package WordPress |
---|
6 | * @subpackage Administration |
---|
7 | */ |
---|
8 | |
---|
9 | /** |
---|
10 | * Create a thumbnail from an Image given a maximum side size. |
---|
11 | * |
---|
12 | * This function can handle most image file formats which PHP supports. If PHP |
---|
13 | * does not have the functionality to save in a file of the same format, the |
---|
14 | * thumbnail will be created as a jpeg. |
---|
15 | * |
---|
16 | * @since 1.2.0 |
---|
17 | * |
---|
18 | * @param mixed $file Filename of the original image, Or attachment id. |
---|
19 | * @param int $max_side Maximum length of a single side for the thumbnail. |
---|
20 | * @return string Thumbnail path on success, Error string on failure. |
---|
21 | */ |
---|
22 | function wp_create_thumbnail( $file, $max_side, $deprecated = '' ) { |
---|
23 | $thumbpath = image_resize( $file, $max_side, $max_side ); |
---|
24 | return apply_filters( 'wp_create_thumbnail', $thumbpath ); |
---|
25 | } |
---|
26 | |
---|
27 | /** |
---|
28 | * Crop an Image to a given size. |
---|
29 | * |
---|
30 | * @since 2.1.0 |
---|
31 | * |
---|
32 | * @param string|int $src_file The source file or Attachment ID. |
---|
33 | * @param int $src_x The start x position to crop from. |
---|
34 | * @param int $src_y The start y position to crop from. |
---|
35 | * @param int $src_w The width to crop. |
---|
36 | * @param int $src_h The height to crop. |
---|
37 | * @param int $dst_w The destination width. |
---|
38 | * @param int $dst_h The destination height. |
---|
39 | * @param int $src_abs Optional. If the source crop points are absolute. |
---|
40 | * @param string $dst_file Optional. The destination file to write to. |
---|
41 | * @return string New filepath on success, String error message on failure. |
---|
42 | */ |
---|
43 | function wp_crop_image( $src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) { |
---|
44 | if ( is_numeric( $src_file ) ) // Handle int as attachment ID |
---|
45 | $src_file = get_attached_file( $src_file ); |
---|
46 | |
---|
47 | $src = wp_load_image( $src_file ); |
---|
48 | |
---|
49 | if ( !is_resource( $src )) |
---|
50 | return $src; |
---|
51 | |
---|
52 | $dst = imagecreatetruecolor( $dst_w, $dst_h ); |
---|
53 | |
---|
54 | if ( $src_abs ) { |
---|
55 | $src_w -= $src_x; |
---|
56 | $src_h -= $src_y; |
---|
57 | } |
---|
58 | |
---|
59 | if (function_exists('imageantialias')) |
---|
60 | imageantialias( $dst, true ); |
---|
61 | |
---|
62 | imagecopyresampled( $dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); |
---|
63 | |
---|
64 | imagedestroy( $src ); // Free up memory |
---|
65 | |
---|
66 | if ( ! $dst_file ) |
---|
67 | $dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file ); |
---|
68 | |
---|
69 | $dst_file = preg_replace( '/\\.[^\\.]+$/', '.jpg', $dst_file ); |
---|
70 | |
---|
71 | if ( imagejpeg( $dst, $dst_file ) ) |
---|
72 | return $dst_file; |
---|
73 | else |
---|
74 | return false; |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * Generate post image attachment meta data. |
---|
79 | * |
---|
80 | * @since 2.1.0 |
---|
81 | * |
---|
82 | * @param int $attachment_id Attachment Id to process. |
---|
83 | * @param string $file Filepath of the Attached image. |
---|
84 | * @return mixed Metadata for attachment. |
---|
85 | */ |
---|
86 | function wp_generate_attachment_metadata( $attachment_id, $file ) { |
---|
87 | $attachment = get_post( $attachment_id ); |
---|
88 | |
---|
89 | $metadata = array(); |
---|
90 | if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) { |
---|
91 | $full_path_file = $file; |
---|
92 | $imagesize = getimagesize( $full_path_file ); |
---|
93 | $metadata['width'] = $imagesize[0]; |
---|
94 | $metadata['height'] = $imagesize[1]; |
---|
95 | list($uwidth, $uheight) = wp_shrink_dimensions($metadata['width'], $metadata['height']); |
---|
96 | $metadata['hwstring_small'] = "height='$uheight' width='$uwidth'"; |
---|
97 | |
---|
98 | // Make the file path relative to the upload dir |
---|
99 | if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { // Get upload directory |
---|
100 | if ( 0 === strpos($file, $uploads['basedir']) ) {// Check that the upload base exists in the file path |
---|
101 | $file = str_replace($uploads['basedir'], '', $file); // Remove upload dir from the file path |
---|
102 | $file = ltrim($file, '/'); |
---|
103 | } |
---|
104 | } |
---|
105 | $metadata['file'] = $file; |
---|
106 | |
---|
107 | // make thumbnails and other intermediate sizes |
---|
108 | $sizes = array('thumbnail', 'medium', 'large'); |
---|
109 | $sizes = apply_filters('intermediate_image_sizes', $sizes); |
---|
110 | |
---|
111 | foreach ($sizes as $size) { |
---|
112 | $resized = image_make_intermediate_size( $full_path_file, get_option("{$size}_size_w"), get_option("{$size}_size_h"), get_option("{$size}_crop") ); |
---|
113 | if ( $resized ) |
---|
114 | $metadata['sizes'][$size] = $resized; |
---|
115 | } |
---|
116 | |
---|
117 | // fetch additional metadata from exif/iptc |
---|
118 | $image_meta = wp_read_image_metadata( $full_path_file ); |
---|
119 | if ($image_meta) |
---|
120 | $metadata['image_meta'] = $image_meta; |
---|
121 | |
---|
122 | } |
---|
123 | |
---|
124 | return apply_filters( 'wp_generate_attachment_metadata', $metadata ); |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * Load an image from a string, if PHP supports it. |
---|
129 | * |
---|
130 | * @since 2.1.0 |
---|
131 | * |
---|
132 | * @param string $file Filename of the image to load. |
---|
133 | * @return resource The resulting image resource on success, Error string on failure. |
---|
134 | */ |
---|
135 | function wp_load_image( $file ) { |
---|
136 | if ( is_numeric( $file ) ) |
---|
137 | $file = get_attached_file( $file ); |
---|
138 | |
---|
139 | if ( ! file_exists( $file ) ) |
---|
140 | return sprintf(__("File '%s' doesn't exist?"), $file); |
---|
141 | |
---|
142 | if ( ! function_exists('imagecreatefromstring') ) |
---|
143 | return __('The GD image library is not installed.'); |
---|
144 | |
---|
145 | // Set artificially high because GD uses uncompressed images in memory |
---|
146 | @ini_set('memory_limit', '256M'); |
---|
147 | $image = imagecreatefromstring( file_get_contents( $file ) ); |
---|
148 | |
---|
149 | if ( !is_resource( $image ) ) |
---|
150 | return sprintf(__("File '%s' is not an image."), $file); |
---|
151 | |
---|
152 | return $image; |
---|
153 | } |
---|
154 | |
---|
155 | /** |
---|
156 | * Calculated the new dimentions for a downsampled image. |
---|
157 | * |
---|
158 | * @since 2.0.0 |
---|
159 | * @see wp_shrink_dimensions() |
---|
160 | * |
---|
161 | * @param int $width Current width of the image |
---|
162 | * @param int $height Current height of the image |
---|
163 | * @return mixed Array(height,width) of shrunk dimensions. |
---|
164 | */ |
---|
165 | function get_udims( $width, $height) { |
---|
166 | return wp_shrink_dimensions( $width, $height ); |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | * Calculates the new dimentions for a downsampled image. |
---|
171 | * |
---|
172 | * @since 2.0.0 |
---|
173 | * @see wp_constrain_dimensions() |
---|
174 | * |
---|
175 | * @param int $width Current width of the image |
---|
176 | * @param int $height Current height of the image |
---|
177 | * @param int $wmax Maximum wanted width |
---|
178 | * @param int $hmax Maximum wanted height |
---|
179 | * @return mixed Array(height,width) of shrunk dimensions. |
---|
180 | */ |
---|
181 | function wp_shrink_dimensions( $width, $height, $wmax = 128, $hmax = 96 ) { |
---|
182 | return wp_constrain_dimensions( $width, $height, $wmax, $hmax ); |
---|
183 | } |
---|
184 | |
---|
185 | /** |
---|
186 | * Convert a fraction string to a decimal. |
---|
187 | * |
---|
188 | * @since 2.5.0 |
---|
189 | * |
---|
190 | * @param string $str |
---|
191 | * @return int|float |
---|
192 | */ |
---|
193 | function wp_exif_frac2dec($str) { |
---|
194 | @list( $n, $d ) = explode( '/', $str ); |
---|
195 | if ( !empty($d) ) |
---|
196 | return $n / $d; |
---|
197 | return $str; |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | * Convert the exif date format to a unix timestamp. |
---|
202 | * |
---|
203 | * @since 2.5.0 |
---|
204 | * |
---|
205 | * @param string $str |
---|
206 | * @return int |
---|
207 | */ |
---|
208 | function wp_exif_date2ts($str) { |
---|
209 | @list( $date, $time ) = explode( ' ', trim($str) ); |
---|
210 | @list( $y, $m, $d ) = explode( ':', $date ); |
---|
211 | |
---|
212 | return strtotime( "{$y}-{$m}-{$d} {$time}" ); |
---|
213 | } |
---|
214 | |
---|
215 | /** |
---|
216 | * Convert the exif geo longitude and latitude format from degrees, minutes and |
---|
217 | * seconds to a float degrees. |
---|
218 | * |
---|
219 | * @since 2.7.0 |
---|
220 | * |
---|
221 | * @param string $geo |
---|
222 | * @return float |
---|
223 | */ |
---|
224 | function wp_exif_gpsconvert($geo) { |
---|
225 | @list( $degree, $minute, $second ) = $geo; |
---|
226 | $float = wp_exif_frac2dec($degree) + (wp_exif_frac2dec($minute)/60) + (wp_exif_frac2dec($second)/3600); |
---|
227 | |
---|
228 | return is_float($float) ? $float : 999; |
---|
229 | } |
---|
230 | |
---|
231 | /** |
---|
232 | * Get extended image metadata, exif or iptc as available. |
---|
233 | * |
---|
234 | * Retrieves the EXIF metadata aperture, credit, camera, caption, copyright, iso |
---|
235 | * created_timestamp, focal_length, shutter_speed, and title. |
---|
236 | * |
---|
237 | * The IPTC metadata that is retrieved is APP13, credit, byline, created date |
---|
238 | * and time, caption, copyright, and title. Also includes FNumber, Model, |
---|
239 | * DateTimeDigitized, FocalLength, ISOSpeedRatings, and ExposureTime. |
---|
240 | * |
---|
241 | * @todo Try other exif libraries if available. |
---|
242 | * @since 2.5.0 |
---|
243 | * |
---|
244 | * @param string $file |
---|
245 | * @return bool|array False on failure. Image metadata array on success. |
---|
246 | */ |
---|
247 | function wp_read_image_metadata( $file ) { |
---|
248 | if ( !file_exists( $file ) ) |
---|
249 | return false; |
---|
250 | |
---|
251 | list(,,$sourceImageType) = getimagesize( $file ); |
---|
252 | |
---|
253 | // exif contains a bunch of data we'll probably never need formatted in ways |
---|
254 | // that are difficult to use. We'll normalize it and just extract the fields |
---|
255 | // that are likely to be useful. Fractions and numbers are converted to |
---|
256 | // floats, dates to unix timestamps, and everything else to strings. |
---|
257 | $meta = array( |
---|
258 | 'aperture' => 0, |
---|
259 | 'credit' => '', |
---|
260 | 'camera' => '', |
---|
261 | 'caption' => '', |
---|
262 | 'created_timestamp' => 0, |
---|
263 | 'copyright' => '', |
---|
264 | 'focal_length' => 0, |
---|
265 | 'iso' => 0, |
---|
266 | 'shutter_speed' => 0, |
---|
267 | 'title' => '', |
---|
268 | 'gps_longitude' => 999, |
---|
269 | 'gps_latitude' => 999, |
---|
270 | ); |
---|
271 | |
---|
272 | // read iptc first, since it might contain data not available in exif such |
---|
273 | // as caption, description etc |
---|
274 | if ( is_callable('iptcparse') ) { |
---|
275 | getimagesize($file, $info); |
---|
276 | if ( !empty($info['APP13']) ) { |
---|
277 | $iptc = iptcparse($info['APP13']); |
---|
278 | if ( !empty($iptc['2#110'][0]) ) // credit |
---|
279 | $meta['credit'] = utf8_encode(trim($iptc['2#110'][0])); |
---|
280 | elseif ( !empty($iptc['2#080'][0]) ) // byline |
---|
281 | $meta['credit'] = utf8_encode(trim($iptc['2#080'][0])); |
---|
282 | if ( !empty($iptc['2#055'][0]) and !empty($iptc['2#060'][0]) ) // created date and time |
---|
283 | $meta['created_timestamp'] = strtotime($iptc['2#055'][0] . ' ' . $iptc['2#060'][0]); |
---|
284 | if ( !empty($iptc['2#120'][0]) ) // caption |
---|
285 | $meta['caption'] = utf8_encode(trim($iptc['2#120'][0])); |
---|
286 | if ( !empty($iptc['2#116'][0]) ) // copyright |
---|
287 | $meta['copyright'] = utf8_encode(trim($iptc['2#116'][0])); |
---|
288 | if ( !empty($iptc['2#005'][0]) ) // title |
---|
289 | $meta['title'] = utf8_encode(trim($iptc['2#005'][0])); |
---|
290 | } |
---|
291 | } |
---|
292 | |
---|
293 | // fetch additional info from exif if available |
---|
294 | if ( is_callable('exif_read_data') && in_array($sourceImageType, apply_filters('wp_read_image_metadata_types', array(IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM)) ) ) { |
---|
295 | $exif = @exif_read_data( $file ); |
---|
296 | if (!empty($exif['FNumber'])) |
---|
297 | $meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 ); |
---|
298 | if (!empty($exif['Model'])) |
---|
299 | $meta['camera'] = trim( $exif['Model'] ); |
---|
300 | if (!empty($exif['DateTimeDigitized'])) |
---|
301 | $meta['created_timestamp'] = wp_exif_date2ts($exif['DateTimeDigitized']); |
---|
302 | if (!empty($exif['FocalLength'])) |
---|
303 | $meta['focal_length'] = wp_exif_frac2dec( $exif['FocalLength'] ); |
---|
304 | if (!empty($exif['ISOSpeedRatings'])) |
---|
305 | $meta['iso'] = $exif['ISOSpeedRatings']; |
---|
306 | if (!empty($exif['ExposureTime'])) |
---|
307 | $meta['shutter_speed'] = wp_exif_frac2dec( $exif['ExposureTime'] ); |
---|
308 | if (!empty($exif['GPSLongitude']) && count($exif['GPSLongitude']) == 3 && !empty($exif['GPSLongitudeRef'])) |
---|
309 | $meta['gps_longitude'] = ($exif['GPSLongitudeRef']== 'W' ? '-' : '') . wp_exif_gpsconvert( $exif['GPSLongitude'] ); |
---|
310 | if (!empty($exif['GPSLatitude']) && count($exif['GPSLatitude']) == 3 && !empty($exif['GPSLatitudeRef'])) |
---|
311 | $meta['gps_latitude'] = ($exif['GPSLatitudeRef']== 'S' ? '-' : '') . wp_exif_gpsconvert( $exif['GPSLatitude'] ); |
---|
312 | } |
---|
313 | |
---|
314 | return apply_filters( 'wp_read_image_metadata', $meta, $file, $sourceImageType ); |
---|
315 | |
---|
316 | } |
---|
317 | |
---|
318 | /** |
---|
319 | * Validate that file is an image. |
---|
320 | * |
---|
321 | * @since 2.5.0 |
---|
322 | * |
---|
323 | * @param string $path File path to test if valid image. |
---|
324 | * @return bool True if valid image, false if not valid image. |
---|
325 | */ |
---|
326 | function file_is_valid_image($path) { |
---|
327 | $size = @getimagesize($path); |
---|
328 | return !empty($size); |
---|
329 | } |
---|
330 | |
---|
331 | /** |
---|
332 | * Validate that file is suitable for displaying within a web page. |
---|
333 | * |
---|
334 | * @since 2.5.0 |
---|
335 | * @uses apply_filters() Calls 'file_is_displayable_image' on $result and $path. |
---|
336 | * |
---|
337 | * @param string $path File path to test. |
---|
338 | * @return bool True if suitable, false if not suitable. |
---|
339 | */ |
---|
340 | function file_is_displayable_image($path) { |
---|
341 | $info = @getimagesize($path); |
---|
342 | if ( empty($info) ) |
---|
343 | $result = false; |
---|
344 | elseif ( !in_array($info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG)) ) // only gif, jpeg and png images can reliably be displayed |
---|
345 | $result = false; |
---|
346 | else |
---|
347 | $result = true; |
---|
348 | |
---|
349 | return apply_filters('file_is_displayable_image', $result, $path); |
---|
350 | } |
---|
351 | |
---|
352 | ?> |
---|