【问题标题】:Adding image dimensions of media to url in wordpress在wordpress中将媒体的图像尺寸添加到url
【发布时间】:2012-11-28 11:53:18
【问题描述】:

当我在 wordpress 中上传图片时,我知道它的尺寸。可以说图像称为 image.jpg 。例如,我现在希望 wordpress 将图像 url 重命名为 image-w-180-h-200.jpg(其中图像的宽度为 180 像素,高度为 200 像素)。无论如何我可以将这些详细信息附加到 url,以便我可以直接“检索”这些参数? (我的目标是最终通过 javascript 获得 img 的宽度和高度,但这样我就不必预先加载 img 来获得它的尺寸,wordpress 已经为我完成了这项工作)

干杯!

【问题讨论】:

    标签: wordpress parameters upload image


    【解决方案1】:

    (改编自https://wordpress.stackexchange.com/questions/38582/hook-to-get-image-filename-when-it-is-uploaded

      function rename_image($results) {
       if( $results['type'] === 'image/jpeg' ) { // or /png or /tif / /whatever
    
         $filename = $results[ 'file' ]; // Use this line to add height and width of image;
         $url = $results[ 'url' ];
    
        // manipulate the image
       } 
     }
    
     add_action('wp_handle_upload', 'rename_image');
    

    请阅读指定的 URL 以了解更多信息

    【讨论】:

      【解决方案2】:

      另一种方法是在上传时修改缩略图文件名。以下方法可以重命名新上传的图片。

      来自其他 WPSE 答案:https://wordpress.stackexchange.com/a/51983/12615

      切入正题,这是full working code

      而且,理论上,您应该将函数 bt_image_make_intermediate_size 更改为:未测试

      function bt_image_make_intermediate_size( $file, $width, $height, $crop = false, $size ) 
      {
          if ( $width || $height ) {
              $suffix = 'w-' . $width . '-h-' . $height;
      
              $resized_file = bt_image_resize( 
                  $file, $width, $height, $crop, $suffix, null, 90 
              );
      
              if ( 
                  !is_wp_error( $resized_file ) 
                  && $resized_file 
                  && $info = getimagesize( $resized_file ) 
              ) {
                  $resized_file = apply_filters(
                      'image_make_intermediate_size', 
                      $resized_file
                  );
                  return array(
                      'file' => wp_basename( $resized_file ),
                      'width' => $info[0],
                      'height' => $info[1],
                  );
              }
          }
          return false;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-14
        • 2014-06-07
        • 1970-01-01
        • 2019-03-15
        • 1970-01-01
        • 2013-04-02
        • 1970-01-01
        • 2015-03-30
        相关资源
        最近更新 更多