I have written a function to make images autoresized if the width or height are larger than the value of the variable $max_size. It was made to make it possible to upload images directly from a camera without resizing the image in a image editor. The function requires possibility to set ini_set("memory_limit","128M"); on the server.
To use it, you need to add four calls an a function for it to work. In /manager/media/browser/mcpuk/connectors/php/Commands/FileUpload.php add the below function at the end of the file right before the last "?>" line.
function resize($src_name, $max_size)
{
//Get info from file
if(!$info=getimagesize($src_name)) {return false;}
//Allocate more memory for resizing function, this can be limited by your web host
ini_set("memory_limit","128M");
// Load image and get image size
switch ($info['mime'])
{
case 'image/jpeg':
$img=imagecreatefromjpeg($src_name);
break;
case 'image/gif':
$img=imagecreatefromgif($src_name);
break;
case 'image/png':
$img=imagecreatefrompng($src_name);
break;
default:
return false;
}
//Get image size
$width=imagesx($img);
$height=imagesy($img);
// Calculate new size
if (($width>$max_size) or ($height>$max_size))
{
$ratio=($height*100)/$width;
if ($ratio>=100) { //Portrait or square
$new_height=$max_size;
$new_width=($max_size*$ratio)/100;
}
else { //Landscape
$new_width=$max_size;
$new_height=($max_size*$ratio)/100;
}
}
else {return false;} //If image is smaller or equal to the max size, return to program without resizing
// Create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// Copy and resize old image into new image
imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Save resized image into a file
switch ($info['mime'])
{
case 'image/jpeg':
imagejpeg($tmp_img, $src_name, 90); //100 is the quality settings, values range from 0-100.
break;
case 'image/gif':
imagegif($tmp_img, $src_name, 90); //100 is the quality settings, values range from 0-100.
break;
case 'image/png':
imagepng($tmp_img, $src_name, 90); //100 is the quality settings, values range from 0-100.
break;
}
//Clean up temporary images
imagedestroy($img);
imagedestroy($tmp_img); // NOTE: PHP will clean up the temp file it created when the request has completed.
echo '<script language="JavaScript" type="text/javascript">alert("The image has been resized to '.$new_width.'x'.$new_height.'pixels.");</script>';
return true;
}
Next, add
resize(($this->real_cwd."$filename($i).$ext"), 800);
after the line
@chmod(($this->real_cwd."/$filename($i).$ext"),$this->fckphp_config['modx']['file_permissions']); //modified for MODx
around line 140 and then add the same line after the next @chmod line around line 150.
Finally, add
resize(($this->real_cwd."$filename.$ext"), 800);
after the line
@chmod(($this->real_cwd."/$filename.$ext"),$this->fckphp_config['modx']['file_permissions']); //modified for MODx
around line 168 and then add the same line after the next @chmod line around line 176.
The second parameter of the resize function that is set to 800 (pixels) can be changed to a different value to resize images larger to that value to the set value.