
內容簡介
此外掛受Fly Dynamic Image Resizer啟發,但它提供更複雜的功能,例如為每個圖像提供焦點選擇器、輸出響應性<picture>大小等。
此外掛主要面向開發人員。您需要在您的佈景主題或外掛中使用其功能才能使其運作。
使用此外掛的原因
WordPress會自動為每個上傳的圖像創建許多較小的圖像。
通常您只需要其中的1或2個,這樣會浪費您的服務器空間和資源。
此外,預設情況下從中心生成裁剪的圖像,這在許多情況下可能是問題。
此外掛允許您:
禁用自動生成的圖像尺寸
只動態生成所需的圖像尺寸以及所需的圖像
逐個或全部從此外掛中刪除已生成的圖像尺寸
指定不同的焦點,它在創建圖像的裁剪版本時將處於焦點
禁用大型圖像尺寸閾值,因此大於2560x2560px的圖像不會縮小
其運作方式
如果您禁用現有的自動生成的圖像尺寸,則新上傳的圖像將不會創建其尺寸
您需要在您的代碼中指定圖像所需的尺寸
圖像在首次訪問頁面時動態創建,並存儲在上傳的文件夾中
接下來每次訪問該頁面時都會加載已生成的圖像
功能
bis_get_attachment_image_src( $attachment_id, $size, $crop )
這個功能是參考預設的wp_get_attachment_image_src而來。
如果您以前使用過Fly動態圖像調整大小,則不需要在您的代碼中更改fly_get_attachment_image_src的功能 - 這是一個後備,因此您可以停用Fly動態圖像調整大小,它仍然可以運作。
參數:
attachment_id (integer)(必要)
圖像附件的ID
例如:123 或 get_post_thumbnail_id() 或 get_field('my_image')
size (array)(必要)
一個有寬度和高度的陣列
例如:[ 1920, 1080 ]
crop (boolean/array)(選用)
如果您不想裁剪,只需縮放,請跳過這一步或傳遞 false 或 0,
否則傳遞true或1以使用在管理媒體中選擇的焦點裁剪(預設情況下中心),
或傳遞具有字符串x軸和y軸參數的數組,例如[ 'right', 'bottom' ]
或傳遞具有數字x軸和y軸參數的數組,例如[ 0.5, 0.8 ]
返回值:
array(
'src' => (string) 圖像url,
'width' => (integer) 寬度(像素),
'height' => (integer) 高度(像素)
)
bis_get_attachment_image( $attachment_id, $size, $crop, $attr )
這個功能是參考預設的wp_get_attachment_image而來。
如果您以前使用過Fly動態圖像調整大小,則不需要在您的代碼中更改fly_get_attachment_image的功能 - 這是一個後備,因此您可以停用Fly動態圖像調整大小,它仍然可以運作。
參數:
attachment_id (integer)(必要)
圖像附件的ID
例如:123 或 get_post_thumbnail_id() 或
外掛標籤
開發者團隊
原文外掛簡介
This plugin is ment mostly for developers. You need to use its functions in your theme or plugin to make it works.
This plugin offers functionality like face detection crop, focal point selector for every image, function to output responsive
Why to use this plugin?
WordPress will automatically create a lot of smaller images for every uploaded image.
You mostly need just 1 or 2 of them, so this is a waste of your server space and resources.
Also cropped images are generated from the center by default, which can be a problem many times.
This plugin allows you to:
disable autogenerated image sizes
dynamically generate only needed image sizes for only needed images
delete generated image sizes from this plugin individually or all together
specify different focal point that will be in the focus while creating cropped versions of the image
automatically detect focal point by face detection algorithm
disable big image size threshold, so image bigger than 2560x2560px will be NOT scaled down
How it works
if you disable existing autogenerated image sizes, then newly uploaded images will not create its sizes
you need to specify in your code what size is needed for the image
image is dynamically created on the first visit of the page and is stored in uploads folder
every next visit of that page will just load already generated image
Functions
bis_get_attachment_image_src( $attachment_id, $size, $crop )
Function inspired by default wp_get_attachment_image_src.
If you used Fly Dynamic Image Resizer before, you don’t need to replace fly_get_attachment_image_src functions in your code – there is a fallback, so you can deactivate Fly Dynamic Image Resizer and it will still work.
Parameters:
attachment_id (integer)(required)
The ID of the image attachment
Example: 123 or get_post_thumbnail_id() or get_field('my_image')
size (array)(required)
An array with the width and height
Example: [ 1920, 1080 ]
crop (boolean/integer/array/string)(optional)
Skip this or pass false or 0 if you don’t want to crop, just rescale,
otherwise pass true or 1 to use focal point crop that is selected in admin media (by default center),
or pass array with string x-axis and y-axis parameters like [ 'right', 'bottom' ]
or pass array with numeric x-axis and y-axis parameters like [ 0.5, 0.8 ]
or pass string 'face' to automatically detect face position (can be exhaustive on server resources)
Returns:
array(
'src' => (string) url of the image,
'width' => (integer) width in pixels,
'height' => (integer) height in pixels
)
bis_get_attachment_image( $attachment_id, $size, $crop, $attr )
Function inspired by default wp_get_attachment_image.
If you used Fly Dynamic Image Resizer before, you don’t need to replace fly_get_attachment_image functions in your code – there is a fallback, so you can deactivate Fly Dynamic Image Resizer and it will still work.
Parameters:
attachment_id (integer)(required)
The ID of the image attachment
Example: 123 or get_post_thumbnail_id() or get_field('my_image')
size (array)(required)
An array with the width and height
Example: [ 1920, 1080 ]
crop (boolean/integer/array/string)(optional)
Skip this or pass false or 0 if you don’t want to crop, just rescale,
otherwise pass true or 1 to use focal point crop that is selected in admin media (by default center),
or pass array with string x-axis and y-axis parameters like [ 'right', 'bottom' ]
or pass array with numeric x-axis and y-axis parameters like [ 0.5, 0.8 ]
or pass string 'face' to automatically detect face position (can be exhaustive on server resources)
attr (array)(optional)
An array of attributes
Special attribute retina allows you to automatically generate srcset for @2x retina devices
Example: array( 'retina' => true, 'alt' => 'Custom alt text', 'class' => 'my-class', 'id' => 'my-id' )
Returns:

