Last active
August 29, 2015 14:06
-
-
Save yao3060/4fd7b4c0dfcb2fad50dd to your computer and use it in GitHub Desktop.
Reduces size of images in posts using bfi_thumb
This file contains 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 | |
require_once('BFI_Thumb.php'); | |
add_filter( 'the_content', 'itc_the_content_img_filter', 999 ); | |
/** | |
* Reduces size of images in posts using bfi_thumb | |
* | |
*/ | |
function itc_the_content_img_filter( $content ) { | |
/* Add here the width you prefer (max width of your posts) */ | |
$max_width = 600; | |
preg_match_all("{<img.*src=\"(.*?)\".*width=\"([0-9]*)\"\s*height=\"([0-9]*)\".*/>}",$content, $images); | |
foreach($images[1] as $key => $image){ | |
$new = $images[0][$key]; | |
if($images[2][$key] > $max_width){ | |
$params = array('width' => $max_width); | |
$new = str_replace('src="'.$image.'"', 'src="'.bfi_thumb( $image, $params ).'"', $new); | |
$new = str_replace('width="'.$images[2][$key].'"', 'width="'.$max_width.'"', $new); | |
$new_height = round($images[3][$key]*$max_width/$images[2][$key]); | |
$new = str_replace('height="'.$images[3][$key].'"', 'height="'.$new_height.'"', $new); | |
} else { | |
$params = array('width' => $images[2][$key]); | |
$new = str_replace('src="'.$image.'"', 'src="'.bfi_thumb( $image, $params ).'"', $new); | |
} | |
$content = str_replace($images[0][$key], $new ,$content); | |
} | |
return $content; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment