Интересно, из этого мона сделать для Joomla мамбот.
/** !! **************************************************
* It is not necessary to modify anything in this file. *
************************************************** !! **/
define('IMAX_VERSION', '1.2');
// The name of the option key that stores the current version
define('IMAX_INSTALLED_VERSION', 'imax_version');
// The name of the option key that contains the max width.
define('IMAX_OPTION_MAX_WIDTH', 'imax_maxwidth');
// The name of the option key for the replace image choice.
define('IMAX_OPTION_PHYSICAL_RESIZE', 'imax_physical_resize');
define('IMAX_OPTION_DONT_FORCE_WIDTH', 'imax_dont_force_width');
/**
imax_size_images
Executes on post save. Makes sure IMG tags have width + height params and are below maxwidth
*/
function imax_size_images($the_content){
$offset = 0;
$the_new_content = '';
// get settings so we don't have to get them a million times in the loop
$force_width = get_option( IMAX_OPTION_DONT_FORCE_WIDTH ) ? 0 : 1;
$max_width = get_option( IMAX_OPTION_MAX_WIDTH );
$physical_resize = get_option( IMAX_OPTION_PHYSICAL_RESIZE ) ? 1 : 0;
$upload_path = attribute_escape(str_replace(ABSPATH, '', get_option('upload_path')));
# COLAS fix for php4, see http://fr2.php.net/function.stripos
$the_lc_content = strtolower($the_content);
while( ( $img_start = strpos( $the_lc_content, '<img', $offset ) ) !== false ){ // it could be the first char so ===
$resize_needed = 0;
$img_end = strpos( $the_content, '>', $img_start );
if( substr( $the_lc_content, $img_end - 1, 1) == '/' ) $img_end--;
//see if IMG tag has existing width/height tags
$height_start = strpos( $the_lc_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 = strpos( $the_lc_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 = strpos( $the_lc_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
$ext = strtolower(substr($src,strrpos($src,".")));
if( $ext == ".jpg" || $ext == ".png" ){ //resizes jpgs & pngs
//$file_name = ABSPATH . UPLOADS . preg_replace('/.*?[\/]files[\/]/', '', $src); // COLAS: WPMU code
$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);
if( $ext == ".jpg" ) {
$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
}
} else { //png
$image = imagecreatefrompng($file_name);
if( @imagecopyresampled($image_new, $image, 0, 0, 0, 0, $width, $height, $real_width, $real_height) ){
imagepng( $image_new, $file_name); //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 <img
}
if( $offset == 0 ){ //never went anywhere - send out old content
return $the_content;
} else {
//tack on the end
$the_new_content .= substr( $the_content, $img_end );
return $the_new_content;
}
}
/**
imax_admin_panel
The option page.
*/
function imax_admin_panel() {
// Proceed with the options panel.
// If the user pushed the update button.
if ( isset($_POST['imax_physical_resize']) ) {
$err = array();
// Set or unset the physical resize option.
if ( !empty($_POST['imax_physical_resize'] ) ) {
update_option(IMAX_OPTION_PHYSICAL_RESIZE, 'yes');
} else {
update_option(IMAX_OPTION_PHYSICAL_RESIZE, '');
}
if ( !empty($_POST['imax_force_width'] ) ) {
update_option(IMAX_OPTION_DONT_FORCE_WIDTH, '');
} else {
update_option(IMAX_OPTION_DONT_FORCE_WIDTH, '1');
}
// Set the max width
update_option(IMAX_OPTION_MAX_WIDTH, $_POST['imax_max_width']);
echo '<div id="message" class="updated fade"><p>Changes Saved!</p></div>';
} // End if update button pushed.
?>
<style>
.imax_box{
padding: 15px;
}
.imax_note{
color:#999;
padding: 3px 0 10px 25px;
display:block;
}
</style>
<div class="wrap" id="imax_options_panel">
<form method='post'>
<h2>Images Max Width Options</h2>
<P>
There are two methods of resizing large inline images to a maximum width. Both methods will place the <em>width</em> & <em>height</em> parameters within all <em><IMG></em> tags (even images smaller than the max width) so that browsers will display the page better on load.
</P>
<div class='imax_box'>
<h3>Permanently Resize Large Images to a Max Width?</h3>
<div class='imax_box'>
<?php if ( get_option(IMAX_OPTION_PHYSICAL_RESIZE) != "" ) { $x2 = 'CHECKED'; } else { $x1 = 'CHECKED'; } ?>
<input type="radio" id="imax_physical_resize" name="imax_physical_resize" <?php echo $x1; ?> value=''/>
<strong>No</strong> // Client-side Resize: Temporarily resize the image in the browser on page download</input>
<div class='imax_note'>The images will remain the original size on the server. Large images may take a long time for the viewer to download. Choose this option if you have no idea what this means.</div>
<BR />
<input type="radio" id="imax_physical_resize2" name="imax_physical_resize" <?php echo $x2; ?> value='1'>
<strong>Yes</strong> // Server-side Resize: Physically resize large images to max width on the server at time of post</input>
<div class='imax_note'>This will produce a better looking image, save space on the server, and will download to viewers browsers faster. The catch is, this overwrites the original image so the full size image is no longer available on the server. </div>
<BR />
</div>
<h3>Image Resize Method</h3>
<div class='imax_box'>
<input type="checkbox" id="imax_force_width" name="imax_force_width" <?php if ( get_option(IMAX_OPTION_DONT_FORCE_WIDTH) == "" ) { echo "CHECKED"; } ?> value='1'>
<strong>Force Width</strong>: Always resize images larger than the max width</input>
<div class='imax_note'>When this is NOT checked images with existing <em>width=</em> & <em>height=</em> params can exceed the max width. By default WP does not insert these tags and it will be resized. Think of this as a Max Width override for special occations.</div>
</div>
<h3>Maximum Image Width</h3>
<input type="text" id='imax_max_width' name='imax_max_width' value='<?php echo get_option(IMAX_OPTION_MAX_WIDTH);?>'/> px
<div class='imax_note'>Leave width empty if you would not like to resize large images.</div>
</div>
<p class="submit">
<input type="submit" name="imax_update" value="Save Settings »"/>
</p>
</form>
</div>
<?php
} // imax_admin_panel
/**
imax_admin_add_panel
Adds the option page.
*/
function imax_admin_add_panel() {
add_options_page('Image Max Width Plugin Settings', 'iMax Width', 8, 'imax_options', 'imax_admin_panel');
}
//hooks and such////////////////////////////////////////////////
if( is_admin()) {
add_filter('content_save_pre', 'imax_size_images');
}
add_action('admin_menu', 'imax_admin_add_panel');
/**
imax_install
Initialize the settings we need to get the cards working
*/
function imax_install() {
if ( get_option(IMAX_INSTALLED_VERSION) < IMAX_VERSION || $_GET['imax_force_install'] == 'true' ) {
update_option(IMAX_INSTALLED_VERSION, IMAX_VERSION);
if ( !get_option(IMAX_OPTION_MAX_WIDTH) ) {
update_option(IMAX_OPTION_MAX_WIDTH, '');
}
if ( !get_option(IMAX_OPTION_PHYSICAL_RESIZE) ) {
update_option(IMAX_OPTION_PHYSICAL_RESIZE, '');
}
if ( !get_option(IMAX_OPTION_DONT_FORCE_WIDTH) ) {
update_option(IMAX_OPTION_DONT_FORCE_WIDTH, '');
}
}
} // imax_install
if( strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false ) {
add_action('init', 'imax_install');
}
?>