by default, fckeditor will accept all kind of image size we upload, and sometimes the image is too big by width or height. Is there any tricks to prevent this? Let say if the image width is more than 500px, then reduce the width to 500px automatically. Thx in advance.
Wed, 09/24/2008 - 05:10
#1

Re: resize image
Re: resize image
Did you have any luck?
Re: resize image
its probably not the cleanest code but it works for resizing jpegs, pngs and gifs, see below. the part added is in a block marked with a /**...***/:
this is for FCKeditor version 2.6.4
function FileUpload( $resourceType, $currentFolder, $sCommand ) { if (!isset($_FILES)) { global $_FILES; } $sErrorNumber = '0' ; $sFileName = '' ; if ( isset( $_FILES['NewFile'] ) && !is_null( $_FILES['NewFile']['tmp_name'] ) ) { global $Config ; $oFile = $_FILES['NewFile'] ; // Map the virtual path to the local server path. $sServerDir = ServerMapFolder( $resourceType, $currentFolder, $sCommand ) ; // Get the uploaded file name. $sFileName = $oFile['name'] ; $sFileName = SanitizeFileName( $sFileName ) ; $sOriginalFileName = $sFileName ; // Get the extension. $sExtension = substr( $sFileName, ( strrpos($sFileName, '.') + 1 ) ) ; $sExtension = strtolower( $sExtension ) ; if ( isset( $Config['SecureImageUploads'] ) ) { if ( ( $isImageValid = IsImageValid( $oFile['tmp_name'], $sExtension ) ) === false ) { $sErrorNumber = '202' ; } } if ( isset( $Config['HtmlExtensions'] ) ) { if ( !IsHtmlExtension( $sExtension, $Config['HtmlExtensions'] ) && ( $detectHtml = DetectHtml( $oFile['tmp_name'] ) ) === true ) { $sErrorNumber = '202' ; } } // Check if it is an allowed extension. if ( !$sErrorNumber && IsAllowedExt( $sExtension, $resourceType ) ) { $iCounter = 0 ; while ( true ) { $sFilePath = $sServerDir . $sFileName ; if ( is_file( $sFilePath ) ) { $iCounter++ ; $sFileName = RemoveExtension( $sOriginalFileName ) . '(' . $iCounter . ').' . $sExtension ; $sErrorNumber = '201' ; } else { /** resize image if needed ************************************************************************/ $maxWidth = 670; $maxHeight = 670; list($oldWidth, $oldHeight) = getimagesize($oFile['tmp_name']); if(($oldWidth > $maxWidth) || ($oldHeight > $maxHeight)){ //upload file with resizing $imgRatio=$oldWidth/$oldHeight; //calculate the image ratio if ($imgRatio>1){ $newWidth = $maxWidth; $newHeight = $maxWidth/$imgRatio; }else{ $newHeight = $maxWidth; $newWidth = $maxWidth*$imgRatio; } $newImage = imagecreatetruecolor($newWidth, $newHeight); switch ($oFile['type']){ case 'image/gif': $oldImage = imagecreatefromgif($oFile['tmp_name']); // create image object for resizing imagecopyresampled($newImage, $oldImage, 0, 0, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight);//resizing imagegif($newImage, $sFilePath); // copy resized image to destination break; case 'image/jpeg': case 'image/pjpeg': $oldImage = imagecreatefromjpeg($oFile['tmp_name']); imagecopyresampled($newImage, $oldImage, 0, 0, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight); imagejpeg($newImage, $sFilePath); break; case 'image/png': case 'image/x-png': $oldImage = imagecreatefrompng($oFile['tmp_name']); imagecopyresampled($newImage, $oldImage, 0, 0, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight); imagepng($newImage, $sFilePath); break; } imagedestroy($oldImage); // destroy temporary image objects imagedestroy($newImage); } else{ //upload file without resizing move_uploaded_file( $oFile['tmp_name'], $sFilePath ) ; } /***************************************************************************************************/ //move_uploaded_file( $oFile['tmp_name'], $sFilePath ) ; if ( is_file( $sFilePath ) ) { if ( isset( $Config['ChmodOnUpload'] ) && !$Config['ChmodOnUpload'] ) { break ; } $permissions = 0777; if ( isset( $Config['ChmodOnUpload'] ) && $Config['ChmodOnUpload'] ) { $permissions = $Config['ChmodOnUpload'] ; } $oldumask = umask(0) ; chmod( $sFilePath, $permissions ) ; umask( $oldumask ) ; } break ; } } if ( file_exists( $sFilePath ) ) { //previous checks failed, try once again if ( isset( $isImageValid ) && $isImageValid === -1 && IsImageValid( $sFilePath, $sExtension ) === false ) { @unlink( $sFilePath ) ; $sErrorNumber = '202' ; } else if ( isset( $detectHtml ) && $detectHtml === -1 && DetectHtml( $sFilePath ) === true ) { @unlink( $sFilePath ) ; $sErrorNumber = '202' ; } } } else $sErrorNumber = '202' ; } else $sErrorNumber = '202' ; $sFileUrl = CombinePaths( GetResourceTypePath( $resourceType, $sCommand ) , $currentFolder ) ; $sFileUrl = CombinePaths( $sFileUrl, $sFileName ) ; SendUploadResults( $sErrorNumber, $sFileUrl, $sFileName ) ; exit ; }Re: resize image
what if the file isn't an image?
// upload pdf's, xls, doc, etc... // check if the file being uploaded isn't any of these then upload - else do a resize if needed. if($sExtension != 'jpg' || $sExtension != 'jpeg' || $sExtension != 'gif' || $sExtension != 'png' ){ move_uploaded_file( $oFile['tmp_name'], $sFilePath ); }else{ ... // the rest of upload - resize code ... ... //remember to end it }