', $img_start );
if( substr( $the_content, $img_end - 1, 1) == '/' ) $img_end--;
//see if IMG tag has existing width/height tags
$height_start = stripos( $the_content, 'height=', $img_start );
if( $height_start && ( $height_start < $img_end ) ){
$height_end = strpos( $the_content, ' ', $height_start );
//could have been against > or there could be no ' '
if( $height_end > $img_end || $height_end == 0 ) $height_end = $img_end;
//skips over height= and removes possible "" ''
$height = trim( substr( $the_content, $height_start + 7, $height_end - ( $height_start + 7 ) ), "\x22\x27\x5C" );
} else {
$height = '';
$height_start = $img_end; //so we know to place new tag at the end of everything
$height_end = $img_end;
}
// do the same thing for width tag
$width_start = stripos( $the_content, 'width=', $img_start );
if( $width_start && ( $width_start < $img_end ) ){
$width_end = strpos( $the_content, ' ', $width_start );
//could have been against >
if( $width_end > $img_end || $width_end == 0 ) $width_end = $img_end;
$width = trim( substr( $the_content, $width_start + 6, $width_end - ( $width_start + 6 ) ), "\x22\x27\x5C" );
} else {
$width = '';
$width_start = $img_end;
$width_end = $img_end;
}
//if existing W/H and if FORCE WIDTH then check size.
if($force_width && $width && height){
//if bigger than its supposed to be flag for resize. don't do it if we don't have a max_width
if( $max_width && ($width > $max_width)){
$resize_needed = 1;
}
} else if ($width && $height){ // W/H exists and we're not forcing width
//no resize needed
} else { //else has no existing W/H tag flag it!
$resize_needed = 1;
}
//do resize
if($resize_needed){
//check image file to get size
//get filename
$src_start = stripos( $the_content, 'src=', $img_start );
if( $src_start && ( $src_start < $img_end ) ){
$src_end = strpos( $the_content, ' ', $src_start );
//could have been against >
if( $src_end > $img_end || $src_end == 0 ) $src_end = $img_end;
//skips over height= and removes possible "" ''
$src = trim( substr( $the_content, $src_start + 4, $src_end - ( $src_start + 4 ) ), "\x22\x27\x5C" );
} else {
$src = ''; //oh no oh my! impossible!
}
// error surpression is slow | i'm open to suggestions, needs to work on urls too
if( $src && (list($real_width, $real_height) = @getimagesize($src)) ){
//use a user inputted width if they have it
$width = ($width == '') ? $real_width : $width;
if($max_width && $width > $max_width ){ // the width needs to be resized down to max_width
$width = $max_width;
}
//keep things proportional
$height = round(($width * $real_height) / $real_width);
//resize on server if requested and resize to max size | this would skip a file that a user put a small width tag in
if( $physical_resize && ( $width == $max_width ) && ( $width != $real_width ) ){
if( ($path_start = strpos( $src, $upload_path ) ) !== 0){ //see if it's from the upload folder or sub folders
if( strtolower(substr($src,strrpos($src,"."))) == ".jpg" ){ //can only resize jpgs
$file_name = ABSPATH.substr( $src, $path_start );
if( file_exists( $file_name ) ){ //make sure it is really on our server
$image_new = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($file_name);
if( @imagecopyresampled($image_new, $image, 0, 0, 0, 0, $width, $height, $real_width, $real_height) ){
imagejpeg( $image_new, $file_name, 80 ); //overwrite only if resize completed
}
}
}
}
}
//put in new img width height tags
//find out which tag comes first so the stuff in the middle doesn't end up in a black hole
if($width_start == $height_start){
$the_new_content .= substr( $the_content, $offset, $height_start - $offset ).' width=\"'.$width.'\" height=\"'.$height.'\"';
} else if($height_start > $width_start){ //width comes first
$the_new_content .= substr( $the_content, $offset, $width_start - $offset ).'width=\"'.$width.'\"';
if($height_start == $width_end){ //no width tag need ''
$the_new_content .= ' ';
} else { //possible inbetween at least a space ' '
$the_new_content .= substr( $the_content, $width_end, $height_start - $width_end);
}
$the_new_content .= 'height=\"'.$height.'\"';
//possible end stuff
$the_new_content .= substr( $the_content, $height_end, $img_end - $height_end);
} else { //height comes first
$the_new_content .= substr( $the_content, $offset, $height_start - $offset ).'height=\"'.$height.'\"';
if($width_start == $height_end){ //no width tag need ''
$the_new_content .= ' ';
} else { //possible inbetween at least a space ' '
$the_new_content .= substr( $the_content, $height_end, $width_start - $height_end);
}
$the_new_content .= 'width=\"'.$width.'\"';
//possible end stuff
$the_new_content .= substr( $the_content, $width_end, $img_end - $width_end);
}
} else { // no src scary! lets skip it
// we should consider removing this all together as it could break a template even if it is missing.
$the_new_content .= substr( $the_content, $offset, $img_end - $offset );
}
} else { // no resize copy over old stuff
$the_new_content .= substr( $the_content, $offset, $img_end - $offset );
}
$offset = $img_end; //on to look for the next
Changes Saved!
'; } // End if update button pushed. ?>