Last active
September 15, 2021 09:16
-
-
Save naokazuterada/c0f0a6e9bec6d83e3f2af3d49046f742 to your computer and use it in GitHub Desktop.
WordPressでアップロードされる画像関係のカスタマイズ方法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// デフォルトのリサイズを無効化 | |
function disable_image_sizes( $sizes ) { | |
unset( $sizes['thumbnail'] ); | |
unset( $sizes['medium'] ); | |
unset( $sizes['large'] ); | |
unset( $sizes['medium_large'] ); | |
unset( $sizes['1536x1536'] ); | |
unset( $sizes['2048x2048'] ); | |
return $sizes; | |
} | |
add_filter( 'intermediate_image_sizes_advanced', 'disable_image_sizes' ); | |
// 画像名に"-scaled"が付与されるもの | |
// - サイズ変更 | |
function change_big_image_size_threshold( $threshold ) { | |
return 2048; | |
} | |
add_filter('big_image_size_threshold', 'change_big_image_size_threshold', 999, 1); | |
// - 無効化 | |
// add_filter( 'big_image_size_threshold', '__return_false' ); | |
// サムネイルサイズの追加 | |
// add_image_size('hoge', 400, 300, true); | |
// 大きすぎる(big_image_size_threshold以上の)画像は容量削減のため、リサイズ後自動削除 | |
function txt_domain_delete_fullsize_image($metadata) { | |
// Dump to /wp-content/debug.log | |
// ob_start(); | |
// var_dump( $metadata ); | |
// $test = ob_get_contents(); | |
// ob_end_clean(); | |
// error_log( $test ); | |
$upload_dir = wp_upload_dir(); | |
$full_image_path = trailingslashit($upload_dir['basedir']) . $metadata['file']; | |
$original_image_path = preg_replace('/\-scaled\./', '.', $full_image_path); | |
if ($full_image_path !== $original_image_path) { | |
// 縮小されていたらもとの画像はより大きいサイズなので削除 | |
unlink($original_image_path); | |
unset( $metadata['original_image'] ); | |
} | |
return $metadata; | |
} | |
add_filter('wp_generate_attachment_metadata', 'txt_domain_delete_fullsize_image'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment