有可能重新组织WordPress上传目录吗?

时间:2017-05-15 作者:thejimbirch

我有一个WordPress网站,所有上传内容都已上传到/wp-content/uploads/*. 现在有数千张图像,性能受到了影响,我被指派来帮助修复它。

以前是否有人对上载目录进行过重新排序?将其移动到更结构化的YYYY/MM?我很想听到一些关于如何实现这一目标的策略。

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

不幸的是,WordPress这是一个非常令人伤心的事实,它几乎没有为后端的上传提供定制(除了y/m组织的东西)。

您可以做的是创建自己的自定义类,以更有组织的方式生成缩略图(这是性能影响的最重要部分,因为每个图像都会生成几个到几十个缩略图)。

Step 1,2 and 3: 备份所有上载的内容。除非您有上载文件夹的备份,否则您将要执行的操作无法逆转!

Step 4 : 下载并安装Thumbnail Cleaner 插件。这允许您删除每个生成的缩略图。

Step 5 : 创建自己的生成方法。我给你举个例子:

add_filter(\'wp_image_editors\', \'my_wp_image_editors\');
function my_wp_image_editors($editors) {
    array_unshift($editors, "my_WP_Image_Editor");
    return $editors;
}
// Include the existing classes first in order to extend them.
require_once ABSPATH . WPINC . "/class-wp-image-editor.php";
require_once ABSPATH . WPINC . "/class-wp-image-editor-gd.php";
// Now we extend the original image editor class
class my_WP_Image_Editor extends WP_Image_Editor_GD {
    public function generate_filename($suffix = null, $dest_path = null, $extension = null) {
        // $suffix will be appended to the destination filename, just before the extension
        if (!$suffix) {
            $suffix = $this->get_suffix();
        }
        $dir = pathinfo($this->file, PATHINFO_DIRNAME);
        $ext = pathinfo($this->file, PATHINFO_EXTENSION);
        $name = wp_basename($this->file, ".$ext");
        $new_ext = strtolower($extension ? $extension : $ext );
        if (!is_null($dest_path) && $_dest_path = realpath($dest_path)) {
            $dir = $_dest_path;
        }
        //we get the dimensions using explode, we could have used the properties of $this->file[height] but the suffix could have been provided
        $size_from_suffix = explode("x", $suffix);
        //we get the slug_name for this dimension
        $slug_name = $this->get_slug_by_size($size_from_suffix[0], $size_from_suffix[1]);
        return trailingslashit( $dir ) . "{$slug_name}/{$name}.{$new_ext}";
    }
    function multi_resize($sizes) {
        $metadata = array();
        $orig_size = $this->size;
        foreach ( $sizes as $size => $size_data ) {
            if ( ! isset( $size_data[\'width\'] ) && ! isset( $size_data[\'height\'] ) ) {
                continue;
            }
            if ( ! isset( $size_data[\'width\'] ) ) {
                $size_data[\'width\'] = null;
            }
            if ( ! isset( $size_data[\'height\'] ) ) {
                $size_data[\'height\'] = null;
            }
            if ( ! isset( $size_data[\'crop\'] ) ) {
                $size_data[\'crop\'] = false;
            }
            $image = $this->_resize( $size_data[\'width\'], $size_data[\'height\'], $size_data[\'crop\'] );
            if ( ! is_wp_error( $image ) ) {
                $resized = $this->_save( $image );
                imagedestroy( $image );
                if ( ! is_wp_error( $resized ) && $resized ) {
                    unset( $resized[\'path\'] );
                    $metadata[$size] = $resized;
                }
            }
            $this->size = $orig_size;
        }
        //we add the slug to the file path
        foreach ($metadata as $slug => $data) {
            $metadata[$slug][\'file\'] = $slug . "/" . $data[\'file\'];
        }
        return $metadata;
    }
    // Our custom function to retrieve the proper slug by weight and height
    function get_slug_by_size($width, $height) {
        // Make thumbnails and other intermediate sizes.
        $_wp_additional_image_sizes = wp_get_additional_image_sizes();
        $image_sizes = array(); //all sizes the default ones and the custom ones in one array
        foreach (get_intermediate_image_sizes() as $s) {
            $image_sizes[$s] = array(\'width\' => \'\', \'height\' => \'\', \'crop\' => false);
            if (isset($_wp_additional_image_sizes[$s][\'width\'])) {
                // For theme-added sizes
                $image_sizes[$s][\'width\'] = intval($_wp_additional_image_sizes[$s][\'width\']);
            } else {
                // For default sizes set in options
                $image_sizes[$s][\'width\'] = get_option("{$s}_size_w");
            }
            if (isset($_wp_additional_image_sizes[$s][\'height\'])) {
                // For theme-added sizes
                $image_sizes[$s][\'height\'] = intval($_wp_additional_image_sizes[$s][\'height\']);
            } else {
                // For default sizes set in options
                $image_sizes[$s][\'height\'] = get_option("{$s}_size_h");
            }
            if (isset($_wp_additional_image_sizes[$s][\'crop\'])) {
                // For theme-added sizes
                $image_sizes[$s][\'crop\'] = $_wp_additional_image_sizes[$s][\'crop\'];
            } else {
                // For default sizes set in options
                $image_sizes[$s][\'crop\'] = get_option("{$s}_crop");
            }
        }
        $slug_name = ""; //the slug\'s name
        if($width >= $height){
            foreach ($image_sizes as $slug => $data) { // we start checking
                if ($data[\'width\'] == $width) {//we use only width because regardless of the height, the width is the one used for resizing in all cases with crop 1 or 0
                    $slug_name = $slug;
                }
                /*
                 * There could be custom added image sizes that have the same width as one of the defaults so we also use height here
                 * if there are several image sizes with the same width all of them will override the previous one leaving the last one, here we get also the last one
                 * since is looping the entire list, the height is used as a max value for non-hard cropped sizes
                 *  */
                  if ($data[\'width\'] == $width && $data[\'height\'] == $height) {
                      $slug_name = $slug;
                  }
        }
        } else {
            foreach ($image_sizes as $slug => $data) {
                if ($data[\'height\'] == $height) {
                    $slug_name = $slug;
                }
                if ($data[\'height\'] == $height && $data[\'width\'] == $width ) {
                    $slug_name = $slug;
                }
            }
        }
        return $slug_name;
    }
}
该类几乎是从中包含的原始类复制而来class-wp-image-editor-gd.php, 有一个区别:它将缩略图存储在单独的文件夹中,所有缩略图都根据大小存储在上传目录中。因此,在上载图像后,您将得到如下结果:

