pHP上传图片变模糊,php – WordPress – 上传的模糊图像

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

我没有想象力,所以你必须自己尝试这些功能,但是你有正确的过程,你只需要更新文件名并在下面的数组中输入. 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字段将需要更新.我已经在这里添加了一个注释.

>过滤器在保存之前捕获元数据,所以任何更改也将在返回$元数据后保存.如果你看源码: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', '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;

}

更新

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

$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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值