Make WordPress Core


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/image.php

    r18632 r21039  
    3333 * @since 2.1.0
    3434 *
    35  * @param string|int $src_file The source file or Attachment ID.
     35 * @param string|int $src The source file or Attachment ID.
    3636 * @param int $src_x The start x position to crop from.
    3737 * @param int $src_y The start y position to crop from.
    … …  
    4444 * @return string|WP_Error|false New filepath on success, WP_Error or false on failure.
    4545 */
    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 ) )
     46function wp_crop_image( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {
     47        if ( is_numeric( $src ) ) { // Handle int as attachment ID
     48                $src_file = get_attached_file( $src );
     49                if ( ! file_exists( $src_file ) ) {
     50                        // If the file doesn't exist, attempt a url fopen on the src link.
     51                        // This can occur with certain file replication plugins.
     52                        $post = get_post( $src );
     53                        $image_type = $post->post_mime_type;
     54                        $src = load_image_to_edit( $src, $post->post_mime_type, 'full' );
     55                } else {
     56                        $size = @getimagesize( $src_file );
     57                        $image_type = ( $size ) ? $size['mime'] : '';
     58                        $src = wp_load_image( $src_file );
     59                }
     60        } else {
     61                $size = @getimagesize( $src );
     62                $image_type = ( $size ) ? $size['mime'] : '';
     63                $src = wp_load_image( $src );
     64        }
     65
     66        if ( ! is_resource( $src ) )
    5367                return new WP_Error( 'error_loading_image', $src, $src_file );
    5468
    … …  
    6074        }
    6175
    62         if (function_exists('imageantialias'))
     76        if ( function_exists( 'imageantialias' ) )
    6377                imageantialias( $dst, true );
    6478
    … …  
    7084                $dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file );
    7185
    72         $dst_file = preg_replace( '/\\.[^\\.]+$/', '.jpg', $dst_file );
    73 
    74         if ( imagejpeg( $dst, $dst_file, apply_filters( 'jpeg_quality', 90, 'wp_crop_image' ) ) )
     86        if ( 'image/png' != $image_type )
     87                $dst_file = preg_replace( '/\\.[^\\.]+$/', '.jpg', $dst_file );
     88
     89        // The directory containing the original file may no longer exist when
     90        // using a replication plugin.
     91        wp_mkdir_p( dirname( $dst_file ) );
     92
     93        $dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) );
     94
     95        if ( 'image/png' == $image_type && imagepng( $dst, $dst_file ) )
     96                return $dst_file;
     97        elseif ( imagejpeg( $dst, $dst_file, apply_filters( 'jpeg_quality', 90, 'wp_crop_image' ) ) )
    7598                return $dst_file;
    7699        else
    … …  
    105128
    106129                foreach ( get_intermediate_image_sizes() as $s ) {
    107                         $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => FALSE );
     130                        $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => false );
    108131                        if ( isset( $_wp_additional_image_sizes[$s]['width'] ) )
    109132                                $sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); // For theme-added sizes
    … …  
    206229        // exif contains a bunch of data we'll probably never need formatted in ways
    207230        // 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
     231        // that are likely to be useful. Fractions and numbers are converted to
    209232        // floats, dates to unix timestamps, and everything else to strings.
    210233        $meta = array(
    … …  
    297320                if ( ! empty($exif['FocalLength'] ) )
    298321                        $meta['focal_length'] = wp_exif_frac2dec( $exif['FocalLength'] );
    299                 if ( ! empty($exif['ISOSpeedRatings'] ) )
    300                         $meta['iso'] = utf8_encode( trim( $exif['ISOSpeedRatings'] ) );
     322                if ( ! empty($exif['ISOSpeedRatings'] ) ) {
     323                        $meta['iso'] = is_array( $exif['ISOSpeedRatings'] ) ? reset( $exif['ISOSpeedRatings'] ) : $exif['ISOSpeedRatings'];
     324                        $meta['iso'] = utf8_encode( trim( $meta['iso'] ) );
     325                }
    301326                if ( ! empty($exif['ExposureTime'] ) )
    302327                        $meta['shutter_speed'] = wp_exif_frac2dec( $exif['ExposureTime'] );
    … …  
    340365        return apply_filters('file_is_displayable_image', $result, $path);
    341366}
     367
     368/**
     369 * Load an image resource for editing.
     370 *
     371 * @since 2.9.0
     372 *
     373 * @param string $attachment_id Attachment ID.
     374 * @param string $mime_type Image mime type.
     375 * @param string $size Optional. Image size, defaults to 'full'.
     376 * @return resource|false The resulting image resource on success, false on failure.
     377 */
     378function load_image_to_edit( $attachment_id, $mime_type, $size = 'full' ) {
     379        $filepath = _load_image_to_edit_path( $attachment_id, $size );
     380        if ( empty( $filepath ) )
     381                return false;
     382
     383        switch ( $mime_type ) {
     384                case 'image/jpeg':
     385                        $image = imagecreatefromjpeg($filepath);
     386                        break;
     387                case 'image/png':
     388                        $image = imagecreatefrompng($filepath);
     389                        break;
     390                case 'image/gif':
     391                        $image = imagecreatefromgif($filepath);
     392                        break;
     393                default:
     394                        $image = false;
     395                        break;
     396        }
     397        if ( is_resource($image) ) {
     398                $image = apply_filters('load_image_to_edit', $image, $attachment_id, $size);
     399                if ( function_exists('imagealphablending') && function_exists('imagesavealpha') ) {
     400                        imagealphablending($image, false);
     401                        imagesavealpha($image, true);
     402                }
     403        }
     404        return $image;
     405}
     406
     407/**
     408 * Retrieve the path or url of an attachment's attached file.
     409 *
     410 * If the attached file is not present on the local filesystem (usually due to replication plugins),
     411 * then the url of the file is returned if url fopen is supported.
     412 *
     413 * @since 3.4.0
     414 * @access private
     415 *
     416 * @param string $attachment_id Attachment ID.
     417 * @param string $size Optional. Image size, defaults to 'full'.
     418 * @return string|false File path or url on success, false on failure.
     419 */
     420function _load_image_to_edit_path( $attachment_id, $size = 'full' ) {
     421        $filepath = get_attached_file( $attachment_id );
     422
     423        if ( $filepath && file_exists( $filepath ) ) {
     424                if ( 'full' != $size && ( $data = image_get_intermediate_size( $attachment_id, $size ) ) ) {
     425                        $filepath = apply_filters( 'load_image_to_edit_filesystempath', path_join( dirname( $filepath ), $data['file'] ), $attachment_id, $size );
     426                }
     427        } elseif ( function_exists( 'fopen' ) && function_exists( 'ini_get' ) && true == ini_get( 'allow_url_fopen' ) ) {
     428                $filepath = apply_filters( 'load_image_to_edit_attachmenturl', wp_get_attachment_url( $attachment_id ), $attachment_id, $size );
     429        }
     430
     431        return apply_filters( 'load_image_to_edit_path', $filepath, $attachment_id, $size );
     432}
     433
     434/**
     435 * Copy an existing image file.
     436 *
     437 * @since 3.4.0
     438 * @access private
     439 *
     440 * @param string $attachment_id Attachment ID.
     441 * @return string|false New file path on success, false on failure.
     442 */
     443function _copy_image_file( $attachment_id ) {
     444        $dst_file = $src_file = get_attached_file( $attachment_id );
     445        if ( ! file_exists( $src_file ) )
     446                $src_file = _load_image_to_edit_path( $attachment_id );
     447
     448        if ( $src_file ) {
     449                $dst_file = str_replace( basename( $dst_file ), 'copy-' . basename( $dst_file ), $dst_file );
     450                $dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) );
     451
     452                // The directory containing the original file may no longer exist when
     453                // using a replication plugin.
     454                wp_mkdir_p( dirname( $dst_file ) );
     455
     456                if ( ! @copy( $src_file, $dst_file ) )
     457                        $dst_file = false;
     458        } else {
     459                $dst_file = false;
     460        }
     461
     462        return $dst_file;
     463}
Note: See TracChangeset for help on using the changeset viewer.