/uploads/image.jpg
/uploads/thumbnail/image.jpg
/uploads/medium/image.jpg
/uploads/large/image.jpg
// And so on...
这将防止在单个文件夹中有一百万个图像。您可以按需要的方式编辑类、更改路径等。提供了一个示例来演示如何生成和存储图像。

Step 6 : 使用Regenerate Thumbnails 插件,以一种奇特的方式用新生成的缩略图填充上传文件夹。这将防止数千个缩略图全部存储在一个文件夹中。可以找到一个工作示例here. 右键单击并在新选项卡中打开缩略图,然后尝试更改slug以查看其工作方式。

我希望这能让您更深入地理解如何在WordPress中操作图像生成。

结束

相关推荐

将WordPress附件导入wp-Content/Uploads/中的自定义目录

我希望有人能阐明我试图解决的这个问题,即使只是简单地改变我的思维过程,告诉我如何解决这个问题。我正在将一个基于CakePHP的定制CMS web应用程序导入WordPress,方法是遍历数据库,进行大量SQL查询,构建数组,最后输出必要的XML文件,以便使用WordPress导入工具导入。目前我已经很好地导入了类别、标签和帖子/页面,没有问题,但问题似乎出在图像上。基于CakePHP的CMS中当前的图像格式是images/year/month/day/post_id/image_name.ext据我所知,