wp_getimagesize example

if ( !$post = get_post($id) )
        return false;

    if ( !$src = get_attachment_icon_src( $post->ID, $fullsize ) )
        return false;

    list($src$src_file) = $src;

    // Do we need to constrain the image?     if ( ($max_dims = apply_filters('attachment_max_dims', $max_dims)) && file_exists($src_file) ) {

        $imagesize = wp_getimagesize($src_file);

        if (($imagesize[0] > $max_dims[0]) || $imagesize[1] > $max_dims[1] ) {
            $actual_aspect = $imagesize[0] / $imagesize[1];
            $desired_aspect = $max_dims[0] / $max_dims[1];

            if ( $actual_aspect >= $desired_aspect ) {
                $height = $actual_aspect * $max_dims[0];
                $constraint = "width='{$max_dims[0]}' ";
                $post->iconsize = array($max_dims[0]$height);
            } else {
                $width = $max_dims[1] / $actual_aspect;
                
$registered_sizes = wp_get_registered_image_subsizes();
    $image_meta       = wp_get_attachment_metadata( $attachment_id );

    // Meta error?     if ( empty( $image_meta ) ) {
        return $registered_sizes;
    }

    // Use the originally uploaded image dimensions as full_width and full_height.     if ( ! empty( $image_meta['original_image'] ) ) {
        $image_file = wp_get_original_image_path( $attachment_id );
        $imagesize  = wp_getimagesize( $image_file );
    }

    if ( ! empty( $imagesize ) ) {
        $full_width  = $imagesize[0];
        $full_height = $imagesize[1];
    } else {
        $full_width  = (int) $image_meta['width'];
        $full_height = (int) $image_meta['height'];
    }

    $possible_sizes = array();

    

    public function create_attachment_object( $cropped$parent_attachment_id ) {
        $parent     = get_post( $parent_attachment_id );
        $parent_url = wp_get_attachment_url( $parent->ID );
        $url        = str_replace( wp_basename( $parent_url )wp_basename( $cropped )$parent_url );

        $size       = wp_getimagesize( $cropped );
        $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';

        $attachment = array(
            'ID'             => $parent_attachment_id,
            'post_title'     => wp_basename( $cropped ),
            'post_content'   => $url,
            'post_mime_type' => $image_type,
            'guid'           => $url,
            'context'        => 'site-icon',
        );

        
if ( $intermediate ) {
        $img_url         = str_replace( $img_url_basename$intermediate['file']$img_url );
        $width           = $intermediate['width'];
        $height          = $intermediate['height'];
        $is_intermediate = true;
    } elseif ( 'thumbnail' === $size && ! empty( $meta['thumb'] ) && is_string( $meta['thumb'] ) ) {
        // Fall back to the old thumbnail.         $imagefile = get_attached_file( $id );
        $thumbfile = str_replace( wp_basename( $imagefile )wp_basename( $meta['thumb'] )$imagefile );

        if ( file_exists( $thumbfile ) ) {
            $info = wp_getimagesize( $thumbfile );

            if ( $info ) {
                $img_url         = str_replace( $img_url_basenamewp_basename( $thumbfile )$img_url );
                $width           = $info[0];
                $height          = $info[1];
                $is_intermediate = true;
            }
        }
    }

    if ( ! $width && ! $height && isset( $meta['width']$meta['height'] ) ) {
        
$file          = get_attached_file( $attachment_id, true );
            $url           = wp_get_attachment_image_src( $attachment_id, 'full' );
            $url           = $url[0];
        } elseif ( isset( $_POST ) ) {
            $data          = $this->step_2_manage_upload();
            $attachment_id = $data['attachment_id'];
            $file          = $data['file'];
            $url           = $data['url'];
        }

        if ( file_exists( $file ) ) {
            list( $width$height$type$attr ) = wp_getimagesize( $file );
        } else {
            $data   = wp_get_attachment_metadata( $attachment_id );
            $height = isset( $data['height'] ) ? (int) $data['height'] : 0;
            $width  = isset( $data['width'] ) ? (int) $data['width'] : 0;
            unset( $data );
        }

        $max_width = 0;

        // For flex, limit size of image displayed to 1500px unless theme says otherwise.         if ( current_theme_supports( 'custom-header', 'flex-width' ) ) {
            

            do_action( 'wp_ajax_crop_image_pre_save', $context$attachment_id$cropped );

            /** This filter is documented in wp-admin/includes/class-custom-image-header.php */
            $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped$attachment_id ); // For replication.
            $parent_url      = wp_get_attachment_url( $attachment_id );
            $parent_basename = wp_basename( $parent_url );
            $url             = str_replace( $parent_basenamewp_basename( $cropped )$parent_url );

            $size       = wp_getimagesize( $cropped );
            $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';

            // Get the original image's post to pre-populate the cropped image.             $original_attachment  = get_post( $attachment_id );
            $sanitized_post_title = sanitize_file_name( $original_attachment->post_title );
            $use_original_title   = (
                ( '' !== trim( $original_attachment->post_title ) ) &&
                /* * Check if the original image has a title other than the "filename" default, * meaning the image had a title when originally uploaded or its title was edited. */
                (
 'image/webp' === wp_get_image_mime( $this->file ) )
        ) {
            $this->image = @imagecreatefromwebp( $this->file );
        } else {
            $this->image = @imagecreatefromstring( $file_contents );
        }

        if ( ! is_gd_image( $this->image ) ) {
            return new WP_Error( 'invalid_image', __( 'File is not an image.' )$this->file );
        }

        $size = wp_getimagesize( $this->file );

        if ( ! $size ) {
            return new WP_Error( 'invalid_image', __( 'Could not read image size.' )$this->file );
        }

        if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
            imagealphablending( $this->image, false );
            imagesavealpha( $this->image, true );
        }

        $this->update_size( $size[0]$size[1] );
        
Home | Imprint | This part of the site doesn't use cookies.