問題描述
情況是這樣的:我使用視頻縮略圖插件來自動獲取並設置 YouTube /Vimeo 縮略圖作為功能特徵圖像。問題是,默認的 youtube /vimeo 縮略圖大小隻是比我的主題主要內容寬度小一點。
所以我需要的是擴大規模。如果我去媒體庫,我可以手動編輯每個圖像,然後設置我的確切寬度和 WordPress 的尺寸正確 (我不介意質量有點差) 。那麼每次上傳圖片時,WP 會自動執行哪種操作?
這是我定義的圖像大小:add_image_size('post-full', 688, 320, true); Vimeo 大拇指尺寸是 640×320 。
最佳解決方案
您可以使用本機 Wordpress image_resize 功能來放大圖像。 WordPress 提供了一個名為”image_resize_dimensions” 的鈎子,您可以使用它來覆蓋默認裁剪設置。這是一個修改的功能,將支持擴展:
function image_crop_dimensions($default, $orig_w, $orig_h, $new_w, $new_h, $crop){
if ( !$crop ) return null; // let the wordpress default function handle this
$aspect_ratio = $orig_w / $orig_h;
$size_ratio = max($new_w / $orig_w, $new_h / $orig_h);
$crop_w = round($new_w / $size_ratio);
$crop_h = round($new_h / $size_ratio);
$s_x = floor( ($orig_w - $crop_w) / 2 );
$s_y = floor( ($orig_h - $crop_h) / 2 );
return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );
}
現在鈎這個功能像這樣:
add_filter('image_resize_dimensions', 'image_crop_dimensions', 10, 6);
完成之後,您可以使用 image_resize 功能根據需要向上或向下縮放圖像。
$cropped_image = image_resize($image_filepath, $width, $height, true);
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。