I have a script which upload image to the server and resize them when uploading))
the script give a max width=200px also max height=200px))for i uploaded image..
i would like if some one can help me modify it,in such away that if Only width must be Max=200px but for height ratio,whichever even up to 500px depending with the image uploaded..
below is the code..

//make sure this directory is writable!
		$path_thumbs ='c:/wamp/www/thumb';
		
		//the new width of the resized image, in pixels.
		$img_thumb_width = 200; // 

		$extlimit = "yes"; //Limit allowed extensions? (no for all extensions allowed)
		//List of allowed extensions if extlimit = yes
		$limitedext = array(".gif",".jpg",".png",".JPG",".JPEG",".jpeg",".bmp");
		
		//the image -> variables
	    $file_type = $_FILES['vImage']['type'];
        $file_name = $_FILES['vImage']['name'];
        $file_size = $_FILES['vImage']['size'];
        $file_tmp = $_FILES['vImage']['tmp_name'];

        //check if you have selected a file.
        if(!is_uploaded_file($file_tmp)){
           echo "Error: Make sure Your Image is not More 2MB before You Upload!. <br><a href=profilephoto.php>back</a>";
           exit(); //exit the script and don't process the rest of it!
        }
       //check the file's extension
       $ext = strrchr($file_name,'.');
       $ext = strtolower($ext);
       //uh-oh! the file extension is not allowed!
       if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
          echo "Wrong file extension.  <br>--<a href=profilephoto.php>back</a>";
          exit();
       }
       //so, whats the file's extension?
       $getExt = explode ('.', $file_name);
       $file_ext = $getExt[count($getExt)-1];

       //create a random file name
       $rand_name = md5(time());
       $rand_name= rand(0,999999999);
       //the new width variable
       $ThumbWidth = $img_thumb_width;

	   //////////////////////////
	   // CREATE THE THUMBNAIL //
	   //////////////////////////
	   
       //keep image type
       if($file_size){
          if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
               $new_img = imagecreatefromjpeg($file_tmp);
           }elseif($file_type == "image/x-png" || $file_type == "image/png"){
               $new_img = imagecreatefrompng($file_tmp);
           }elseif($file_type == "image/gif"){
               $new_img = imagecreatefromgif($file_tmp);
           }
           //list the width and height and keep the height ratio.
           list($width, $height) = getimagesize($file_tmp);
           //calculate the image ratio
           $imgratio=$width/$height;   //From here is where i want the modification to Go))))
            if ($imgratio>1){
              $newwidth = $ThumbWidth;
              $newheight = $ThumbWidth/$imgratio;
           }else{
                 $newheight = $ThumbWidth;
                 $newwidth = $ThumbWidth*$imgratio;
           }
           //function for resize image.
           if (function_exists(imagecreatetruecolor)){
           $resized_img = imagecreatetruecolor($newwidth,$newheight);
           }else{
                 die("Error: Please make sure you have GD library ver 2+");
           }
           //the resizing is going on here!
           imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
           //finally, save the image
           ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
           ImageDestroy ($resized_img);
           ImageDestroy ($new_img);
           
           
        }

        //ok copy the finished file to the thumbnail directory
		move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");

Recommended Answers

All 4 Replies

Do you mean like this:

