删除原始图像-保留缩略图?

时间:2011-06-22 作者:FLX

我使用了很多缩略图,但从未使用原始文件。为了节省空间,我想阻止原始文件保存在磁盘上,但只保留100px的缩略图。我该怎么做?

谢谢,丹尼斯

7 个回复
最合适的回答,由SO网友:Cristian 整理而成
add_filter( \'wp_generate_attachment_metadata\', \'delete_fullsize_image\' );
function delete_fullsize_image( $metadata )
{
    $upload_dir = wp_upload_dir();
    $full_image_path = trailingslashit( $upload_dir[\'basedir\'] ) . $metadata[\'file\'];
    $deleted = unlink( $full_image_path );

    return $metadata;
}
SO网友:SimonSimCity

我在网上找到了另一个解决方案。这是基于这里所接受的,但它更进一步。

此处接受的一个删除了主图像并继续。我找到的解决方案用为“大”生成的图像替换了原始图像。如果未定义此图像分辨率,它将继续运行而不替换。

这样可以确保依赖于原始图像的每个脚本仍能像以前一样工作,例如thumnail再生。

http://www.wprecipes.com/how-to-automatically-use-resized-image-instead-of-originals

EDIT:

@dalbaeb向我指出了博客评论中的一个问题。我根据API而不是那里提供的解决方案重写了代码。这并没有多大区别,只是为了使用与API中相同的函数调用;)

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_filename = $image_data[\'sizes\'][\'large\'][\'file\'];

    // Do what wordpress does in image_downsize() ... just replace the filenames ;)
    $image_basename = wp_basename($uploaded_image_location);
    $large_image_location = str_replace($image_basename, $large_image_filename, $uploaded_image_location);

    // 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\']);

    // Check if other size-configurations link to the large-file
    foreach($image_data[\'sizes\'] as $size => $sizeData) {
        if ($sizeData[\'file\'] === $large_image_filename)
            unset($image_data[\'sizes\'][$size]);
    }

    return $image_data;
}
add_filter(\'wp_generate_attachment_metadata\', \'replace_uploaded_image\');

EDIT2:

我在一个客户端上遇到了代码问题,其中另一个大小配置链接到了一个大文件。我相应地更新了代码。如果你有什么问题,给我发封邮件。

SO网友:Carlos Minatti

我只是创建了一个插件,因为我有同样的问题。您可以从下载here

SO网友:Pitchpole

我知道这很旧,但我仍然需要解决这个问题,当我在函数中使用它时,这可能对新图像有效。php,但我确实有一些问题和图像变得不可用,我也无法利用这一点来更新现有的图像。我有大约2000张现有的图片,我需要把它们全部浏览一遍,把所有旧的大图片缩小。我在前端创建了一个只有管理员才能访问的页面,并使用了此代码。

这里最大的区别在于,不是删除OG图像,而是将“大”图像重命名为OG名称。(这对我不起作用,我尝试这种方法时丢失了很多图像),因此我复制了大图像,并将其命名为与OG图像相同的副本。因此,大图像会被较小的“大”图像覆盖。同样在前端,我必须改变元数据的更新方式。

Test this on individual posts first, be sure this works for you before changing numposts to -1. I stupidly lost about 100 images trying to run the original code without carefully checking the results first.

更新现有图像的前端代码:

function replace_uploaded_image_frontend($image_data, $attachment_id) {

 // 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\'];
  $current_subdir = substr($image_data[\'file\'],0,strrpos($image_data[\'file\'],"/"));
  $large_image_location = $upload_dir[\'basedir\'] . \'/\'.$current_subdir.\'/\'.$image_data[\'sizes\'][\'large\'][\'file\'];

  //Original code was delete OG file and rename the large file to the OG name. This means you no longer have the large size image. So...
  //Instead I want to take the large file and make a copy and overwrite it over the large file.
    $file_to_be_copied = $large_image_location;
    $copied_file_name = $uploaded_image_location;
  //make a copy of the large image and name that the title of the original image
    if (!copy($file_to_be_copied, $copied_file_name)) {
        echo "failed to copy $file...\\n";
    }  
    fixImageMeta($attachment_id );

}

    function fixImageMeta($attach_id) {

            $file = get_attached_file($attach_id);
            if (!empty($file)) {
                $info = getimagesize($file);
                $meta = array (
                    \'width\' => $info[0],
                    \'height\' => $info[1],
                    \'hwstring_small\' => "height=\'{$info[1]}\' width=\'{$info[0]}\'",
                    \'file\' => basename($file),
                    \'sizes\' => array(),         // thumbnails etc.
                    \'image_meta\' => array(),    // EXIF data
                );
                update_post_meta($attach_id, \'_wp_attachment_metadata\', $meta);
            }
    }


          $args = array(
        \'post_type\' => \'exhibitions\',
        \'p\' => 12926,
        \'order\' => \'DESC\',
        /*\'date_query\'     => array(
            \'after\' => array(
                \'year\'  => 2016,
                \'month\' => 2,
                \'day\'   => 28,
            ),
            \'before\' => array(
                \'year\'  => 2016,
                \'month\' => 6,
                \'day\'   => 30,
            ),
        ),*/
        \'numberposts\' => 1
    );
    $myposts = get_posts($args);
    foreach ($myposts as $mypost){

        $attachment_id = get_post_thumbnail_id( $mypost->ID );
        echo \'<br>Attach ID: \' . $attachment_id . \' \';
        $unfiltered = false;
        $image_data = wp_get_attachment_metadata( $attachment_id, $unfiltered );
        print_r($image_data);
        replace_uploaded_image_frontend($image_data, $attachment_id);

    }
我想这当然是由于一些损坏的图像而导致的几次错误。因此,在尝试一次检查完所有日期并发现错误后,我不得不花一些时间检查日期范围,以确保所有日期都得到更新。

新图像的代码,在函数中。php

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\'];
  $current_subdir = substr($image_data[\'file\'],0,strrpos($image_data[\'file\'],"/"));
  $large_image_location = $upload_dir[\'basedir\'] . \'/\'.$current_subdir.\'/\'.$image_data[\'sizes\'][\'large\'][\'file\'];

  //Instead I want to take the large file and make a copy and overwrite it over the original file.
    $file_to_be_copied = $large_image_location; 
    $copied_file_name = $uploaded_image_location;
  //make a copy of the large image and name that the title of the original image
    if (!copy($file_to_be_copied, $copied_file_name)) {
        echo "failed to copy $file...\\n";
    }

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

  return $image_data;
}
add_filter(\'wp_generate_attachment_metadata\',\'replace_uploaded_image\');

SO网友:Joris van Montfort

上面的答案似乎不再适用了。我稍微修改了代码,下面的示例适用于WordPress 5.5

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

    // paths to the uploaded image and the large image
    $upload_dir = wp_upload_dir();
    $uploaded_image_location = dirname($upload_dir[\'basedir\'] . \'/\' . $image_data[\'file\']) . \'/\' . $image_data[\'original_image\'];
    
    // delete the uploaded image
    unlink($uploaded_image_location);

    unset($image_data[\'original_image\']);

    return $image_data;
}
add_filter(\'wp_generate_attachment_metadata\', \'replace_uploaded_image\');```

SO网友:ztvmark

Wordpress禁用上载图像原件

测试wordpress 5.6

通过https://stackoverflow.com/questions/41626139/wordpress-removing-original-image-after-resize

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\']; // ** This only works for new image uploads - fixed for older images below.
  $current_subdir = substr($image_data[\'file\'],0,strrpos($image_data[\'file\'],"/"));
  $large_image_location = $upload_dir[\'basedir\'] . \'/\'.$current_subdir.\'/\'.$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_filter(\'wp_generate_attachment_metadata\',\'replace_uploaded_image\');

SO网友:Francois

上述解决方案的问题是,未链接的图像是“big\\u image\\u size\\u threshold”中的缩放图像。(自WP 5.3起)如果您添加:

add_filter( \'big_image_size_threshold\', \'__return_false\' );
您的功能。php,解决方案有效!

结束

相关推荐

how to edit attachments?

在将例如文件附加到帖子时,如何在事后编辑/删除它们?在帖子编辑器中找不到任何内容。谢谢