【问题标题】:Change image name on upload images wordpress在上传图片 wordpress 上更改图片名称
【发布时间】:2014-05-04 19:42:15
【问题描述】:

当我上传图片时,wordpress 会将四个文件保存到服务器上(在 settings->media 中定义):

1 -> thumbnailsize (150x150)
2 -> Medium size (1024x768)
3 -> Large size (1920x1080)
4 -> Original size (---x---)

我总是需要大尺寸的图片,但是当我上传尺寸为 1620x1080 的图片时,Wordpress 会将图片命名为 some-image-1620x1080.jpg 什么我不要。

我可以以任何方式将图像名称从 some-image-1920x1080.jpg 更改为 some-image-large.jpg

【问题讨论】:

    标签: php wordpress file-upload


    【解决方案1】:

    我还没有找到针对这个问题的具体解决方案,并且有一些人声称您无法更改上传文件的名称,只能添加一些内容。我现在使用的是 Maxon 的 Gallery Theme 插件,以便在两个单独的数据路径中获取我的缩略图和大图像,这解决了我的整个问题。该插件非常轻巧,非常适合画廊。

    插件可以在这里找到: http://wordpress.org/plugins/gallery-theme/

    【讨论】:

      【解决方案2】:

      您必须在 functions.php 文件中创建一个函数,该函数将处理文件上传并与过滤器 wp_handle_upload_prefilter 挂钩:

      add_filter('wp_handle_upload_prefilter', 'wp_rename_large_images', 1, 1);
      //Check to see if function name is unique
      if (!function_exists("wp_rename_large_images")) {
      function wp_rename_large_images( $file ){
         //Get image size 
         $img = getimagesize($file['tmp_name']);
         //Image dimensions
         $width = $img[0];
         $height = $img[1];
         //Check to see if image is large enough to change the file name
         if ($width > 1200 || $height > 1080) {
           //Modify the file name WITH an extension
           $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
           //Build the new file name (remove the old extension - last 3 characters from the file name, i.e. jpg, gif, png, bmp, etc.)
           $file['name'] = substr($file['name'], -3) . '-large'. $ext;
         }
          return $file;
      }
      }
      

      阅读文档here

      【讨论】:

      • 我试了脚本,上传图片一直报错:cl.ly/image/031M2b1U1m1Y文件扩展名有问题吗?
      • 这很奇怪……你确定你上传的是图片,而不是其他类型的 mime?
      • $filetype['ext'] 不是必需的,因为扩展名已经在文件名中。这就是问题所在。
      • 不过,它似乎不起作用。您的“-large”不能在文件名后面。你会得到类似 some-image.jpg-large 的东西。图片上传完美,但他没有进入 if 语句。
      • 似乎没有填充宽度和高度变量。我也不知道如何正确调试它,如果我想打印一些 wordpress 在上传图片时给我一个 HTTP 错误,所以我无法解决它。
      猜你喜欢
      • 2014-04-30
      • 2019-07-26
      • 2011-01-28
      • 2018-10-30
      • 2016-01-24
      • 2023-03-23
      • 2021-06-26
      • 1970-01-01
      • 2012-09-02
      相关资源
      最近更新 更多