//make sure this directory is writable!
		$path_thumbs ='c:/wamp/www/thumb';
		
		//the new width of the resized image, in pixels.
		$img_thumb_width = 200; // 

		$extlimit = "yes"; //Limit allowed extensions? (no for all extensions allowed)
		//List of allowed extensions if extlimit = yes
		$limitedext = array(".gif",".jpg",".png",".JPG",".JPEG",".jpeg",".bmp");
		
		//the image -> variables
	    $file_type = $_FILES['vImage']['type'];
        $file_name = $_FILES['vImage']['name'];
        $file_size = $_FILES['vImage']['size'];
        $file_tmp = $_FILES['vImage']['tmp_name'];

        //check if you have selected a file.
        if(!is_uploaded_file($file_tmp)){
           echo "Error: Make sure Your Image is not More 2MB before You Upload!. <br><a href=profilephoto.php>back</a>";
           exit(); //exit the script and don't process the rest of it!
        }
       //check the file's extension
       $ext = strrchr($file_name,'.');
       $ext = strtolower($ext);
       //uh-oh! the file extension is not allowed!
       if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
          echo "Wrong file extension.  <br>--<a href=profilephoto.php>back</a>";
          exit();
       }
       //so, whats the file's extension?
       $getExt = explode ('.', $file_name);
       $file_ext = $getExt[count($getExt)-1];

       //create a random file name
       $rand_name = md5(time());
       $rand_name= rand(0,999999999);
       //the new width variable
       $ThumbWidth = $img_thumb_width;

	   //////////////////////////
	   // CREATE THE THUMBNAIL //
	   //////////////////////////
	   
       //keep image type
       if($file_size){
          if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
               $new_img = imagecreatefromjpeg($file_tmp);
           }elseif($file_type == "image/x-png" || $file_type == "image/png"){
               $new_img = imagecreatefrompng($file_tmp);
           }elseif($file_type == "image/gif"){
               $new_img = imagecreatefromgif($file_tmp);
           }
           //list the width and height and keep the height ratio.
           list($width, $height) = getimagesize($file_tmp);
           //calculate the image ratio
           if ($width>$height) {
               $ratio=$height/$width;
               $newwidth=200;
               $newheight=$width*$ratio;
               } else {
               $ratio=$width/$height;
               $newheight=200;
               $newwidth=$height*$ratio;
               }           
           //function for resize image.
           if (function_exists(imagecreatetruecolor)){
           $resized_img = imagecreatetruecolor($newwidth,$newheight);
           }else{
                 die("Error: Please make sure you have GD library ver 2+");
           }
           //the resizing is going on here!
           imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
           //finally, save the image
           ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
           ImageDestroy ($resized_img);
           ImageDestroy ($new_img);
           
           
        }

        //ok copy the finished file to the thumbnail directory
		move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");

@cwarn23. I Tasted the code You modified with four different type of image,two of them when the image is larger than the height and other two when height is greater.
it gave me the following Results.
1.it was having width=400 which resulted to 200
height=267 which resulted to 267
2.t was having width=400 which resulted to 200
height=300 which resulted to 300
3.t was having width=300 which resulted to 300
height=400 which resulted to 200
4.t was having width=319 which resulted to 319
height=480 which resulted to 200
What i need is that...
for example in No1.becoz Width is greater than height then it should give width which do not exceed 200px and a height which is less than the width as the original photo.
Also in No3.
Becoz height is Greater than width then it should give a width which donot exceed 200px and a height which is Greater than the Width.
As the original image height is greater than the width.
something to note here is that only Width has Got limitation of 200px but height can even exceed it or be less than that.
I hope You Can help me After this Explanation)))
I real appreciate for ur reply)))
thankx))

I just tested the script and found out that 2 variable names I placed were incorrect. So replace the end bit with the following:

list($width, $height) = getimagesize($file_tmp);
           //calculate the image ratio
           if ($width>$height) {
               $ratio=$height/$width;
               $newwidth=200;
               $newheight=$newwidth*$ratio;
               } else {
               $ratio=$width/$height;
               $newheight=200;
               $newwidth=$newheight*$ratio;
               }           
           //function for resize image.
           if (function_exists(imagecreatetruecolor)){
           $resized_img = imagecreatetruecolor($newwidth,$newheight);
           }else{
                 die("Error: Please make sure you have GD library ver 2+");
           }
           //the resizing is going on here!
           imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
           //finally, save the image
           ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
           ImageDestroy ($resized_img);
           ImageDestroy ($new_img);
 
 
        }
 
        //ok copy the finished file to the thumbnail directory
		move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");

Thankx Alot,its working as i wanted))
have a niceday))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.