<bdo id='KQLc6'></bdo><ul id='KQLc6'></ul>
      <legend id='KQLc6'><style id='KQLc6'><dir id='KQLc6'><q id='KQLc6'></q></dir></style></legend>
      1. <i id='KQLc6'><tr id='KQLc6'><dt id='KQLc6'><q id='KQLc6'><span id='KQLc6'><b id='KQLc6'><form id='KQLc6'><ins id='KQLc6'></ins><ul id='KQLc6'></ul><sub id='KQLc6'></sub></form><legend id='KQLc6'></legend><bdo id='KQLc6'><pre id='KQLc6'><center id='KQLc6'></center></pre></bdo></b><th id='KQLc6'></th></span></q></dt></tr></i><div id='KQLc6'><tfoot id='KQLc6'></tfoot><dl id='KQLc6'><fieldset id='KQLc6'></fieldset></dl></div>
        <tfoot id='KQLc6'></tfoot>

        <small id='KQLc6'></small><noframes id='KQLc6'>

      2. WordPress - 上传时模糊图像

        时间:2023-12-01
          <tfoot id='p6qXx'></tfoot>

                <small id='p6qXx'></small><noframes id='p6qXx'>

                  <bdo id='p6qXx'></bdo><ul id='p6qXx'></ul>
                  <legend id='p6qXx'><style id='p6qXx'><dir id='p6qXx'><q id='p6qXx'></q></dir></style></legend>

                  <i id='p6qXx'><tr id='p6qXx'><dt id='p6qXx'><q id='p6qXx'><span id='p6qXx'><b id='p6qXx'><form id='p6qXx'><ins id='p6qXx'></ins><ul id='p6qXx'></ul><sub id='p6qXx'></sub></form><legend id='p6qXx'></legend><bdo id='p6qXx'><pre id='p6qXx'><center id='p6qXx'></center></pre></bdo></b><th id='p6qXx'></th></span></q></dt></tr></i><div id='p6qXx'><tfoot id='p6qXx'></tfoot><dl id='p6qXx'><fieldset id='p6qXx'></fieldset></dl></div>
                    <tbody id='p6qXx'></tbody>

                • 本文介绍了WordPress - 上传时模糊图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  所以我遵循 这里给出的示例(我修改为仅模糊,无水印),以便在上传时在 WordPress 中制作模糊图像.问题是,如果上传的文件大小完全相同,或者小于设置的大小,那么 WordPress 将不会生成图像,因此不会产生模糊的图像.

                  So I'm following the example given here (which I modified to only blur, no watermark), to make a blurred image in WordPress on upload. The problem is, that if the uploaded file is the exact same size, or smaller, than the set size, then WordPress will not generate an image, and hence no blurred one will be made.

                  我尝试使用 isst($meta['sizes']['background-image-blurred']['file']) 来确定是否制作了一个,如果没有,则 copy() 源文件,但是不会为图像生成 WordPress元数据"(对于非 WordPress 人员,元数据与您的想法不同),所以它会给出高度/宽度使用 wp_get_attachment_image 显示时未定义的问题.

                  I tried using a isst($meta['sizes']['background-image-blurred']['file']) to determine if one was made, and if not then copy() the source file, but then no WordPress "metadata" would be generated for the image (for non-WordPress people, the metadata is different than what you think), so it would give height/width undefined problems when displaying using wp_get_attachment_image.

                  所以我确信使用如下所示的 wp_get_attachment_image 钩子可能是错误的方法.它可能需要在图像上传过程的早期发生.

                  So I'm convinced using wp_get_attachment_image hook as shown below is probably the wrong way to do this. It probably needs to happen earlier in the image upload process.

                  关于如何最好地完成这项工作的任何想法?

                  Any ideas on how to best get this working?

                  /**
                   * Several functions relatting to blurring images on uploaded.
                   * @see https://codeable.io/community/how-to-watermark-wordpress-images-with-imagemagick/
                   */ 
                      add_image_size( 'background-image-blurred', 1920, 1080, true );
                  
                      function generate_blurred_image( $meta ) {
                  
                        $time = substr( $meta['file'], 0, 7); // Extract the date in form "2015/04"
                        $upload_dir = wp_upload_dir( $time ); // Get the "proper" upload dir
                  
                        $filename = $meta['sizes']['background-image-blurred']['file'];
                        $meta['sizes']['background-image-blurred']['file'] = blur_image( $filename, $upload_dir );
                  
                        return $meta;
                  
                      }
                      add_filter( 'wp_generate_attachment_metadata', 'generate_blurred_image' );    
                  
                      function blur_image( $filename, $upload_dir ) {
                  
                        $original_image_path = trailingslashit( $upload_dir['path'] ) . $filename;
                  
                        $image_resource = new Imagick( $original_image_path );
                        $image_resource->gaussianBlurImage( 10, 100 ); // See: http://phpimagick.com/Imagick/gaussianBlurImage
                  
                        return save_blurred_image( $image_resource, $original_image_path );
                  
                      }    
                  
                      function save_blurred_image( $image_resource, $original_image_path ) {
                  
                        $image_data = pathinfo( $original_image_path );
                  
                        $new_filename = $image_data['filename'] . '-blurred.' . $image_data['extension'];
                  
                        // Build path to new blurred image
                        $blurred_image_path = str_replace($image_data['basename'], $new_filename, $original_image_path);
                  
                        if ( ! $image_resource->writeImage( $blurred_image_path ) ) {
                          return $image_data['basename'];          
                        }
                  
                        // Delete the placeholder image WordPress made now that it's been blurred
                        unlink( $original_image_path );
                  
                        return $new_filename;
                  
                      }    
                  

                  推荐答案

                  不幸的是 wp 没有一个过滤器来强制调整大小,所以你可以做的是在之后挂钩并调整你的图像大小,如果没有创建并将它弹出到元数据.

                  Unfortunately wp hasn't got a filter to force a size so what you can do is hook in after and resize your image if not created and pop it into the metadata.

                  我没有 imagick,所以你必须自己尝试这些函数,但你上面的过程是正确的,你只需要更新文件名并输入下面的数组.PS不要在过滤器内输出任何东西!

                  I haven't got imagick so you will have to try these functions yourself, but you have the correct process above, you just need to update the filename and type in the array below. PS don't output anything within the filter!

                  function custom_img_size(){
                      add_image_size( 'background-image-blurred', 1920, 1080, true );
                  }
                  
                  add_action( 'after_setup_theme', 'custom_img_size' );
                  
                  
                  add_filter('wp_generate_attachment_metadata', 'force_add_size', 100);
                  function force_add_size( $metadata ) {
                  
                     if(!isset($metadata['sizes']['background-image-blurred'])){
                          //not set so initiate our custom size...
                          //I dont have imagick installed so just use your functions here to duplicate
                          //note original file = $filename update the $newfilename below...
                          //sample resize code ...
                          $upload_dir = wp_upload_dir();
                          $filename= $upload_dir['basedir'].'/'.$metadata['file'];
                          $extension = strtolower(strrchr($metadata['file'], '.'));
                          $newfilename= str_replace($extension, '-1200x1080', $filename).$extension;
                  
                          copy($filename, $newfilename );
                          //end sample resize code.....
                  
                  
                  
                          $filetype= 'image/jpeg';
                          $metadata['sizes']['background-image-blurred']= array(
                              "file"=> $newfilename,
                              "width"=> 1920, 
                              "height"=> 1080,
                              "mime-type"=> $filetype 
                          );
                  
                     }
                  
                  
                     return $metadata;
                  
                  }
                  

                  更新

                  1. 这旨在仅捕获现有过滤器未能创建模糊自定义尺寸的位置,否则它什么也不做.您仍应包含原始过滤器.您可能在原始代码中遇到问题:您正在删除过滤器中的原始文件,这将导致问题,因为有一个名为_wp_attached_file"的 postmeta 字段需要更新.我在下面附上了一个注释.

                  1. This is designed to only catch where your existing filter has failed to create your blurred custom size otherwise it does nothing. You should still include your original filters. You may have an issue in the original code: You are deleting the original file in your filters and this will cause issues as there is a postmeta field called '_wp_attached_file' that will need updating. I have included a note below on this.

                  过滤器会在保存之前捕获元数据,因此一旦您返回 $metadata,任何更改也将被保存.如果您查看源代码:https://core.trac.wordpress.org/browser/tags/3.8.1/src/wp-admin/includes/image.php#L72 在这里你可以确切地看到它是如何工作的.我也确认使用 wp4.3

                  The filter catches the metadata before saving so any changes are also going to be saved once you return the $metadata. If you look at the source code: https://core.trac.wordpress.org/browser/tags/3.8.1/src/wp-admin/includes/image.php#L72 here you can see exactly how it works. I've also confirmed using wp4.3

                  我已尝试在下方插入您需要的 imagick 函数.我没有测试过,因为我实际上并没有将它安装在任何地方.(imagemagick 实际上是一个很棒的开源程序,但服务器非常密集).试试这个函数来代替上面的那个:

                  I have attempted to insert the imagick functions you need below. I havent tested as i dont actually have it installed anywhere. (imagemagick is actually a wonderful opensource program but very server intensive). Try this function in place of the one above:

                  add_filter('wp_generate_attachment_metadata', 'force_add_size', 100, 2);
                  
                  function force_add_size( $metadata, $id ){
                  
                      $upload_dir = wp_upload_dir();
                      $filename= $upload_dir['basedir'].'/'.$metadata['file'];
                      $extension = strtolower(strrchr($metadata['file'], '.'));
                      $newfilename= str_replace($extension, '-blurred', $filename).$extension;
                  
                      $image_resource = new Imagick( $filename);
                      $image_resource->resizeImage(1920,1080,Imagick::FILTER_LANCZOS,.3);
                      $image_resource->writeImage( $newfilename );
                      //http://www.dylanbeattie.net/magick/filters/result.html
                  
                      unlink( $filename );//delete original image altogether ---> you might want to update the post meta on this '_wp_attached_file' , you can actually get the attachment id from the filter, i have added it above. It would be best to have a actual image url in there! something like $sfile= str_replace($upload_dir['basedir'],'', $newfilename); update_post_meta($id, '_wp_attached_file', $sfile );
                  
                  
                  
                      switch($extension){
                          case '.jpg':
                          case '.jpeg':
                              $type = 'image/jpeg';
                              break;
                          case '.gif':
                              $type = 'image/gif';
                              break;
                          case '.png':
                              $type = 'image/png';
                              break;
                          default:
                              $type = 'image/jpeg'; // you might want a conditional to check its actually a image...imagick will do this for you as well....it shouldnt get this far if not a image.
                              break;
                      } 
                  
                      $metadata['sizes']['background-image-blurred']= array(
                          "file"=> $newfilename,
                          "width"=> 1920,//your custom image size, has to be this! you could get the global var and check for sizes but its this size in particular we want? 
                          "height"=> 1080,
                          "mime-type"=> $type 
                      );
                  
                      return $metadata;
                  }
                  

                  更新为防止图像拉伸较小的图像,请用此替换 imagick 代码.

                  update to prevent the image stretching out smaller images replace the imagick code with this.

                  $upload_dir = wp_upload_dir();
                  $filename= $upload_dir['basedir'].'/'.$metadata['file'];
                  $extension = strtolower(strrchr($metadata['file'], '.'));
                  $newfilename= str_replace($extension, '-blurred', $filename).$extension;
                  
                  $image_resource = new Imagick( $filename);
                  
                  
                  if($image_resource->getImageWidth() <= 1920 || $image_resource->getImageHeight() > <= 1020) {
                      return $metadata;
                  }
                  
                  $image_resource->resizeImage(1920,1080,Imagick::FILTER_LANCZOS,.3);
                  $image_resource->writeImage( $newfilename );
                  //http://www.dylanbeattie.net/magick/filters/result.html
                  

                  这篇关于WordPress - 上传时模糊图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:php &amp;jquery 上传图片并即时预览显示 下一篇:是否可以使用 HTML 表单/PHP 脚本上传文件夹

                  相关文章

                  • <bdo id='hlHDZ'></bdo><ul id='hlHDZ'></ul>
                  <legend id='hlHDZ'><style id='hlHDZ'><dir id='hlHDZ'><q id='hlHDZ'></q></dir></style></legend>

                  <tfoot id='hlHDZ'></tfoot>

                      <small id='hlHDZ'></small><noframes id='hlHDZ'>

                      <i id='hlHDZ'><tr id='hlHDZ'><dt id='hlHDZ'><q id='hlHDZ'><span id='hlHDZ'><b id='hlHDZ'><form id='hlHDZ'><ins id='hlHDZ'></ins><ul id='hlHDZ'></ul><sub id='hlHDZ'></sub></form><legend id='hlHDZ'></legend><bdo id='hlHDZ'><pre id='hlHDZ'><center id='hlHDZ'></center></pre></bdo></b><th id='hlHDZ'></th></span></q></dt></tr></i><div id='hlHDZ'><tfoot id='hlHDZ'></tfoot><dl id='hlHDZ'><fieldset id='hlHDZ'></fieldset></dl></div>