Make WordPress Core

Changeset 4643


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

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

Location:
trunk
Files:
2 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?>
  • trunk/wp-includes/functions.php

    r4631 r4643  
    11101110}
    11111111
    1112 function wp_generate_attachment_metadata( $attachment_id, $file ) {
    1113     $attachment = get_post( $attachment_id );
    1114 
    1115     $metadata = array();
    1116     if ( preg_match('!^image/!', get_post_mime_type( $attachment )) ) {
    1117         $imagesize = getimagesize($file);
    1118         $metadata['width'] = $imagesize['0'];
    1119         $metadata['height'] = $imagesize['1'];
    1120         list($uwidth, $uheight) = get_udims($metadata['width'], $metadata['height']);
    1121         $metadata['hwstring_small'] = "height='$uheight' width='$uwidth'";
    1122         $metadata['file'] = $file;
    1123 
    1124         if ( $metadata['width'] * $metadata['height'] < 3 * 1024 * 1024 ) {
    1125             if ( $metadata['width'] > 128 && $metadata['width'] >= $metadata['height'] * 4 / 3 )
    1126                 $thumb = wp_create_thumbnail($file, 128);
    1127             elseif ( $metadata['height'] > 96 )
    1128                 $thumb = wp_create_thumbnail($file, 96);
    1129 
    1130             if ( @file_exists($thumb) )
    1131                 $metadata['thumb'] = basename($thumb);
    1132         }
    1133     }
    1134     return apply_filters( 'wp_generate_attachment_metadata', $metadata );
    1135 }
    1136 
    1137 function wp_create_thumbnail( $file, $max_side, $effect = '' ) {
    1138 
    1139         // 1 = GIF, 2 = JPEG, 3 = PNG
    1140 
    1141     if ( file_exists( $file ) ) {
    1142         $type = getimagesize( $file );
    1143 
    1144         // if the associated function doesn't exist - then it's not
    1145         // handle. duh. i hope.
    1146 
    1147         if (!function_exists( 'imagegif' ) && $type[2] == 1 ) {
    1148             $error = __( 'Filetype not supported. Thumbnail not created.' );
    1149         }
    1150         elseif (!function_exists( 'imagejpeg' ) && $type[2] == 2 ) {
    1151             $error = __( 'Filetype not supported. Thumbnail not created.' );
    1152         }
    1153         elseif (!function_exists( 'imagepng' ) && $type[2] == 3 ) {
    1154             $error = __( 'Filetype not supported. Thumbnail not created.' );
    1155         } else {
    1156 
    1157             // create the initial copy from the original file
    1158             if ( $type[2] == 1 ) {
    1159                 $image = imagecreatefromgif( $file );
    1160             }
    1161             elseif ( $type[2] == 2 ) {
    1162                 $image = imagecreatefromjpeg( $file );
    1163             }
    1164             elseif ( $type[2] == 3 ) {
    1165                 $image = imagecreatefrompng( $file );
    1166             }
    1167 
    1168             if ( function_exists( 'imageantialias' ))
    1169                 imageantialias( $image, TRUE );
    1170 
    1171             $image_attr = getimagesize( $file );
    1172 
    1173             // figure out the longest side
    1174 
    1175             if ( $image_attr[0] > $image_attr[1] ) {
    1176                 $image_width = $image_attr[0];
    1177                 $image_height = $image_attr[1];
    1178                 $image_new_width = $max_side;
    1179 
    1180                 $image_ratio = $image_width / $image_new_width;
    1181                 $image_new_height = $image_height / $image_ratio;
    1182                 //width is > height
    1183             } else {
    1184                 $image_width = $image_attr[0];
    1185                 $image_height = $image_attr[1];
    1186                 $image_new_height = $max_side;
    1187 
    1188                 $image_ratio = $image_height / $image_new_height;
    1189                 $image_new_width = $image_width / $image_ratio;
    1190                 //height > width
    1191             }
    1192 
    1193             $thumbnail = imagecreatetruecolor( $image_new_width, $image_new_height);
    1194             @ imagecopyresampled( $thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1] );
    1195 
    1196             // If no filters change the filename, we'll do a default transformation.
    1197             if ( basename( $file ) == $thumb = apply_filters( 'thumbnail_filename', basename( $file ) ) )
    1198                 $thumb = preg_replace( '!(\.[^.]+)?$!', __( '.thumbnail' ).'$1', basename( $file ), 1 );
    1199 
    1200             $thumbpath = str_replace( basename( $file ), $thumb, $file );
    1201 
    1202             // move the thumbnail to it's final destination
    1203             if ( $type[2] == 1 ) {
    1204                 if (!imagegif( $thumbnail, $thumbpath ) ) {
    1205                     $error = __( "Thumbnail path invalid" );
    1206                 }
    1207             }
    1208             elseif ( $type[2] == 2 ) {
    1209                 if (!imagejpeg( $thumbnail, $thumbpath ) ) {
    1210                     $error = __( "Thumbnail path invalid" );
    1211                 }
    1212             }
    1213             elseif ( $type[2] == 3 ) {
    1214                 if (!imagepng( $thumbnail, $thumbpath ) ) {
    1215                     $error = __( "Thumbnail path invalid" );
    1216                 }
    1217             }
    1218 
    1219         }
    1220     } else {
    1221         $error = __( 'File not found' );
    1222     }
    1223 
    1224     if (!empty ( $error ) ) {
    1225         return $error;
    1226     } else {
    1227         apply_filters( 'wp_create_thumbnail', $thumbpath );
    1228         return $thumbpath;
    1229     }
    1230 }
    1231 
    12321112function wp_explain_nonce($action) {
    12331113    if ( $action !== -1 && preg_match('/([a-z]+)-([a-z]+)(_(.+))?/', $action, $matches) ) {
Note: See TracChangeset for help on using the changeset viewer.