Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
0 votes
1.6k views
in Plugins by
edited by

As I have seen today, there is no max-height set in qa-wysiwyg-upload.php

Just realized - using the function qa_upload_file_one() there (before I was using my own) - that  max-height is necessary. Get a laptop and click on the image of the lady here: http://www.klaustukai.lt/477/kaip-rengtis-i-pokalbi-del-darbo#a507 (for a default laptop resolution it is too big).

I suggest:
$maxImgWidth = 900; // define maximal image width
$maxImgHeight = 600; // define maximal image height

Or set this optional in qa_opt() and the wysiwyg-plugin options.

---

Important: With the recent code, max-width is set and a max-height parameter would be ignored if max-width is set. That is why we need to check if max-height is also regarded. That is the according resize code of my custom plugin:

 

    case "image/png":
        // create image from file
        $src = imagecreatefrompng($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;
            $resizedFlag = true;
        }
        // consider newheight of resized dimensions, if this height is still bigger than maxHeight, resize to maxHeight
        if( (!$resizedFlag && $height>$maxImgHeight) ||
$newheight>$maxImgHeight) {
            // resize the image to maxImgHeight, maintain the original aspect ratio
            $newheight = $maxImgHeight;
            $newwidth=($width/$height)*$newheight;
            $resizedFlag = true;
        }
 
       // height or width is greater than maxDimensions
        if($resizedFlag) {
            // create new image
            $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,90); // do JPG instead of PNG
            $newImageToSave = ob_get_contents(); // store stdout in $newImageToSave
            ob_end_clean(); // clear buffer
            // remove images from php buffer
            imagedestroy($src);
            imagedestroy($newImage);
        }
        break; // END png

Q2A version: 1.6.2
by
Actually the function to be changed is in qa-util-image.php:
function qa_image_constrain_data()

We have to add "  if($newheight>$maxImgHeight) { ... }" (see code above)
by
And in qa-wysiwyg-upload.php:

    $upload=qa_upload_file_one(
        qa_opt('wysiwyg_editor_upload_max_size'),
        qa_get('qa_only_image') || !qa_opt('wysiwyg_editor_upload_all'),
        qa_get('qa_only_image') ? 900 : null, // max width if it's an image upload
        qa_get('qa_only_image') ? 600 : null // max height if it's an image upload
    );

2 Answers

0 votes
by

The following implementation should actually work, but it does not set the new height. I am still trying to find the reason:

    function qa_image_constrain(&$width, &$height, $maxwidth, $maxheight=null)
/*
    Given and $width and $height, return true if those need to be constrained to fit in $maxwidth x $maxheight.
    If so, also set $width and $height to the new proportionally constrained values.
    If $maxheight is omitted or set to null, assume it to be the same as $maxwidth.
*/
    {
        if (!isset($maxheight))
            $maxheight=$maxwidth;
        
        if (($width>$maxwidth) || ($height>$maxheight)) {
            $multiplier=min($maxwidth/$width, $maxheight/$height);
            // kai: changed var names
            $newwidth=floor($width*$multiplier);
            $newheight=floor($height*$multiplier);


            // kai: added if image height is still > maxheight
            if($newheight > $maxheight) {
                $newheight = $maxheight;
                // set width by maintaining the original aspect ratio
                $newwidth = ($width/$height)*$newheight;
            }            
            $width = $newwidth;
            $height = $newheight;

            
            return true;
        }
        
        return false;
    }
 

0 votes
by

Hi there

Thanks for your detailed solutions.For image resizing,i have tried this code:

namespace RE__Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string fileName = "c:/Sample.png";

            REImage reImage = REFile.OpenImageFile(fileName);

            Image Processing.ApplyResize(reImage, 500, 500);

            REFile.SaveImageFile(reImage, "c:/reimage.png", new PNGEncoder());

But it can not work effectively.What's wrong with my code?Thanks a lot

...