bis_get_attachment_picture( $attachment_id, $sizes, $attr )
Parameters:
attachment_id (integer)(required)
The ID of the image attachment
Example: 123 or get_post_thumbnail_id() or get_field('my_image')
sizes (array)(required)
An array with the key => value pair
where key means breakpoint
and value is array of width, height, crop and alternative_attachment_id
Example: [ 767 => [ 767, 400, 1, 987 ], 9999 => [ 1200, 500, 1 ] ]
This will generate.
attr (array)(optional)
An array of attributes
Special attribute retina allows you to automatically generate srcset for @2x retina devices
Example: array( 'retina' => true, 'alt' => 'Custom alt text', 'class' => 'my-class', 'id' => 'my-id' )
Example:
To generate perfect fullwidth hero image, that will looks great on 4K devices and also on small phones, but it will load only needed size, you can use:
echo bis_get_attachment_picture(
get_post_thumbnail_id(),
[
375 => [ 375, 500, 1, 987 ],
575 => [ 575, 500, 1, 987 ],
767 => [ 767, 500, 1, 987 ],
991 => [ 991, 500, 1 ],
1199 => [ 1199, 500, 1 ],
1399 => [ 1399, 500, 1 ],
1600 => [ 1600, 500, 1 ],
1920 => [ 1920, 500, 1 ],
2560 => [ 2560, 500, 1 ],
3440 => [ 3440, 500, 1 ],
3840 => [ 3840, 500, 1 ],
]
);
Returns:
There is no fallback for fly_add_image_size function
If you used Fly Dynamic Image Resizer before, you need to remove fly_add_image_size functions from your code.
You can create your own variables for sizes if you need it, like
define( 'MY_CUSTOM_SIZE', [ 1000, 200 ] );
and then just us it inside functions, like:
echo bis_get_attachment_image( get_post_thumbnail_id(), MY_CUSTOM_SIZE );
Support other extensions than JPG, PNG and WEBP
This plugin works by default only with JPG, PNG and WEBP files,
but you can easily allow any other mime types,
just use this code eg. in wp-config.php or in your functions.php
define( 'BIS_ALLOWED_MIME_TYPES', array( 'image/jpeg', 'image/png', 'any_other/mime_type' ) );
Get selected image focal point
Focal point data are stored in the attachement post metas.
There is also custom sanitize function, that you can use.
$focal_point = sanitize_focal_point( get_post_meta( get_post_thumbnail_id(), 'focal_point', true ) );
Returns:
array( 0.5, 0.8 )
which means that focal point is 50% from left and 80% from top
