所以我正在关注 example given here (我将其修改为仅模糊,无水印),以便在上传时在 WordPress 中制作模糊图像。问题是,如果上传的文件与设置的大小完全相同或更小,那么 WordPress 将不会生成图像,因此也不会制作模糊图像。
我尝试使用 isst($meta['sizes']['background-image-blurred']['file'])
来确定是否制作了一个,如果没有制作 copy()
源文件,但随后不会为图像生成 WordPress“元数据”(对于非 WordPress 用户,元数据与您想象的不同),因此它会给出高度/宽度使用 wp_get_attachment_image
显示时出现未定义的问题。
所以我确信使用如下所示的 wp_get_attachment_image
Hook 可能是错误的方法。它可能需要在图像上传过程的早期发生。
关于如何最好地让它发挥作用有什么想法吗?
/**
* 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 没有过滤器来强制调整大小,所以您可以做的是在之后 Hook 并调整图像大小(如果未创建)并将其弹出到元数据中。
我没有 imagick,所以你必须自己尝试这些功能,但你有上面的正确过程,你只需要更新文件名并在下面的数组中键入。 PS 不要在过滤器内输出任何东西!
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;
}
更新
这旨在仅捕获现有过滤器无法创建模糊自定义尺寸的地方,否则它什么都不做。你仍然应该包括你原来的过滤器。您可能在原始代码中遇到问题:您正在删除过滤器中的原始文件,这将导致问题,因为有一个名为“_wp_attached_file”的 postmeta 字段需要更新。我在下面附上了一条注释。
过滤器在保存之前捕获元数据,因此一旦您返回 $metadata,任何更改也将被保存。如果您查看源代码:https://core.trac.wordpress.org/browser/tags/3.8.1/src/wp-admin/includes/image.php#L72在这里你可以确切地看到它是如何工作的。我也确认使用wp4.3
我已尝试在下方插入您需要的 imagick 函数。我还没有测试,因为我实际上没有在任何地方安装它。 (imagemagick 实际上是一个很棒的开源程序,但服务器非常密集)。试试用这个函数代替上面的函数:
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; }
更新 为了防止图像拉伸(stretch)较小的图像,用这个替换 imagick 代码。
$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
关于php - WordPress - 上传时模糊图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34377231/