Fckeditor: Image resize on Upload

by Noman Muhammad

Fckeditor (Html text editor) v-2.6.4 does not have automatic image resizing feature on upload or at least i don't know :( whether this editor has this feature. So to be able to resize picture on upload via fckeditor i bring some changes on "commands.php" file of fckeditor files. This file located on '\editor\filemanager\connectors\php' of the fckeditor folder. Open this file and

1. go to line # 219. It looks like


move_uploaded_file( $oFile['tmp_name'], $sFilePath ) ;

This line is under the function 'FileUpload'

2. Now change the line to

if($sExtension == 'jpg' || $sExtension == 'jpeg' || $sExtension == 'gif' )
{
$uploadedfile = $_FILES['NewFile']['tmp_name'];

// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);

// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);

// For my purposes, I have resized the image to be
// 300 pixels wide, and maintain the original aspect
// ratio. This prevents the image from being "stretched"
// or "squashed". If you prefer some max width other than
// 300, simply change the $newwidth variable
$newwidth=300;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);

// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

// now write the resized image to disk. I have assumed that you want the
// resized, uploaded image file to reside in the ./images subdirectory.
$filename = $_FILES['NewFile']['tmp_name'];
imagejpeg($tmp,$filename,100);
move_uploaded_file( $filename, $sFilePath ) ;
imagedestroy($src);
imagedestroy($tmp);
}
else
{
move_uploaded_file( $oFile['tmp_name'], $sFilePath ) ;
}

3. thats all.

It works for me and hope will for U :)

This entry was posted on Sunday, May 03, 2009 and is filed under , , . You can leave a response and follow any responses to this entry through the Subscribe to: Post Comments (Atom) .

14 comments

oasisfleeting  

your if statement throws an error because there are too many OR clauses. || ||

Anonymous  

Cool! It's work!
Thanks!

Anonymous  

Super! Thank you.

Anonymous  

Hi,

I tried ur code in my fckeditor. Sometimes it works, sometimes not.

I don't know why, but I got some images with black background and some images are totally black, nothing, just black image.

Could u tell me why?

Thanks a lot!

Dandy

@Dandy, can u plz send me ur code. It works fine for me as well as for many folks.

Anonymous  

hi Noman,

i have solved the problem already with this code:

$background = imagecolorallocate($tmp, 255, 255, 255);
imagecolortransparent($tmp, $background);
imagefilledrectangle($tmp, 0, 0, $newwidth,$newheight, $background);

Hi, in your code you do not consider that an Image could be little than 300px.

This is my patch:

$newwidth=300;
if ($width < $newwidth) $newwidth = $width;

Two problems:

You are accepting GIF files, but then you are using imagejpeg() and it's not working.

Also, if the image is smaller than 300 pixels, you are enlarging it. Not good for a lot of reasons, including loss of image quality.

That said, thanks for showing me the correct place to put some code.

Here is an alternative, but it only works if you have Image Magick installed on the server:

if (in_array($sExtension, array('png', 'gif', 'jpeg', 'jpg'))) {
exec('convert '.$oFile['tmp_name'].' -resize 600x600\\> '.$sFilePath);
} else {
move_uploaded_file( $oFile['tmp_name'], $sFilePath);
}

Features:
- Less verbose
- Accepts JPEG, GIF, and PNG
- Doesn't enlarge small images
- Accepts a height maximum as well as a width maximum

@Matt - your script didn't work for me, even though I do have Image Magick installed. Do I maybe need to include a file first for it to work?

I replaced the following with your code.

move_uploaded_file( $oFile['tmp_name'], $sFilePath ) ;

Maybe I'm missing something?

I have a solution to get the original script to resize gif as well as jpg:

// change line 6 to this
if($sExtension == "jpg" || $sExtension == "jpeg"){
$src = imagecreatefromjpeg($uploadedfile);
}elseif($sExtension == "gif"){
$src = imagecreatefromgif($uploadedfile);
}


// change line 27 to this
if($sExtension == "jpg" || $sExtension == "jpeg"){
imagejpeg($tmp,$filename,100);
}elseif($sExtension == "gif"){
imagegif($tmp,$filename,100);
}

Anonymous  

great! = ) thanks!