I am continuing the development of "qa-wysiwyg-upload.php" to resize images if they are too big in their dimensions.
What I have so far is working for JPG/JPEG and might help you already:
// new: check image size, if it is too big resize it
$resizedFlag = false;
$maxImgWidth = 900; // define maximal image width
if (empty($message)) {
$fileImgCheck = getimagesize($file['tmp_name']);
switch ($fileImgCheck['mime']) {
case "image/jpeg":
// create image from file
$src = imagecreatefromjpeg($file['tmp_name']);
// get original size of uploaded image
list($width,$height) = getimagesize($file['tmp_name']);
if($width>$maxImgWidth) {
// resize the image to maxImgWidth, maintain the original aspect ratio
$newwidth = $maxImgWidth;
$newheight=($height/$width)*$newwidth;
$newImage=imagecreatetruecolor($newwidth,$newheight);
// do the image resizing by copying from the original into $newImage image
imagecopyresampled($newImage,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
// write image to buffer and save in variable
ob_start(); // Stdout --> buffer
imagejpeg($newImage,NULL,75); // last parameter is jpg quality, see also http://php.net/manual/en/function.imagejpeg.php
$newImageToSave = ob_get_contents(); // store stdout in $newImageToSave
ob_end_clean(); // clear buffer
// remove images from php buffer
imagedestroy($src);
imagedestroy($newImage);
$resizedFlag = true;
}
break; // END jpeg
default:
// continue, no resizing needed
break;
}
}
If you implement this, you need to change the following line as well, so that it saves the resized file:
from:
$blobid=qa_db_blob_create(file_get_contents($file['tmp_name']), $extension, @$file['name'], $userid, $cookieid, qa_remote_ip_address());
to:
// added: save resized image instead of original
if($resizedFlag) {
$blobid=qa_db_blob_create($newImageToSave, $extension, @$file['name'], $userid, $cookieid, qa_remote_ip_address());
}
else {
$blobid=qa_db_blob_create(file_get_contents($file['tmp_name']), $extension, @$file['name'], $userid, $cookieid, qa_remote_ip_address());
}
Cheers, Kai