自动修改原始[全尺寸]图像

时间:2012-04-13 作者:Dan

有没有办法让WordPress处理默认不修改的全尺寸图像?

1 个回复
最合适的回答,由SO网友:brasofilo 整理而成

是的,有。

在此处找到:
http://www.wprecipes.com/how-to-automatically-use-resized-image-instead-of-originals

它将用large 大小,在介质设置中定义(/wp-admin/options-media.php).

代码如下:

add_filter(\'wp_generate_attachment_metadata\',\'replace_uploaded_image\');

function replace_uploaded_image($image_data) 
{
    // if there is no large image : return
    if ( !isset($image_data[\'sizes\'][\'large\']) ) 
        return $image_data;

    // paths to the uploaded image and the large image
    $upload_dir              = wp_upload_dir();
    $uploaded_image_location = $upload_dir[\'basedir\'] . \'/\' . $image_data[\'file\'];
    $large_image_location    = $upload_dir[\'path\'] . \'/\' . $image_data[\'sizes\'][\'large\'][\'file\'];

    // delete the uploaded image
    unlink($uploaded_image_location);

    // rename the large image
    rename($large_image_location, $uploaded_image_location);

    // update image metadata and return them
    $image_data[\'width\']  = $image_data[\'sizes\'][\'large\'][\'width\'];
    $image_data[\'height\'] = $image_data[\'sizes\'][\'large\'][\'height\'];
    unset($image_data[\'sizes\'][\'large\']);

    return $image_data;
}
可以使用自定义尺寸:

add_image_size( \'new-large\', 1600, 1200 ); 
更改的所有引用$image_data[\'sizes\'][\'large\'] 在原始代码中,使用$image_data[\'sizes\'][\'new-large\']

结束

相关推荐

where is images/image.jpg?

我被迫在站点上使用SOAP服务,对于一些UI元素,它调用的是我必须放置到位的图像。我无法将它们指向主题文件夹,因为我无法控制HTML,而且我不愿意使用javascript。代码指向<img src=\"images/image.jpg\" >, 我尝试将一个图像文件夹添加到WP安装的根目录中,但没有成功。所以我的问题是,我应该把图像放在哪里,以便HTML可以找到它?