Make WordPress Core


Ignore:
Timestamp:
12/07/2006 10:42:22 PM (18 years ago)
Author:
ryan
Message:

Create attachment metadata for xmlrpc uploads. Props mdawaffe. fixes #3452

File:
1 edited

Legend:

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

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