Changeset 42343 for trunk/src/wp-admin/includes/image.php
- Timestamp:
- 11/30/2017 11:09:33 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/image.php
r42228 r42343 38 38 39 39 $editor = wp_get_image_editor( $src ); 40 if ( is_wp_error( $editor ) ) 40 if ( is_wp_error( $editor ) ) { 41 41 return $editor; 42 } 42 43 43 44 $src = $editor->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs ); 44 if ( is_wp_error( $src ) ) 45 if ( is_wp_error( $src ) ) { 45 46 return $src; 46 47 if ( ! $dst_file ) 47 } 48 49 if ( ! $dst_file ) { 48 50 $dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file ); 51 } 49 52 50 53 /* … … 57 60 58 61 $result = $editor->save( $dst_file ); 59 if ( is_wp_error( $result ) ) 62 if ( is_wp_error( $result ) ) { 60 63 return $result; 64 } 61 65 62 66 return $dst_file; … … 75 79 $attachment = get_post( $attachment_id ); 76 80 77 $metadata = array();78 $support = false;81 $metadata = array(); 82 $support = false; 79 83 $mime_type = get_post_mime_type( $attachment ); 80 84 81 85 if ( preg_match( '!^image/!', $mime_type ) && file_is_displayable_image( $file ) ) { 82 $imagesize = getimagesize( $file );83 $metadata['width'] = $imagesize[0];86 $imagesize = getimagesize( $file ); 87 $metadata['width'] = $imagesize[0]; 84 88 $metadata['height'] = $imagesize[1]; 85 89 86 90 // Make the file path relative to the upload dir. 87 $metadata['file'] = _wp_relative_upload_path( $file);91 $metadata['file'] = _wp_relative_upload_path( $file ); 88 92 89 93 // Make thumbnails and other intermediate sizes. … … 92 96 $sizes = array(); 93 97 foreach ( get_intermediate_image_sizes() as $s ) { 94 $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => false ); 95 if ( isset( $_wp_additional_image_sizes[$s]['width'] ) ) { 98 $sizes[ $s ] = array( 99 'width' => '', 100 'height' => '', 101 'crop' => false, 102 ); 103 if ( isset( $_wp_additional_image_sizes[ $s ]['width'] ) ) { 96 104 // For theme-added sizes 97 $sizes[ $s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] );105 $sizes[ $s ]['width'] = intval( $_wp_additional_image_sizes[ $s ]['width'] ); 98 106 } else { 99 107 // For default sizes set in options 100 $sizes[ $s]['width'] = get_option( "{$s}_size_w" );101 } 102 103 if ( isset( $_wp_additional_image_sizes[ $s]['height'] ) ) {108 $sizes[ $s ]['width'] = get_option( "{$s}_size_w" ); 109 } 110 111 if ( isset( $_wp_additional_image_sizes[ $s ]['height'] ) ) { 104 112 // For theme-added sizes 105 $sizes[ $s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] );113 $sizes[ $s ]['height'] = intval( $_wp_additional_image_sizes[ $s ]['height'] ); 106 114 } else { 107 115 // For default sizes set in options 108 $sizes[ $s]['height'] = get_option( "{$s}_size_h" );109 } 110 111 if ( isset( $_wp_additional_image_sizes[ $s]['crop'] ) ) {116 $sizes[ $s ]['height'] = get_option( "{$s}_size_h" ); 117 } 118 119 if ( isset( $_wp_additional_image_sizes[ $s ]['crop'] ) ) { 112 120 // For theme-added sizes 113 $sizes[ $s]['crop'] = $_wp_additional_image_sizes[$s]['crop'];121 $sizes[ $s ]['crop'] = $_wp_additional_image_sizes[ $s ]['crop']; 114 122 } else { 115 123 // For default sizes set in options 116 $sizes[ $s]['crop'] = get_option( "{$s}_crop" );124 $sizes[ $s ]['crop'] = get_option( "{$s}_crop" ); 117 125 } 118 126 } … … 132 140 $editor = wp_get_image_editor( $file ); 133 141 134 if ( ! is_wp_error( $editor ) ) 142 if ( ! is_wp_error( $editor ) ) { 135 143 $metadata['sizes'] = $editor->multi_resize( $sizes ); 144 } 136 145 } else { 137 146 $metadata['sizes'] = array(); … … 140 149 // Fetch additional metadata from EXIF/IPTC. 141 150 $image_meta = wp_read_image_metadata( $file ); 142 if ( $image_meta ) 151 if ( $image_meta ) { 143 152 $metadata['image_meta'] = $image_meta; 144 153 } 145 154 } elseif ( wp_attachment_is( 'video', $attachment ) ) { 146 155 $metadata = wp_read_video_metadata( $file ); 147 $support = current_theme_supports( 'post-thumbnails', 'attachment:video' ) || post_type_supports( 'attachment:video', 'thumbnail' );156 $support = current_theme_supports( 'post-thumbnails', 'attachment:video' ) || post_type_supports( 'attachment:video', 'thumbnail' ); 148 157 } elseif ( wp_attachment_is( 'audio', $attachment ) ) { 149 158 $metadata = wp_read_audio_metadata( $file ); 150 $support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) || post_type_supports( 'attachment:audio', 'thumbnail' );159 $support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) || post_type_supports( 'attachment:audio', 'thumbnail' ); 151 160 } 152 161 153 162 if ( $support && ! empty( $metadata['image']['data'] ) ) { 154 163 // Check for existing cover. 155 $hash = md5( $metadata['image']['data'] ); 156 $posts = get_posts( array( 157 'fields' => 'ids', 158 'post_type' => 'attachment', 159 'post_mime_type' => $metadata['image']['mime'], 160 'post_status' => 'inherit', 161 'posts_per_page' => 1, 162 'meta_key' => '_cover_hash', 163 'meta_value' => $hash 164 ) ); 164 $hash = md5( $metadata['image']['data'] ); 165 $posts = get_posts( 166 array( 167 'fields' => 'ids', 168 'post_type' => 'attachment', 169 'post_mime_type' => $metadata['image']['mime'], 170 'post_status' => 'inherit', 171 'posts_per_page' => 1, 172 'meta_key' => '_cover_hash', 173 'meta_value' => $hash, 174 ) 175 ); 165 176 $exists = reset( $posts ); 166 177 … … 170 181 $ext = '.jpg'; 171 182 switch ( $metadata['image']['mime'] ) { 172 case 'image/gif':173 $ext = '.gif';174 break;175 case 'image/png':176 $ext = '.png';177 break;183 case 'image/gif': 184 $ext = '.gif'; 185 break; 186 case 'image/png': 187 $ext = '.png'; 188 break; 178 189 } 179 190 $basename = str_replace( '.', '-', basename( $file ) ) . '-image' . $ext; … … 182 193 $image_attachment = array( 183 194 'post_mime_type' => $metadata['image']['mime'], 184 'post_type' => 'attachment',185 'post_content' => '',195 'post_type' => 'attachment', 196 'post_content' => '', 186 197 ); 187 198 /** … … 203 214 } 204 215 } 205 } 206 // Try to create image thumbnails for PDFs 207 else if ( 'application/pdf' === $mime_type ) { 216 } // Try to create image thumbnails for PDFs 217 elseif ( 'application/pdf' === $mime_type ) { 208 218 $fallback_sizes = array( 209 219 'thumbnail', … … 222 232 $fallback_sizes = apply_filters( 'fallback_intermediate_image_sizes', $fallback_sizes, $metadata ); 223 233 224 $sizes = array();234 $sizes = array(); 225 235 $_wp_additional_image_sizes = wp_get_additional_image_sizes(); 226 236 … … 257 267 * Ensure the PDF preview image does not overwrite any JPEG images that already exist. 258 268 */ 259 $dirname = dirname( $file ) . '/';260 $ext = '.' . pathinfo( $file, PATHINFO_EXTENSION );269 $dirname = dirname( $file ) . '/'; 270 $ext = '.' . pathinfo( $file, PATHINFO_EXTENSION ); 261 271 $preview_file = $dirname . wp_unique_filename( $dirname, wp_basename( $file, $ext ) . '-pdf.jpg' ); 262 272 … … 270 280 271 281 if ( ! is_wp_error( $editor ) ) { 272 $metadata['sizes'] = $editor->multi_resize( $sizes );282 $metadata['sizes'] = $editor->multi_resize( $sizes ); 273 283 $metadata['sizes']['full'] = $uploaded; 274 284 } … … 302 312 * @return int|float 303 313 */ 304 function wp_exif_frac2dec( $str) {314 function wp_exif_frac2dec( $str ) { 305 315 @list( $n, $d ) = explode( '/', $str ); 306 if ( ! empty($d) )316 if ( ! empty( $d ) ) { 307 317 return $n / $d; 318 } 308 319 return $str; 309 320 } … … 317 328 * @return int 318 329 */ 319 function wp_exif_date2ts( $str) {320 @list( $date, $time ) = explode( ' ', trim( $str) );321 @list( $y, $m, $d ) = explode( ':', $date );330 function wp_exif_date2ts( $str ) { 331 @list( $date, $time ) = explode( ' ', trim( $str ) ); 332 @list( $y, $m, $d ) = explode( ':', $date ); 322 333 323 334 return strtotime( "{$y}-{$m}-{$d} {$time}" ); … … 341 352 */ 342 353 function wp_read_image_metadata( $file ) { 343 if ( ! file_exists( $file ) ) 354 if ( ! file_exists( $file ) ) { 344 355 return false; 356 } 345 357 346 358 list( , , $sourceImageType ) = getimagesize( $file ); … … 353 365 */ 354 366 $meta = array( 355 'aperture' => 0,356 'credit' => '',357 'camera' => '',358 'caption' => '',367 'aperture' => 0, 368 'credit' => '', 369 'camera' => '', 370 'caption' => '', 359 371 'created_timestamp' => 0, 360 'copyright' => '',361 'focal_length' => 0,362 'iso' => 0,363 'shutter_speed' => 0,364 'title' => '',365 'orientation' => 0,366 'keywords' => array(),372 'copyright' => '', 373 'focal_length' => 0, 374 'iso' => 0, 375 'shutter_speed' => 0, 376 'title' => '', 377 'orientation' => 0, 378 'keywords' => array(), 367 379 ); 368 380 … … 381 393 if ( ! empty( $iptc['2#105'][0] ) ) { 382 394 $meta['title'] = trim( $iptc['2#105'][0] ); 383 /*384 385 386 395 /* 396 * Title, "Many use the Title field to store the filename of the image, 397 * though the field may be used in many ways." 398 */ 387 399 } elseif ( ! empty( $iptc['2#005'][0] ) ) { 388 400 $meta['title'] = trim( $iptc['2#005'][0] ); … … 404 416 } 405 417 406 if ( ! empty( $iptc['2#110'][0] ) ) // credit418 if ( ! empty( $iptc['2#110'][0] ) ) { // credit 407 419 $meta['credit'] = trim( $iptc['2#110'][0] ); 408 elseif ( ! empty( $iptc['2#080'][0] ) )// creator / legacy byline420 } elseif ( ! empty( $iptc['2#080'][0] ) ) { // creator / legacy byline 409 421 $meta['credit'] = trim( $iptc['2#080'][0] ); 410 411 if ( ! empty( $iptc['2#055'][0] ) && ! empty( $iptc['2#060'][0] ) ) // created date and time 422 } 423 424 if ( ! empty( $iptc['2#055'][0] ) && ! empty( $iptc['2#060'][0] ) ) { // created date and time 412 425 $meta['created_timestamp'] = strtotime( $iptc['2#055'][0] . ' ' . $iptc['2#060'][0] ); 413 414 if ( ! empty( $iptc['2#116'][0] ) ) // copyright 426 } 427 428 if ( ! empty( $iptc['2#116'][0] ) ) { // copyright 415 429 $meta['copyright'] = trim( $iptc['2#116'][0] ); 430 } 416 431 417 432 if ( ! empty( $iptc['2#025'][0] ) ) { // keywords array … … 455 470 if ( ! empty( $exif['Artist'] ) ) { 456 471 $meta['credit'] = trim( $exif['Artist'] ); 457 } elseif ( ! empty( $exif['Author'] ) ) {472 } elseif ( ! empty( $exif['Author'] ) ) { 458 473 $meta['credit'] = trim( $exif['Author'] ); 459 474 } … … 524 539 * @return bool True if valid image, false if not valid image. 525 540 */ 526 function file_is_valid_image( $path) {527 $size = @getimagesize( $path);528 return ! empty($size);541 function file_is_valid_image( $path ) { 542 $size = @getimagesize( $path ); 543 return ! empty( $size ); 529 544 } 530 545 … … 537 552 * @return bool True if suitable, false if not suitable. 538 553 */ 539 function file_is_displayable_image( $path) {554 function file_is_displayable_image( $path ) { 540 555 $displayable_image_types = array( IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP ); 541 556 … … 572 587 function load_image_to_edit( $attachment_id, $mime_type, $size = 'full' ) { 573 588 $filepath = _load_image_to_edit_path( $attachment_id, $size ); 574 if ( empty( $filepath ) ) 589 if ( empty( $filepath ) ) { 575 590 return false; 591 } 576 592 577 593 switch ( $mime_type ) { 578 594 case 'image/jpeg': 579 $image = imagecreatefromjpeg( $filepath);595 $image = imagecreatefromjpeg( $filepath ); 580 596 break; 581 597 case 'image/png': 582 $image = imagecreatefrompng( $filepath);598 $image = imagecreatefrompng( $filepath ); 583 599 break; 584 600 case 'image/gif': 585 $image = imagecreatefromgif( $filepath);601 $image = imagecreatefromgif( $filepath ); 586 602 break; 587 603 default: … … 589 605 break; 590 606 } 591 if ( is_resource( $image) ) {607 if ( is_resource( $image ) ) { 592 608 /** 593 609 * Filters the current image being loaded for editing. … … 600 616 */ 601 617 $image = apply_filters( 'load_image_to_edit', $image, $attachment_id, $size ); 602 if ( function_exists( 'imagealphablending') && function_exists('imagesavealpha') ) {603 imagealphablending( $image, false);604 imagesavealpha( $image, true);618 if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) { 619 imagealphablending( $image, false ); 620 imagesavealpha( $image, true ); 605 621 } 606 622 } … … 677 693 function _copy_image_file( $attachment_id ) { 678 694 $dst_file = $src_file = get_attached_file( $attachment_id ); 679 if ( ! file_exists( $src_file ) ) 695 if ( ! file_exists( $src_file ) ) { 680 696 $src_file = _load_image_to_edit_path( $attachment_id ); 697 } 681 698 682 699 if ( $src_file ) { … … 690 707 wp_mkdir_p( dirname( $dst_file ) ); 691 708 692 if ( ! @copy( $src_file, $dst_file ) ) 709 if ( ! @copy( $src_file, $dst_file ) ) { 693 710 $dst_file = false; 711 } 694 712 } else { 695 713 $dst_file = false;
Note: See TracChangeset
for help on using the changeset viewer.