php – WordPress – 上传的模糊图像

所以我按照 example given here(我修改为只有模糊,没有水印),在wordpress上传造成一个模糊的图像.问题是,如果上传文件与设置的大小完全相同的大小或更小,那么wordpress将不会生成图像,因此不会造成模糊.

我尝试使用一个isst($Meta [‘sizes’] [‘background-image-blur’] [‘file’])来确定是否已经被创建,如果没有,那么copy()文件,但是没有wordpress将为图像生成“元数据”(对于非wordpress的人,元数据与您的想法不同),因此在使用wp_get_attachment_image显示时会给出高度/宽度未定义的问题.

所以我相信使用wp_get_attachment_image钩子,如下所示可能是错误的方式来做到这一点.它可能需要在图像上传过程的早期发生.

关于如何最好地得到这个工作的任何想法?

/**
 * 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'],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没有一个过滤器强制大小,所以你可以做的是钩入并调整你的图像大小,如果没有创建并将其弹出到元数据.

我没有想象力,所以你必须自己尝试这些功能,但是你有正确的过程,你只需要更新文件名并在下面的数组中输入. PS不会在过滤器中输出任何内容

function custom_img_size(){
    add_image_size( 'background-image-blurred',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;

}

更新

>这是为了只捕捉你现有的过滤器失败创建模糊的自定义大小,否则它什么都不做.您仍然应该包含原始过滤器.您可能在原始代码中有问题:您正在删除过滤器中的原始文件,这将导致问题,因为有一个名为’_wp_attached_file’的postMeta字段将需要更新.我已经在这里添加一个注释.
>过滤器在保存之前捕获元数据,所以任何更改也将在返回$元数据后保存.如果你看源码:https://core.trac.wordpress.org/browser/tags/3.8.1/src/wp-admin/includes/image.php#L72这里你可以看到它的工作原理.我也确认使用wp4.3
>我尝试在下面插入您需要的想像功能.我没有测试,因为我实际上没有安装在任何地方. (imagemagick实际上是一个很棒的开源程序,但服务器密集).尝试此功能代替上述功能

add_filter('wp_generate_attachment_Metadata',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,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,//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;
}

更新
为了防止图像展开较小的图像,用这个代替想象代码.

$upload_dir = wp_upload_dir();
$filename= $upload_dir['basedir'].'/'.$Metadata['file'];
$extension = strtolower(strrchr($Metadata['file'],'.'));
$newfilename= str_replace($extension,$filename).$extension;

$image_resource = new Imagick( $filename);


if($image_resource->getimageWidth() <= 1920 || $image_resource->getimageHeight() > <= 1020) {
    return $Metadata;
}

$image_resource->resizeImage(1920,.3);
$image_resource->writeImage( $newfilename );
//http://www.dylanbeattie.net/magick/filters/result.html

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...