Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+1 vote
1.5k views
in Q2A Core by

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

by
I actually added some more stuff now. Resizing if >maxWidth or >maxHeight. Converting to JPG if PNG is too big, etc. If somebody is interested, I can publish more on that.
by
edited by
Hi Kai,
Worked like a charm!
I am very interested in your latest implementations. Thank you in advance.
by
I wonder if you could consider updating this post with your new code as you mentioned above.

Please log in or register to answer this question.

...