| 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 | * @param mixed $deprecated Never used. |
|---|
| 21 | * @return string Thumbnail path on success, Error string on failure. |
|---|
| 22 | */ |
|---|
| 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 ); |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | * Crop an Image to a given size. |
|---|
| 32 | * |
|---|
| 33 | * @since 2.1.0 |
|---|
| 34 | * |
|---|
| 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. |
|---|
| 45 | */ |
|---|
| 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 ); |
|---|
| 49 | |
|---|
| 50 | $src = wp_load_image( $src_file ); |
|---|
| 51 | |
|---|
| 52 | if ( !is_resource( $src ) ) |
|---|
| 53 | return new WP_Error( 'error_loading_image', $src, $src_file ); |
|---|
| 54 | |
|---|
| 55 | $dst = wp_imagecreatetruecolor( $dst_w, $dst_h ); |
|---|
| 56 | |
|---|
| 57 | if ( $src_abs ) { |
|---|
| 58 | $src_w -= $src_x; |
|---|
| 59 | $src_h -= $src_y; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | if (function_exists('imageantialias')) |
|---|
| 63 | imageantialias( $dst, true ); |
|---|
| 64 | |
|---|
| 65 | imagecopyresampled( $dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); |
|---|
| 66 | |
|---|
| 67 | imagedestroy( $src ); // Free up memory |
|---|
| 68 | |
|---|
| 69 | if ( ! $dst_file ) |
|---|
| 70 | $dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file ); |
|---|
| 71 | |
|---|
| 72 | $dst_file = preg_replace( '/\\.[^\\.]+$/', '.jpg', $dst_file ); |
|---|
| 73 | |
|---|
| 74 | if ( imagejpeg( $dst, $dst_file, apply_filters( 'jpeg_quality', 90, 'wp_crop_image' ) ) ) |
|---|
| 75 | return $dst_file; |
|---|
| 76 | else |
|---|
| 77 | return false; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | /** |
|---|
| 81 | * Generate post thumbnail attachment meta data. |
|---|
| 82 | * |
|---|
| 83 | * @since 2.1.0 |
|---|
| 84 | * |
|---|
| 85 | * @param int $attachment_id Attachment Id to process. |
|---|
| 86 | * @param string $file Filepath of the Attached image. |
|---|
| 87 | * @return mixed Metadata for attachment. |
|---|
| 88 | */ |
|---|
| 89 | function wp_generate_attachment_metadata( $attachment_id, $file ) { |
|---|
| 90 | $attachment = get_post( $attachment_id ); |
|---|
| 91 | |
|---|
| 92 | $metadata = array(); |
|---|
| 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'"; |
|---|
| 99 | |
|---|
| 100 | // Make the file path relative to the upload dir |
|---|
| 101 | $metadata['file'] = _wp_relative_upload_path($file); |
|---|
| 102 | |
|---|
| 103 | // make thumbnails and other intermediate sizes |
|---|
| 104 | global $_wp_additional_image_sizes; |
|---|
| 105 | |
|---|
| 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 |
|---|
| 110 | else |
|---|
| 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 |
|---|
| 114 | else |
|---|
| 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 |
|---|
| 118 | else |
|---|
| 119 | $sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes ); |
|---|
| 123 | |
|---|
| 124 | foreach ($sizes as $size => $size_data ) { |
|---|
| 125 | $resized = image_make_intermediate_size( $file, $size_data['width'], $size_data['height'], $size_data['crop'] ); |
|---|
| 126 | if ( $resized ) |
|---|
| 127 | $metadata['sizes'][$size] = $resized; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | // fetch additional metadata from exif/iptc |
|---|
| 131 | $image_meta = wp_read_image_metadata( $file ); |
|---|
| 132 | if ( $image_meta ) |
|---|
| 133 | $metadata['image_meta'] = $image_meta; |
|---|
| 134 | |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id ); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | /** |
|---|
| 141 | * Load an image from a string, if PHP supports it. |
|---|
| 142 | * |
|---|
| 143 | * @since 2.1.0 |
|---|
| 144 | * |
|---|
| 145 | * @param string $file Filename of the image to load. |
|---|
| 146 | * @return resource The resulting image resource on success, Error string on failure. |
|---|
| 147 | */ |
|---|
| 148 | function wp_load_image( $file ) { |
|---|
| 149 | if ( is_numeric( $file ) ) |
|---|
| 150 | $file = get_attached_file( $file ); |
|---|
| 151 | |
|---|
| 152 | if ( ! file_exists( $file ) ) |
|---|
| 153 | return sprintf(__('File “%s” doesn’t exist?'), $file); |
|---|
| 154 | |
|---|
| 155 | if ( ! function_exists('imagecreatefromstring') ) |
|---|
| 156 | return __('The GD image library is not installed.'); |
|---|
| 157 | |
|---|
| 158 | // Set artificially high because GD uses uncompressed images in memory |
|---|
| 159 | @ini_set('memory_limit', '256M'); |
|---|
| 160 | $image = imagecreatefromstring( file_get_contents( $file ) ); |
|---|
| 161 | |
|---|
| 162 | if ( !is_resource( $image ) ) |
|---|
| 163 | return sprintf(__('File “%s” is not an image.'), $file); |
|---|
| 164 | |
|---|
| 165 | return $image; |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | /** |
|---|
| 169 | * Calculated the new dimentions for a downsampled image. |
|---|
| 170 | * |
|---|
| 171 | * @since 2.0.0 |
|---|
| 172 | * @see wp_constrain_dimensions() |
|---|
| 173 | * |
|---|
| 174 | * @param int $width Current width of the image |
|---|
| 175 | * @param int $height Current height of the image |
|---|
| 176 | * @return mixed Array(height,width) of shrunk dimensions. |
|---|
| 177 | */ |
|---|
| 178 | function get_udims( $width, $height) { |
|---|
| 179 | return wp_constrain_dimensions( $width, $height, 128, 96 ); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | /** |
|---|
| 183 | * Convert a fraction string to a decimal. |
|---|
| 184 | * |
|---|
| 185 | * @since 2.5.0 |
|---|
| 186 | * |
|---|
| 187 | * @param string $str |
|---|
| 188 | * @return int|float |
|---|
| 189 | */ |
|---|
| 190 | function wp_exif_frac2dec($str) { |
|---|
| 191 | @list( $n, $d ) = explode( '/', $str ); |
|---|
| 192 | if ( !empty($d) ) |
|---|
| 193 | return $n / $d; |
|---|
| 194 | return $str; |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | /** |
|---|
| 198 | * Convert the exif date format to a unix timestamp. |
|---|
| 199 | * |
|---|
| 200 | * @since 2.5.0 |
|---|
| 201 | * |
|---|
| 202 | * @param string $str |
|---|
| 203 | * @return int |
|---|
| 204 | */ |
|---|
| 205 | function wp_exif_date2ts($str) { |
|---|
| 206 | @list( $date, $time ) = explode( ' ', trim($str) ); |
|---|
| 207 | @list( $y, $m, $d ) = explode( ':', $date ); |
|---|
| 208 | |
|---|
| 209 | return strtotime( "{$y}-{$m}-{$d} {$time}" ); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | /** |
|---|
| 213 | * Get extended image metadata, exif or iptc as available. |
|---|
| 214 | * |
|---|
| 215 | * Retrieves the EXIF metadata aperture, credit, camera, caption, copyright, iso |
|---|
| 216 | * created_timestamp, focal_length, shutter_speed, and title. |
|---|
| 217 | * |
|---|
| 218 | * The IPTC metadata that is retrieved is APP13, credit, byline, created date |
|---|
| 219 | * and time, caption, copyright, and title. Also includes FNumber, Model, |
|---|
| 220 | * DateTimeDigitized, FocalLength, ISOSpeedRatings, and ExposureTime. |
|---|
| 221 | * |
|---|
| 222 | * @todo Try other exif libraries if available. |
|---|
| 223 | * @since 2.5.0 |
|---|
| 224 | * |
|---|
| 225 | * @param string $file |
|---|
| 226 | * @return bool|array False on failure. Image metadata array on success. |
|---|
| 227 | */ |
|---|
| 228 | function wp_read_image_metadata( $file ) { |
|---|
| 229 | if ( !file_exists( $file ) ) |
|---|
| 230 | return false; |
|---|
| 231 | |
|---|
| 232 | list(,,$sourceImageType) = getimagesize( $file ); |
|---|
| 233 | |
|---|
| 234 | // exif contains a bunch of data we'll probably never need formatted in ways |
|---|
| 235 | // that are difficult to use. We'll normalize it and just extract the fields |
|---|
| 236 | // that are likely to be useful. Fractions and numbers are converted to |
|---|
| 237 | // floats, dates to unix timestamps, and everything else to strings. |
|---|
| 238 | $meta = array( |
|---|
| 239 | 'aperture' => 0, |
|---|
| 240 | 'credit' => '', |
|---|
| 241 | 'camera' => '', |
|---|
| 242 | 'caption' => '', |
|---|
| 243 | 'created_timestamp' => 0, |
|---|
| 244 | 'copyright' => '', |
|---|
| 245 | 'focal_length' => 0, |
|---|
| 246 | 'iso' => 0, |
|---|
| 247 | 'shutter_speed' => 0, |
|---|
| 248 | 'title' => '', |
|---|
| 249 | ); |
|---|
| 250 | |
|---|
| 251 | // read iptc first, since it might contain data not available in exif such |
|---|
| 252 | // as caption, description etc |
|---|
| 253 | if ( is_callable('iptcparse') ) { |
|---|
| 254 | getimagesize($file, $info); |
|---|
| 255 | error_log(var_export($info, TRUE)); |
|---|
| 256 | if ( !empty($info['APP13']) ) { |
|---|
| 257 | $iptc = iptcparse($info['APP13']); |
|---|
| 258 | error_log(var_export($iptc, TRUE)); |
|---|
| 259 | if ( !empty($iptc['2#105'][0]) ) // headline, "A brief synopsis of the caption." |
|---|
| 260 | $meta['title'] = utf8_encode(trim($iptc['2#105'][0])); |
|---|
| 261 | elseif ( !empty($iptc['2#005'][0]) ) // title, "Many use the Title field to store the filename of the image, though the field may be used in many ways." |
|---|
| 262 | $meta['title'] = utf8_encode(trim($iptc['2#005'][0])); |
|---|
| 263 | |
|---|
| 264 | if ( !empty($iptc['2#120'][0]) ) { // description / legacy caption |
|---|
| 265 | $caption = utf8_encode(trim($iptc['2#120'][0])); |
|---|
| 266 | if (empty($meta['title'])) { |
|---|
| 267 | if (strlen($caption) < 80) { |
|---|
| 268 | // In this situation, assume the title is stored in 2:120. |
|---|
| 269 | $meta['title'] = $caption; |
|---|
| 270 | } else { |
|---|
| 271 | $meta['caption'] = $caption; |
|---|
| 272 | } |
|---|
| 273 | } elseif ($caption != $meta['title']) { |
|---|
| 274 | $meta['caption'] = $caption; |
|---|
| 275 | } |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 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]) ) // creator / legacy byline |
|---|
| 281 | $meta['credit'] = utf8_encode(trim($iptc['2#080'][0])); |
|---|
| 282 | |
|---|
| 283 | if ( !empty($iptc['2#055'][0]) and !empty($iptc['2#060'][0]) ) // created date and time |
|---|
| 284 | $meta['created_timestamp'] = strtotime($iptc['2#055'][0] . ' ' . $iptc['2#060'][0]); |
|---|
| 285 | |
|---|
| 286 | if ( !empty($iptc['2#116'][0]) ) // copyright |
|---|
| 287 | $meta['copyright'] = utf8_encode(trim($iptc['2#116'][0])); |
|---|
| 288 | } |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | // fetch additional info from exif if available |
|---|
| 292 | 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)) ) ) { |
|---|
| 293 | $exif = @exif_read_data( $file ); |
|---|
| 294 | error_log(var_export($exif, TRUE)); |
|---|
| 295 | if (!empty($exif['Title'])) |
|---|
| 296 | $meta['title'] = trim( $exif['Title'] ); |
|---|
| 297 | |
|---|
| 298 | if (!empty($exif['ImageDescription'])) { |
|---|
| 299 | if (empty($meta['title']) && strlen($exif['ImageDescription']) < 80) { |
|---|
| 300 | // In this situation, the title is stored in ImageDescription |
|---|
| 301 | $meta['title'] = trim($exif['ImageDescription']); |
|---|
| 302 | if (!empty($exif['COMPUTED']['UserComment']) && trim($exif['COMPUTED']['UserComment']) != $meta['title']) { |
|---|
| 303 | $meta['caption'] = trim($exif['COMPUTED']['UserComment']); |
|---|
| 304 | } |
|---|
| 305 | } elseif (trim($exif['ImageDescription']) != $meta['title']) { |
|---|
| 306 | $meta['caption'] = trim($exif['ImageDescription']); |
|---|
| 307 | } |
|---|
| 308 | } elseif (!empty($exif['Comments']) && trim($exif['Comments']) != $meta['title']) { |
|---|
| 309 | $meta['caption'] = trim( $exif['Comments'] ); |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | if (!empty($exif['Artist'])) |
|---|
| 313 | $meta['credit'] = trim( $exif['Artist'] ); |
|---|
| 314 | elseif (!empty($exif['Author'])) |
|---|
| 315 | $meta['credit'] = trim( $exif['Author'] ); |
|---|
| 316 | |
|---|
| 317 | if (!empty($exif['Copyright'])) |
|---|
| 318 | $meta['copyright'] = trim( $exif['Copyright'] ); |
|---|
| 319 | if (!empty($exif['FNumber'])) |
|---|
| 320 | $meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 ); |
|---|
| 321 | if (!empty($exif['Model'])) |
|---|
| 322 | $meta['camera'] = trim( $exif['Model'] ); |
|---|
| 323 | if (!empty($exif['DateTimeDigitized'])) |
|---|
| 324 | $meta['created_timestamp'] = wp_exif_date2ts($exif['DateTimeDigitized']); |
|---|
| 325 | if (!empty($exif['FocalLength'])) |
|---|
| 326 | $meta['focal_length'] = wp_exif_frac2dec( $exif['FocalLength'] ); |
|---|
| 327 | if (!empty($exif['ISOSpeedRatings'])) |
|---|
| 328 | $meta['iso'] = $exif['ISOSpeedRatings']; |
|---|
| 329 | if (!empty($exif['ExposureTime'])) |
|---|
| 330 | $meta['shutter_speed'] = wp_exif_frac2dec( $exif['ExposureTime'] ); |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | return apply_filters( 'wp_read_image_metadata', $meta, $file, $sourceImageType ); |
|---|
| 334 | |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | /** |
|---|
| 338 | * Validate that file is an image. |
|---|
| 339 | * |
|---|
| 340 | * @since 2.5.0 |
|---|
| 341 | * |
|---|
| 342 | * @param string $path File path to test if valid image. |
|---|
| 343 | * @return bool True if valid image, false if not valid image. |
|---|
| 344 | */ |
|---|
| 345 | function file_is_valid_image($path) { |
|---|
| 346 | $size = @getimagesize($path); |
|---|
| 347 | return !empty($size); |
|---|
| 348 | } |
|---|
| 349 | |
|---|
| 350 | /** |
|---|
| 351 | * Validate that file is suitable for displaying within a web page. |
|---|
| 352 | * |
|---|
| 353 | * @since 2.5.0 |
|---|
| 354 | * @uses apply_filters() Calls 'file_is_displayable_image' on $result and $path. |
|---|
| 355 | * |
|---|
| 356 | * @param string $path File path to test. |
|---|
| 357 | * @return bool True if suitable, false if not suitable. |
|---|
| 358 | */ |
|---|
| 359 | function file_is_displayable_image($path) { |
|---|
| 360 | $info = @getimagesize($path); |
|---|
| 361 | if ( empty($info) ) |
|---|
| 362 | $result = false; |
|---|
| 363 | elseif ( !in_array($info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG)) ) // only gif, jpeg and png images can reliably be displayed |
|---|
| 364 | $result = false; |
|---|
| 365 | else |
|---|
| 366 | $result = true; |
|---|
| 367 | |
|---|
| 368 | return apply_filters('file_is_displayable_image', $result, $path); |
|---|
| 369 | } |
|---|