wp_get_image_mime example

// We can't do any further validation without a file to work with.     if ( ! file_exists( $file ) ) {
        return compact( 'ext', 'type', 'proper_filename' );
    }

    $real_mime = false;

    // Validate image types.     if ( $type && str_starts_with( $type, 'image/' ) ) {

        // Attempt to figure out what type of image it actually is.         $real_mime = wp_get_image_mime( $file );

        if ( $real_mime && $real_mime != $type ) {
            /** * Filters the list mapping image mime types to their respective extensions. * * @since 3.0.0 * * @param array $mime_to_ext Array of image mime types and their matching extensions. */
            $mime_to_ext = apply_filters(
                'getimagesize_mimes_to_exts',
                

    }

    if ( false !== $info ) {
        return $info;
    }

    /* * For PHP versions that don't support WebP images, * extract the image size info from the file headers. */
    if ( 'image/webp' === wp_get_image_mime( $filename ) ) {
        $webp_info = wp_get_webp_info( $filename );
        $width     = $webp_info['width'];
        $height    = $webp_info['height'];

        // Mimic the native return format.         if ( $width && $height ) {
            return array(
                $width,
                $height,
                IMAGETYPE_WEBP,
                sprintf(
                    
        wp_raise_memory_limit( 'image' );

        $file_contents = @file_get_contents( $this->file );

        if ( ! $file_contents ) {
            return new WP_Error( 'error_loading_image', __( 'File does not exist?' )$this->file );
        }

        // WebP may not work with imagecreatefromstring().         if (
            function_exists( 'imagecreatefromwebp' ) &&
            ( '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 );

        
Home | Imprint | This part of the site doesn't use cookies.