1

When I upload an image in wordpress knows its dimensions. Lets say the image is called image.jpg . I now want wordpress to rename the image url to image-w-180-h-200.jpg for example (where the image has a width of 180 pixels and a height of 200 pixels). Is there anyway I can append these details to the url so I can 'retrieve' these parameters directly? (My goal is to eventually get the img width and height through javascript but this way I don't have to preload the img to get it's dimensions, wordpress already has done the work for me)

Cheers!

4

2 回答 2

1

(改编自https://wordpress.stackexchange.com/questions/38582/hook-to-get-image-filename-when-it-is-uploaded

  function rename_image($results) {
   if( $results['type'] === 'image/jpeg' ) { // or /png or /tif / /whatever

     $filename = $results[ 'file' ]; // Use this line to add height and width of image;
     $url = $results[ 'url' ];

    // manipulate the image
   } 
 }

 add_action('wp_handle_upload', 'rename_image');

请阅读指定的 URL 以获取更多信息

于 2012-12-11T00:26:03.763 回答
1

另一种方法是在上传时修改缩略图文件名。以下将用于重命名新上传的图像。

从另一个 WPSE 答案:https ://wordpress.stackexchange.com/a/51983/12615

切入正题,这是完整的工作代码

而且,理论上,您应该将功能更改bt_image_make_intermediate_size为:未测试

function bt_image_make_intermediate_size( $file, $width, $height, $crop = false, $size ) 
{
    if ( $width || $height ) {
        $suffix = 'w-' . $width . '-h-' . $height;

        $resized_file = bt_image_resize( 
            $file, $width, $height, $crop, $suffix, null, 90 
        );

        if ( 
            !is_wp_error( $resized_file ) 
            && $resized_file 
            && $info = getimagesize( $resized_file ) 
        ) {
            $resized_file = apply_filters(
                'image_make_intermediate_size', 
                $resized_file
            );
            return array(
                'file' => wp_basename( $resized_file ),
                'width' => $info[0],
                'height' => $info[1],
            );
        }
    }
    return false;
}
于 2012-12-11T01:43:54.480 回答