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字段将需要更新.我已经在这里添加了一个注释. 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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |