Sorry? How do you mean " max 300 wide OR 300 heigh"? Do you want it to pick one or the other randomly or something?

    I mean max 300 pixels wide, or max 300 pixels high.

      If the image to be resized is not a square, you will end up with one side either greater than or less than the 300px desired...

      Unless you don't mind it not being a true representation of the original...

        The most important to me is that its width not greater than the value I specify, but it seems here that the height is priority.
        Example:

        Original picture is 1024(w)533(h)
        The output is 576(w)
        300(h)
        I want 300(w)*277(h)

        All values are pixels...

          Looks to me like you will have to do something with the compute ratio line...

          try

          // compute ratio
              $ratio = min($thumb_max_width,$size[0],$thumb_max_height,$size[1]); 
          

          Or

          // compute ratio
              $ratio = min($thumb_max_height,$size[1],$thumb_max_width/$size[0]);  

          Something like that...

            5 days later

            It still doesn't work quite like it should

            I want it to do this.

            Check if height is more than width,
            If height is more than width, then max height is ex 400
            if width is more than height, then max width is ex 400

            the script is now:

            /*********************************************************************
            Resizing images
            *********************************************************************/
            function resizeimage($input_file, $output_file, $thumb_max_width, $thumb_max_height, $thumb_quality) {
            	// read image size 
            	$size = getimagesize($input_file); 
            
            // read the input file 
            $input_image = imagecreatefromjpeg($input_file); 
            
            // compute ratio 
            $ratio = min($thumb_max_width,$size[1],$thumb_max_height/$size[0]); 
            
            // create output image 
            $output_image = ImageCreatefromjpeg($input_file); 
            
            // resample image 
            // when using GD 1.x change this to imagecopyresized 
            $output_image = imagecreatetruecolor(round($size[0] * $ratio),round($size[1] * $ratio)); 
            imagecopyresampled($output_image, $input_image, 0, 0, 0, 0, round($size[0]* $ratio), round($size[1] * $ratio), $size[0], $size[1]); 
            
            
            // create output image 
            imagejpeg($output_image,$output_file,$thumb_quality); 
            imagedestroy($input_image); 
            imagedestroy($output_image); 
            }
            ?>

              I solved the problem myself...

              It probably could be written simpler, but it works.

              function resizeimage($input_file, $output_file, $maxwidth, $maxheight, $quality) {
              	//Read image size
              	$size = getimagesize($input_file);
              	$imgwidth = $size[0];
              	$imgheight = $size[1];
              
              //Read input file
              $input_image = imagecreatefromjpeg($input_file);
              
              //Ratio
              $ratio = $imgheight / $imgwidth;
              
              //check if width is greater then height or vice versa
              //defines new dimensions
              
              //If width is greater than height
              if ($imgwidth > $imgheight) {
              	$ratio = $maxwidth / $imgwidth;
              	$newwidth = $maxwidth;
              	$newheight = round($imgheight * $ratio);
              } 
              //If height is greater than width
              if ($imgwidth < $imgheight) {
              	$ratio = $maxheight / $imgheight;
              	$newwidth = round($imgwidth * $ratio);
              	$newheight = $maxheight;
              }
              //If both height and width is less than max defined
              if ($imgwidth < $maxwidth AND $imgheight < $maxheight) {
              	$newwidth = $imgwidth;
              	$newheight = $imgheight;
              }
              
              //Create output file
              $output_image = imagecreatefromjpeg($input_file);
              
              //Resample image (GD2.0)
              $output_image = imagecreatetruecolor($newwidth, $newheight);
              imagecopyresampled($output_image, $input_image, 0, 0, 0, 0, $newwidth, $newheight, $imgwidth, $imgheight);
              
              //create output image
              imagejpeg($output_image, $output_file, $quality);
              imagedestroy($input_image);
              imagedestroy($output_image);
              }
                a month later

                When I use this function my browser times out trying to create the thumbnail-image. Any idea why?

                  Which version of GD/PHP are u using.
                  imagecreatetruecolor is only working in GD2.0.

                  Bengt

                    Either that or you are trying to resize a huge image on a really slow machine... then it would timeout....

                      Ahh I got it. for some reson not all jpg-files can be resized?!?! Dont know why.

                      Next step is now to resize GIF and PNG

                        Write a Reply...