我有这个PHP代码:
function ShowFileExtension($filepath)
{
preg_match('/[^?]*/', $filepath, $matches);
$string = $matches[0];
$pattern = preg_split('/\./', $string, -1, PREG_SPLIT_OFFSET_CAPTURE);
if(count($pattern) > 1)
{
$filenamepart = $pattern[count($pattern)-1][0];
preg_match('/[^?]*/', $filenamepart, $matches);
return strtolower($matches[0]);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我有一个名为的文件my.zip,则此函数返回.zip.
我想反过来,我希望函数在my没有扩展名的情况下返回.
该文件只是变量中的一个字符串.
所以我按照这里给出的示例(我修改为仅模糊,没有水印),在上传时在WordPress中制作模糊图像.问题是,如果上传的文件与设置的大小完全相同或更小,那么WordPress将不会生成图像,因此不会产生模糊的图像.
我尝试使用a isst($meta['sizes']['background-image-blurred']['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'], 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' …Run Code Online (Sandbox Code Playgroud)