Make WordPress Core


Ignore:
Timestamp:
12/12/2006 08:36:16 AM (20 years ago)
Author:
ryan
Message:

Move wp_generate_attachment_metadata() and friends to admin-functions.php and include admin-functions from xmlrpc.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/admin-functions.php

    r4637 r4643  
    20462046
    20472047        if ( !file_exists( $file ) )
    2048                 return "File '$file' doesn't exist?";
     2048                return sprintf(__("File '%s' doesn't exist?"), $file);
     2049
     2050        if ( ! function_exists('imagecreatefromstring') )
     2051                return __('The GD image library is not installed.');
    20492052
    20502053        $contents = file_get_contents( $file );
    … …  
    20532056
    20542057        if ( !is_resource( $image ) )
    2055                 return "File '$file' is not image?";
     2058                return sprintf(__("File '%s' is not an image."), $file);
    20562059
    20572060        return $image;
    20582061}
    20592062
     2063function wp_generate_attachment_metadata( $attachment_id, $file ) {
     2064        $attachment = get_post( $attachment_id );
     2065
     2066        $metadata = array();
     2067        if ( preg_match('!^image/!', get_post_mime_type( $attachment )) ) {
     2068                $imagesize = getimagesize($file);
     2069                $metadata['width'] = $imagesize['0'];
     2070                $metadata['height'] = $imagesize['1'];
     2071                list($uwidth, $uheight) = get_udims($metadata['width'], $metadata['height']);
     2072                $metadata['hwstring_small'] = "height='$uheight' width='$uwidth'";
     2073                $metadata['file'] = $file;
     2074
     2075                if ( $metadata['width'] * $metadata['height'] < 3 * 1024 * 1024 ) {
     2076                        if ( $metadata['width'] > 128 && $metadata['width'] >= $metadata['height'] * 4 / 3 )
     2077                                $thumb = wp_create_thumbnail($file, 128);
     2078                        elseif ( $metadata['height'] > 96 )
     2079                                $thumb = wp_create_thumbnail($file, 96);
     2080
     2081                        if ( @file_exists($thumb) )
     2082                                $metadata['thumb'] = basename($thumb);
     2083                }
     2084        }
     2085        return apply_filters( 'wp_generate_attachment_metadata', $metadata );
     2086}
     2087
     2088function wp_create_thumbnail( $file, $max_side, $effect = '' ) {
     2089
     2090                // 1 = GIF, 2 = JPEG, 3 = PNG
     2091
     2092        if ( file_exists( $file ) ) {
     2093                $type = getimagesize( $file );
     2094
     2095                // if the associated function doesn't exist - then it's not
     2096                // handle. duh. i hope.
     2097
     2098                if (!function_exists( 'imagegif' ) && $type[2] == 1 ) {
     2099                        $error = __( 'Filetype not supported. Thumbnail not created.' );
     2100                }
     2101                elseif (!function_exists( 'imagejpeg' ) && $type[2] == 2 ) {
     2102                        $error = __( 'Filetype not supported. Thumbnail not created.' );
     2103                }
     2104                elseif (!function_exists( 'imagepng' ) && $type[2] == 3 ) {
     2105                        $error = __( 'Filetype not supported. Thumbnail not created.' );
     2106                } else {
     2107
     2108                        // create the initial copy from the original file
     2109                        if ( $type[2] == 1 ) {
     2110                                $image = imagecreatefromgif( $file );
     2111                        }
     2112                        elseif ( $type[2] == 2 ) {
     2113                                $image = imagecreatefromjpeg( $file );
     2114                        }
     2115                        elseif ( $type[2] == 3 ) {
     2116                                $image = imagecreatefrompng( $file );
     2117                        }
     2118
     2119                        if ( function_exists( 'imageantialias' ))
     2120                                imageantialias( $image, TRUE );
     2121
     2122                        $image_attr = getimagesize( $file );
     2123
     2124                        // figure out the longest side
     2125
     2126                        if ( $image_attr[0] > $image_attr[1] ) {
     2127                                $image_width = $image_attr[0];
     2128                                $image_height = $image_attr[1];
     2129                                $image_new_width = $max_side;
     2130
     2131                                $image_ratio = $image_width / $image_new_width;
     2132                                $image_new_height = $image_height / $image_ratio;
     2133                                //width is > height
     2134                        } else {
     2135                                $image_width = $image_attr[0];
     2136                                $image_height = $image_attr[1];
     2137                                $image_new_height = $max_side;
     2138
     2139                                $image_ratio = $image_height / $image_new_height;
     2140                                $image_new_width = $image_width / $image_ratio;
     2141                                //height > width
     2142                        }
     2143
     2144                        $thumbnail = imagecreatetruecolor( $image_new_width, $image_new_height);
     2145                        @ imagecopyresampled( $thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1] );
     2146
     2147                        // If no filters change the filename, we'll do a default transformation.
     2148                        if ( basename( $file ) == $thumb = apply_filters( 'thumbnail_filename', basename( $file ) ) )
     2149                                $thumb = preg_replace( '!(\.[^.]+)?$!', __( '.thumbnail' ).'$1', basename( $file ), 1 );
     2150
     2151                        $thumbpath = str_replace( basename( $file ), $thumb, $file );
     2152
     2153                        // move the thumbnail to it's final destination
     2154                        if ( $type[2] == 1 ) {
     2155                                if (!imagegif( $thumbnail, $thumbpath ) ) {
     2156                                        $error = __( "Thumbnail path invalid" );
     2157                                }
     2158                        }
     2159                        elseif ( $type[2] == 2 ) {
     2160                                if (!imagejpeg( $thumbnail, $thumbpath ) ) {
     2161                                        $error = __( "Thumbnail path invalid" );
     2162                                }
     2163                        }
     2164                        elseif ( $type[2] == 3 ) {
     2165                                if (!imagepng( $thumbnail, $thumbpath ) ) {
     2166                                        $error = __( "Thumbnail path invalid" );
     2167                                }
     2168                        }
     2169
     2170                }
     2171        } else {
     2172                $error = __( 'File not found' );
     2173        }
     2174
     2175        if (!empty ( $error ) ) {
     2176                return $error;
     2177        } else {
     2178                apply_filters( 'wp_create_thumbnail', $thumbpath );
     2179                return $thumbpath;
     2180        }
     2181}
     2182
    20602183?>
Note: See TracChangeset for help on using the changeset viewer.