3 * File contains all the administration image manipulation functions.
6 * @subpackage Administration
10 * Create a thumbnail from an Image given a maximum side size.
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.
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 * @param mixed $deprecated Never used.
21 * @return string Thumbnail path on success, Error string on failure.
23 function wp_create_thumbnail( $file, $max_side, $deprecated = '' ) {
24 if ( !empty( $deprecated ) )
25 _deprecated_argument( __FUNCTION__, '1.2' );
26 $thumbpath = image_resize( $file, $max_side, $max_side );
27 return apply_filters( 'wp_create_thumbnail', $thumbpath );
31 * Crop an Image to a given size.
35 * @param string|int $src_file The source file or Attachment ID.
36 * @param int $src_x The start x position to crop from.
37 * @param int $src_y The start y position to crop from.
38 * @param int $src_w The width to crop.
39 * @param int $src_h The height to crop.
40 * @param int $dst_w The destination width.
41 * @param int $dst_h The destination height.
42 * @param int $src_abs Optional. If the source crop points are absolute.
43 * @param string $dst_file Optional. The destination file to write to.
44 * @return string|WP_Error|false New filepath on success, WP_Error or false on failure.
46 function wp_crop_image( $src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {
47 if ( is_numeric( $src_file ) ) // Handle int as attachment ID
48 $src_file = get_attached_file( $src_file );
50 $src = wp_load_image( $src_file );
52 if ( !is_resource( $src ) )
53 return new WP_Error( 'error_loading_image', $src, $src_file );
55 $dst = wp_imagecreatetruecolor( $dst_w, $dst_h );
62 if (function_exists('imageantialias'))
63 imageantialias( $dst, true );
65 imagecopyresampled( $dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
67 imagedestroy( $src ); // Free up memory
70 $dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file );
72 $dst_file = preg_replace( '/\\.[^\\.]+$/', '.jpg', $dst_file );
74 if ( imagejpeg( $dst, $dst_file, apply_filters( 'jpeg_quality', 90, 'wp_crop_image' ) ) )
81 * Generate post thumbnail attachment meta data.
85 * @param int $attachment_id Attachment Id to process.
86 * @param string $file Filepath of the Attached image.
87 * @return mixed Metadata for attachment.
89 function wp_generate_attachment_metadata( $attachment_id, $file ) {
90 $attachment = get_post( $attachment_id );
93 if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) {
94 $imagesize = getimagesize( $file );
95 $metadata['width'] = $imagesize[0];
96 $metadata['height'] = $imagesize[1];
97 list($uwidth, $uheight) = wp_constrain_dimensions($metadata['width'], $metadata['height'], 128, 96);
98 $metadata['hwstring_small'] = "height='$uheight' width='$uwidth'";
100 // Make the file path relative to the upload dir
101 $metadata['file'] = _wp_relative_upload_path($file);
103 // make thumbnails and other intermediate sizes
104 global $_wp_additional_image_sizes;
106 foreach ( get_intermediate_image_sizes() as $s ) {
107 $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => FALSE );
108 if ( isset( $_wp_additional_image_sizes[$s]['width'] ) )
109 $sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); // For theme-added sizes
111 $sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options
112 if ( isset( $_wp_additional_image_sizes[$s]['height'] ) )
113 $sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); // For theme-added sizes
115 $sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
116 if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) )
117 $sizes[$s]['crop'] = intval( $_wp_additional_image_sizes[$s]['crop'] ); // For theme-added sizes
119 $sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
122 $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );
124 foreach ($sizes as $size => $size_data ) {
125 $resized = image_make_intermediate_size( $file, $size_data['width'], $size_data['height'], $size_data['crop'] );
127 $metadata['sizes'][$size] = $resized;
130 // fetch additional metadata from exif/iptc
131 $image_meta = wp_read_image_metadata( $file );
133 $metadata['image_meta'] = $image_meta;
137 return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id );
141 * Calculated the new dimensions for a downsampled image.
144 * @see wp_constrain_dimensions()
146 * @param int $width Current width of the image
147 * @param int $height Current height of the image
148 * @return mixed Array(height,width) of shrunk dimensions.
150 function get_udims( $width, $height) {
151 return wp_constrain_dimensions( $width, $height, 128, 96 );
155 * Convert a fraction string to a decimal.
162 function wp_exif_frac2dec($str) {
163 @list( $n, $d ) = explode( '/', $str );
170 * Convert the exif date format to a unix timestamp.
177 function wp_exif_date2ts($str) {
178 @list( $date, $time ) = explode( ' ', trim($str) );
179 @list( $y, $m, $d ) = explode( ':', $date );
181 return strtotime( "{$y}-{$m}-{$d} {$time}" );
185 * Get extended image metadata, exif or iptc as available.
187 * Retrieves the EXIF metadata aperture, credit, camera, caption, copyright, iso
188 * created_timestamp, focal_length, shutter_speed, and title.
190 * The IPTC metadata that is retrieved is APP13, credit, byline, created date
191 * and time, caption, copyright, and title. Also includes FNumber, Model,
192 * DateTimeDigitized, FocalLength, ISOSpeedRatings, and ExposureTime.
194 * @todo Try other exif libraries if available.
197 * @param string $file
198 * @return bool|array False on failure. Image metadata array on success.
200 function wp_read_image_metadata( $file ) {
201 if ( ! file_exists( $file ) )
204 list( , , $sourceImageType ) = getimagesize( $file );
206 // exif contains a bunch of data we'll probably never need formatted in ways
207 // that are difficult to use. We'll normalize it and just extract the fields
208 // that are likely to be useful. Fractions and numbers are converted to
209 // floats, dates to unix timestamps, and everything else to strings.
215 'created_timestamp' => 0,
219 'shutter_speed' => 0,
223 // read iptc first, since it might contain data not available in exif such
224 // as caption, description etc
225 if ( is_callable( 'iptcparse' ) ) {
226 getimagesize( $file, $info );
228 if ( ! empty( $info['APP13'] ) ) {
229 $iptc = iptcparse( $info['APP13'] );
231 // headline, "A brief synopsis of the caption."
232 if ( ! empty( $iptc['2#105'][0] ) )
233 $meta['title'] = utf8_encode( trim( $iptc['2#105'][0] ) );
234 // title, "Many use the Title field to store the filename of the image, though the field may be used in many ways."
235 elseif ( ! empty( $iptc['2#005'][0] ) )
236 $meta['title'] = utf8_encode( trim( $iptc['2#005'][0] ) );
238 if ( ! empty( $iptc['2#120'][0] ) ) { // description / legacy caption
239 $caption = utf8_encode( trim( $iptc['2#120'][0] ) );
240 if ( empty( $meta['title'] ) ) {
241 // Assume the title is stored in 2:120 if it's short.
242 if ( strlen( $caption ) < 80 )
243 $meta['title'] = $caption;
245 $meta['caption'] = $caption;
246 } elseif ( $caption != $meta['title'] ) {
247 $meta['caption'] = $caption;
251 if ( ! empty( $iptc['2#110'][0] ) ) // credit
252 $meta['credit'] = utf8_encode(trim($iptc['2#110'][0]));
253 elseif ( ! empty( $iptc['2#080'][0] ) ) // creator / legacy byline
254 $meta['credit'] = utf8_encode(trim($iptc['2#080'][0]));
256 if ( ! empty( $iptc['2#055'][0] ) and ! empty( $iptc['2#060'][0] ) ) // created date and time
257 $meta['created_timestamp'] = strtotime( $iptc['2#055'][0] . ' ' . $iptc['2#060'][0] );
259 if ( ! empty( $iptc['2#116'][0] ) ) // copyright
260 $meta['copyright'] = utf8_encode( trim( $iptc['2#116'][0] ) );
264 // fetch additional info from exif if available
265 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 ) ) ) ) {
266 $exif = @exif_read_data( $file );
268 if ( !empty( $exif['Title'] ) )
269 $meta['title'] = utf8_encode( trim( $exif['Title'] ) );
271 if ( ! empty( $exif['ImageDescription'] ) ) {
272 if ( empty( $meta['title'] ) && strlen( $exif['ImageDescription'] ) < 80 ) {
273 // Assume the title is stored in ImageDescription
274 $meta['title'] = utf8_encode( trim( $exif['ImageDescription'] ) );
275 if ( ! empty( $exif['COMPUTED']['UserComment'] ) && trim( $exif['COMPUTED']['UserComment'] ) != $meta['title'] )
276 $meta['caption'] = utf8_encode( trim( $exif['COMPUTED']['UserComment'] ) );
277 } elseif ( trim( $exif['ImageDescription'] ) != $meta['title'] ) {
278 $meta['caption'] = utf8_encode( trim( $exif['ImageDescription'] ) );
280 } elseif ( ! empty( $exif['Comments'] ) && trim( $exif['Comments'] ) != $meta['title'] ) {
281 $meta['caption'] = utf8_encode( trim( $exif['Comments'] ) );
284 if ( ! empty( $exif['Artist'] ) )
285 $meta['credit'] = utf8_encode( trim( $exif['Artist'] ) );
286 elseif ( ! empty($exif['Author'] ) )
287 $meta['credit'] = utf8_encode( trim( $exif['Author'] ) );
289 if ( ! empty( $exif['Copyright'] ) )
290 $meta['copyright'] = utf8_encode( trim( $exif['Copyright'] ) );
291 if ( ! empty($exif['FNumber'] ) )
292 $meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 );
293 if ( ! empty($exif['Model'] ) )
294 $meta['camera'] = utf8_encode( trim( $exif['Model'] ) );
295 if ( ! empty($exif['DateTimeDigitized'] ) )
296 $meta['created_timestamp'] = wp_exif_date2ts($exif['DateTimeDigitized'] );
297 if ( ! empty($exif['FocalLength'] ) )
298 $meta['focal_length'] = wp_exif_frac2dec( $exif['FocalLength'] );
299 if ( ! empty($exif['ISOSpeedRatings'] ) )
300 $meta['iso'] = utf8_encode( trim( $exif['ISOSpeedRatings'] ) );
301 if ( ! empty($exif['ExposureTime'] ) )
302 $meta['shutter_speed'] = wp_exif_frac2dec( $exif['ExposureTime'] );
305 return apply_filters( 'wp_read_image_metadata', $meta, $file, $sourceImageType );
310 * Validate that file is an image.
314 * @param string $path File path to test if valid image.
315 * @return bool True if valid image, false if not valid image.
317 function file_is_valid_image($path) {
318 $size = @getimagesize($path);
319 return !empty($size);
323 * Validate that file is suitable for displaying within a web page.
326 * @uses apply_filters() Calls 'file_is_displayable_image' on $result and $path.
328 * @param string $path File path to test.
329 * @return bool True if suitable, false if not suitable.
331 function file_is_displayable_image($path) {
332 $info = @getimagesize($path);
335 elseif ( !in_array($info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG)) ) // only gif, jpeg and png images can reliably be displayed
340 return apply_filters('file_is_displayable_image', $result, $path);