重命名上载过程中生成的媒体文件

时间:2017-02-16 作者:Estarius

我正在尝试修改WordPress中的默认媒体名称,以删除文件中的维度,并用简单的后缀替换它们。

例如:

图像A-300x300。jpg图像A-480x480。jpg图像A-1024x1024。jpg将成为

想象一个拇指。jpg图像A介质。jpg图像A大。我该怎么做?到目前为止,我已经尝试在wp\\u handle\\u upload\\u prefilter中添加一个过滤器,但它似乎只修改了原始名称。。。

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

使用我自己的代码here, 我更新了逻辑以添加图像大小的名称,而不是后缀,请尝试将此添加到functions.php 文件:

add_filter("wp_image_editors", "my_wp_image_editors");

function my_wp_image_editors($editors) {
    array_unshift($editors, "WP_Image_Editor_Custom");
    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";

class WP_Image_Editor_Custom 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) . "{$name}-{$slug_name}.{$new_ext}";
    }

    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 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;
    }
}
我还删除了不必要的代码,这也适用于自定义图像大小,如下所示:

enter image description here

home-bottom 是我的自定义图像大小。

相关推荐

Media uploads error

我对这个很陌生,昨天刚创建了我的网站。突然,我无法上传任何照片,并且显示了一个错误“上传时出错”。请稍后再试\'。我看到一个使用控制台的建议,它给出了一个错误,表示内容混合,并且我的页面是通过HTTPS加载的,但请求了一个不安全的样式表tp://fonts。googleapis。com/css?家庭=舞蹈+脚本(&P);ver=4.9.8’。此请求已被阻止;内容必须通过HTTPS提供。