[ Index ] |
WordPress Cross Reference |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * File contains all the administration image manipulation functions. 4 * 5 * @package WordPress 6 * @subpackage Administration 7 */ 8 9 /** 10 * Crop an Image to a given size. 11 * 12 * @since 2.1.0 13 * 14 * @param string|int $src The source file or Attachment ID. 15 * @param int $src_x The start x position to crop from. 16 * @param int $src_y The start y position to crop from. 17 * @param int $src_w The width to crop. 18 * @param int $src_h The height to crop. 19 * @param int $dst_w The destination width. 20 * @param int $dst_h The destination height. 21 * @param int $src_abs Optional. If the source crop points are absolute. 22 * @param string $dst_file Optional. The destination file to write to. 23 * @return string|WP_Error New filepath on success, WP_Error on failure. 24 */ 25 function wp_crop_image( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) { 26 $src_file = $src; 27 if ( is_numeric( $src ) ) { // Handle int as attachment ID 28 $src_file = get_attached_file( $src ); 29 30 if ( ! file_exists( $src_file ) ) { 31 // If the file doesn't exist, attempt a url fopen on the src link. 32 // This can occur with certain file replication plugins. 33 $src = _load_image_to_edit_path( $src, 'full' ); 34 } else { 35 $src = $src_file; 36 } 37 } 38 39 $editor = wp_get_image_editor( $src ); 40 if ( is_wp_error( $editor ) ) 41 return $editor; 42 43 $src = $editor->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs ); 44 if ( is_wp_error( $src ) ) 45 return $src; 46 47 if ( ! $dst_file ) 48 $dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file ); 49 50 // The directory containing the original file may no longer exist when 51 // using a replication plugin. 52 wp_mkdir_p( dirname( $dst_file ) ); 53 54 $dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) ); 55 56 $result = $editor->save( $dst_file ); 57 if ( is_wp_error( $result ) ) 58 return $result; 59 60 return $dst_file; 61 } 62 63 /** 64 * Generate post thumbnail attachment meta data. 65 * 66 * @since 2.1.0 67 * 68 * @param int $attachment_id Attachment Id to process. 69 * @param string $file Filepath of the Attached image. 70 * @return mixed Metadata for attachment. 71 */ 72 function wp_generate_attachment_metadata( $attachment_id, $file ) { 73 $attachment = get_post( $attachment_id ); 74 75 $metadata = array(); 76 $support = false; 77 if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) { 78 $imagesize = getimagesize( $file ); 79 $metadata['width'] = $imagesize[0]; 80 $metadata['height'] = $imagesize[1]; 81 82 // Make the file path relative to the upload dir 83 $metadata['file'] = _wp_relative_upload_path($file); 84 85 // make thumbnails and other intermediate sizes 86 global $_wp_additional_image_sizes; 87 88 $sizes = array(); 89 foreach ( get_intermediate_image_sizes() as $s ) { 90 $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => false ); 91 if ( isset( $_wp_additional_image_sizes[$s]['width'] ) ) 92 $sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); // For theme-added sizes 93 else 94 $sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options 95 if ( isset( $_wp_additional_image_sizes[$s]['height'] ) ) 96 $sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); // For theme-added sizes 97 else 98 $sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options 99 if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) ) 100 $sizes[$s]['crop'] = intval( $_wp_additional_image_sizes[$s]['crop'] ); // For theme-added sizes 101 else 102 $sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options 103 } 104 105 $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes ); 106 107 if ( $sizes ) { 108 $editor = wp_get_image_editor( $file ); 109 110 if ( ! is_wp_error( $editor ) ) 111 $metadata['sizes'] = $editor->multi_resize( $sizes ); 112 } else { 113 $metadata['sizes'] = array(); 114 } 115 116 // fetch additional metadata from exif/iptc 117 $image_meta = wp_read_image_metadata( $file ); 118 if ( $image_meta ) 119 $metadata['image_meta'] = $image_meta; 120 121 } elseif ( preg_match( '#^video/#', get_post_mime_type( $attachment ) ) ) { 122 $metadata = wp_read_video_metadata( $file ); 123 $support = current_theme_supports( 'post-thumbnails', 'attachment:video' ) && post_type_supports( 'attachment:video', 'thumbnail' ); 124 } elseif ( preg_match( '#^audio/#', get_post_mime_type( $attachment ) ) ) { 125 $metadata = wp_read_audio_metadata( $file ); 126 $support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) && post_type_supports( 'attachment:audio', 'thumbnail' ); 127 } 128 129 if ( $support && ! empty( $metadata['image']['data'] ) ) { 130 $ext = '.jpg'; 131 switch ( $metadata['image']['mime'] ) { 132 case 'image/gif': 133 $ext = '.gif'; 134 break; 135 case 'image/png': 136 $ext = '.png'; 137 break; 138 } 139 $basename = str_replace( '.', '-', basename( $file ) ) . '-image' . $ext; 140 $uploaded = wp_upload_bits( $basename, '', $metadata['image']['data'] ); 141 if ( false === $uploaded['error'] ) { 142 $attachment = array( 143 'post_mime_type' => $metadata['image']['mime'], 144 'post_type' => 'attachment', 145 'post_content' => '', 146 ); 147 $sub_attachment_id = wp_insert_attachment( $attachment, $uploaded['file'] ); 148 $attach_data = wp_generate_attachment_metadata( $sub_attachment_id, $uploaded['file'] ); 149 wp_update_attachment_metadata( $sub_attachment_id, $attach_data ); 150 update_post_meta( $attachment_id, '_thumbnail_id', $sub_attachment_id ); 151 } 152 } 153 154 // remove the blob of binary data from the array 155 if ( isset( $metadata['image']['data'] ) ) 156 unset( $metadata['image']['data'] ); 157 158 return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id ); 159 } 160 161 /** 162 * Convert a fraction string to a decimal. 163 * 164 * @since 2.5.0 165 * 166 * @param string $str 167 * @return int|float 168 */ 169 function wp_exif_frac2dec($str) { 170 @list( $n, $d ) = explode( '/', $str ); 171 if ( !empty($d) ) 172 return $n / $d; 173 return $str; 174 } 175 176 /** 177 * Convert the exif date format to a unix timestamp. 178 * 179 * @since 2.5.0 180 * 181 * @param string $str 182 * @return int 183 */ 184 function wp_exif_date2ts($str) { 185 @list( $date, $time ) = explode( ' ', trim($str) ); 186 @list( $y, $m, $d ) = explode( ':', $date ); 187 188 return strtotime( "{$y}-{$m}-{$d} {$time}" ); 189 } 190 191 /** 192 * Get extended image metadata, exif or iptc as available. 193 * 194 * Retrieves the EXIF metadata aperture, credit, camera, caption, copyright, iso 195 * created_timestamp, focal_length, shutter_speed, and title. 196 * 197 * The IPTC metadata that is retrieved is APP13, credit, byline, created date 198 * and time, caption, copyright, and title. Also includes FNumber, Model, 199 * DateTimeDigitized, FocalLength, ISOSpeedRatings, and ExposureTime. 200 * 201 * @todo Try other exif libraries if available. 202 * @since 2.5.0 203 * 204 * @param string $file 205 * @return bool|array False on failure. Image metadata array on success. 206 */ 207 function wp_read_image_metadata( $file ) { 208 if ( ! file_exists( $file ) ) 209 return false; 210 211 list( , , $sourceImageType ) = getimagesize( $file ); 212 213 // exif contains a bunch of data we'll probably never need formatted in ways 214 // that are difficult to use. We'll normalize it and just extract the fields 215 // that are likely to be useful. Fractions and numbers are converted to 216 // floats, dates to unix timestamps, and everything else to strings. 217 $meta = array( 218 'aperture' => 0, 219 'credit' => '', 220 'camera' => '', 221 'caption' => '', 222 'created_timestamp' => 0, 223 'copyright' => '', 224 'focal_length' => 0, 225 'iso' => 0, 226 'shutter_speed' => 0, 227 'title' => '', 228 ); 229 230 // read iptc first, since it might contain data not available in exif such 231 // as caption, description etc 232 if ( is_callable( 'iptcparse' ) ) { 233 getimagesize( $file, $info ); 234 235 if ( ! empty( $info['APP13'] ) ) { 236 $iptc = iptcparse( $info['APP13'] ); 237 238 // headline, "A brief synopsis of the caption." 239 if ( ! empty( $iptc['2#105'][0] ) ) 240 $meta['title'] = trim( $iptc['2#105'][0] ); 241 // title, "Many use the Title field to store the filename of the image, though the field may be used in many ways." 242 elseif ( ! empty( $iptc['2#005'][0] ) ) 243 $meta['title'] = trim( $iptc['2#005'][0] ); 244 245 if ( ! empty( $iptc['2#120'][0] ) ) { // description / legacy caption 246 $caption = trim( $iptc['2#120'][0] ); 247 if ( empty( $meta['title'] ) ) { 248 // Assume the title is stored in 2:120 if it's short. 249 if ( strlen( $caption ) < 80 ) 250 $meta['title'] = $caption; 251 else 252 $meta['caption'] = $caption; 253 } elseif ( $caption != $meta['title'] ) { 254 $meta['caption'] = $caption; 255 } 256 } 257 258 if ( ! empty( $iptc['2#110'][0] ) ) // credit 259 $meta['credit'] = trim( $iptc['2#110'][0] ); 260 elseif ( ! empty( $iptc['2#080'][0] ) ) // creator / legacy byline 261 $meta['credit'] = trim( $iptc['2#080'][0] ); 262 263 if ( ! empty( $iptc['2#055'][0] ) and ! empty( $iptc['2#060'][0] ) ) // created date and time 264 $meta['created_timestamp'] = strtotime( $iptc['2#055'][0] . ' ' . $iptc['2#060'][0] ); 265 266 if ( ! empty( $iptc['2#116'][0] ) ) // copyright 267 $meta['copyright'] = trim( $iptc['2#116'][0] ); 268 } 269 } 270 271 // fetch additional info from exif if available 272 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 ) ) ) ) { 273 $exif = @exif_read_data( $file ); 274 275 if ( !empty( $exif['Title'] ) ) 276 $meta['title'] = trim( $exif['Title'] ); 277 278 if ( ! empty( $exif['ImageDescription'] ) ) { 279 if ( empty( $meta['title'] ) && strlen( $exif['ImageDescription'] ) < 80 ) { 280 // Assume the title is stored in ImageDescription 281 $meta['title'] = trim( $exif['ImageDescription'] ); 282 if ( ! empty( $exif['COMPUTED']['UserComment'] ) && trim( $exif['COMPUTED']['UserComment'] ) != $meta['title'] ) 283 $meta['caption'] = trim( $exif['COMPUTED']['UserComment'] ); 284 } elseif ( trim( $exif['ImageDescription'] ) != $meta['title'] ) { 285 $meta['caption'] = trim( $exif['ImageDescription'] ); 286 } 287 } elseif ( ! empty( $exif['Comments'] ) && trim( $exif['Comments'] ) != $meta['title'] ) { 288 $meta['caption'] = trim( $exif['Comments'] ); 289 } 290 291 if ( ! empty( $exif['Artist'] ) ) 292 $meta['credit'] = trim( $exif['Artist'] ); 293 elseif ( ! empty($exif['Author'] ) ) 294 $meta['credit'] = trim( $exif['Author'] ); 295 296 if ( ! empty( $exif['Copyright'] ) ) 297 $meta['copyright'] = trim( $exif['Copyright'] ); 298 if ( ! empty($exif['FNumber'] ) ) 299 $meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 ); 300 if ( ! empty($exif['Model'] ) ) 301 $meta['camera'] = trim( $exif['Model'] ); 302 if ( ! empty($exif['DateTimeDigitized'] ) ) 303 $meta['created_timestamp'] = wp_exif_date2ts($exif['DateTimeDigitized'] ); 304 if ( ! empty($exif['FocalLength'] ) ) 305 $meta['focal_length'] = (string) wp_exif_frac2dec( $exif['FocalLength'] ); 306 if ( ! empty($exif['ISOSpeedRatings'] ) ) { 307 $meta['iso'] = is_array( $exif['ISOSpeedRatings'] ) ? reset( $exif['ISOSpeedRatings'] ) : $exif['ISOSpeedRatings']; 308 $meta['iso'] = trim( $meta['iso'] ); 309 } 310 if ( ! empty($exif['ExposureTime'] ) ) 311 $meta['shutter_speed'] = (string) wp_exif_frac2dec( $exif['ExposureTime'] ); 312 } 313 314 foreach ( array( 'title', 'caption', 'credit', 'copyright', 'camera', 'iso' ) as $key ) { 315 if ( $meta[ $key ] && ! seems_utf8( $meta[ $key ] ) ) 316 $meta[ $key ] = utf8_encode( $meta[ $key ] ); 317 } 318 319 return apply_filters( 'wp_read_image_metadata', $meta, $file, $sourceImageType ); 320 321 } 322 323 /** 324 * Validate that file is an image. 325 * 326 * @since 2.5.0 327 * 328 * @param string $path File path to test if valid image. 329 * @return bool True if valid image, false if not valid image. 330 */ 331 function file_is_valid_image($path) { 332 $size = @getimagesize($path); 333 return !empty($size); 334 } 335 336 /** 337 * Validate that file is suitable for displaying within a web page. 338 * 339 * @since 2.5.0 340 * @uses apply_filters() Calls 'file_is_displayable_image' on $result and $path. 341 * 342 * @param string $path File path to test. 343 * @return bool True if suitable, false if not suitable. 344 */ 345 function file_is_displayable_image($path) { 346 $info = @getimagesize($path); 347 if ( empty($info) ) 348 $result = false; 349 elseif ( !in_array($info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG)) ) // only gif, jpeg and png images can reliably be displayed 350 $result = false; 351 else 352 $result = true; 353 354 return apply_filters('file_is_displayable_image', $result, $path); 355 } 356 357 /** 358 * Load an image resource for editing. 359 * 360 * @since 2.9.0 361 * 362 * @param string $attachment_id Attachment ID. 363 * @param string $mime_type Image mime type. 364 * @param string $size Optional. Image size, defaults to 'full'. 365 * @return resource|false The resulting image resource on success, false on failure. 366 */ 367 function load_image_to_edit( $attachment_id, $mime_type, $size = 'full' ) { 368 $filepath = _load_image_to_edit_path( $attachment_id, $size ); 369 if ( empty( $filepath ) ) 370 return false; 371 372 switch ( $mime_type ) { 373 case 'image/jpeg': 374 $image = imagecreatefromjpeg($filepath); 375 break; 376 case 'image/png': 377 $image = imagecreatefrompng($filepath); 378 break; 379 case 'image/gif': 380 $image = imagecreatefromgif($filepath); 381 break; 382 default: 383 $image = false; 384 break; 385 } 386 if ( is_resource($image) ) { 387 $image = apply_filters('load_image_to_edit', $image, $attachment_id, $size); 388 if ( function_exists('imagealphablending') && function_exists('imagesavealpha') ) { 389 imagealphablending($image, false); 390 imagesavealpha($image, true); 391 } 392 } 393 return $image; 394 } 395 396 /** 397 * Retrieve the path or url of an attachment's attached file. 398 * 399 * If the attached file is not present on the local filesystem (usually due to replication plugins), 400 * then the url of the file is returned if url fopen is supported. 401 * 402 * @since 3.4.0 403 * @access private 404 * 405 * @param string $attachment_id Attachment ID. 406 * @param string $size Optional. Image size, defaults to 'full'. 407 * @return string|false File path or url on success, false on failure. 408 */ 409 function _load_image_to_edit_path( $attachment_id, $size = 'full' ) { 410 $filepath = get_attached_file( $attachment_id ); 411 412 if ( $filepath && file_exists( $filepath ) ) { 413 if ( 'full' != $size && ( $data = image_get_intermediate_size( $attachment_id, $size ) ) ) { 414 $filepath = apply_filters( 'load_image_to_edit_filesystempath', path_join( dirname( $filepath ), $data['file'] ), $attachment_id, $size ); 415 } 416 } elseif ( function_exists( 'fopen' ) && function_exists( 'ini_get' ) && true == ini_get( 'allow_url_fopen' ) ) { 417 $filepath = apply_filters( 'load_image_to_edit_attachmenturl', wp_get_attachment_url( $attachment_id ), $attachment_id, $size ); 418 } 419 420 return apply_filters( 'load_image_to_edit_path', $filepath, $attachment_id, $size ); 421 } 422 423 /** 424 * Copy an existing image file. 425 * 426 * @since 3.4.0 427 * @access private 428 * 429 * @param string $attachment_id Attachment ID. 430 * @return string|false New file path on success, false on failure. 431 */ 432 function _copy_image_file( $attachment_id ) { 433 $dst_file = $src_file = get_attached_file( $attachment_id ); 434 if ( ! file_exists( $src_file ) ) 435 $src_file = _load_image_to_edit_path( $attachment_id ); 436 437 if ( $src_file ) { 438 $dst_file = str_replace( basename( $dst_file ), 'copy-' . basename( $dst_file ), $dst_file ); 439 $dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) ); 440 441 // The directory containing the original file may no longer exist when 442 // using a replication plugin. 443 wp_mkdir_p( dirname( $dst_file ) ); 444 445 if ( ! @copy( $src_file, $dst_file ) ) 446 $dst_file = false; 447 } else { 448 $dst_file = false; 449 } 450 451 return $dst_file; 452 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Mar 25 01:41:18 2014 | WordPress honlapkészítés: online1.hu |