Page 1 of 1

Help with resizing mantaining aspect ratio with gd

Posted: Sat Nov 13, 2004 1:33 pm
by France79
Hi guys, I am new there... I hope you can help me with one question...

I upload an image from a form and change its name with a random number, what i would like to do now, is:

- Resize maintaining aspect ratio, saving resized image with that random name in /img
and original one in /hires_img

Can you help me doin it? I attached my original code, hope it can helps you to make you loose less time.

I tried some samples from image copy resize class but they didn't work...

In my server I can only use Gd library.

Thank you my friends.

France

Code: Select all

//**********************************************************************//
//  $_FILES['filetoupload']  is the value of                            //
// file field from the form. <input type="file" name="filetoupload">    //
//**********************************************************************//
srand ((double) microtime( )*1000000);
$random_number = rand(100000,1000000 );
// this is the upload dir where files will go.

$upload_dir = "img/";   
$size_bytes = 1048576; //bytes  will be uploaded
$limit_file_type = "yes"; 

// specify file types.
$allowed_file_type = array('image/jpg'
                        );
						$allowed_file_type = array(
                           'image/pjpeg',
                           'image/jpeg',
                        'image/jpg');

          //check if the directory exists or not.
          if (!is_dir("$upload_dir")) {
	     die ("The directory <b>($upload_dir)</b> doesn't exist");
          }
          //check if the directory is writable.
          if (!is_writeable("$upload_dir")){
             die ("The directory <b>($upload_dir)</b> is NOT writable, Please Chmod (777)");
          }

//Check first if a file has been selected
//is_uploaded_file('filename') returns true if
//a file was uploaded via HTTP POST. Returns false otherwise.
if (is_uploaded_file($_FILES['filetoupload']['tmp_name']))
{//begin of is_uploaded_file

         //Get the Size of the File
         $size = $_FILES['filetoupload']['size'];
         //Make sure that $size is less than 1MB (1000000 bytes)
         if ($size > $size_bytes)
         {
         echo"<script language=Javascript>
alert ("Il file รจ troppo grande, deve essere massimo <b>$size_bytes</b> bytes")</script>";
             exit();
         }
              //checks file type
         if (($limit_file_type == "yes") && (!in_array($_FILES['filetoupload']['type'],$allowed_file_type)))
         {
             echo"<script language=Javascript>
alert ("Il file deve essere un''immagine JPG non progressiva, della dimensione massima di 10mb")</script>";
         }

         // $filename will hold the value of the file name submetted from the form.
         $filename =  $_FILES['filetoupload']['name'];
		 $filenamedefinitivo = "img_$random_number.jpg";
         // Check if file is Already EXISTS.
         if(file_exists($upload_dir.$filenamedefinitivo)){
             $filenamedefinitivo = "img_$random_number.jpg";
         }

         //Move the File to the Directory of your choice
         //move_uploaded_file('filename','destination') Moves afile to a new location.
		 $fronte ="fronte";
		 $retro ="retro";
         if ((move_uploaded_file($_FILES['filetoupload']['tmp_name'],$upload_dir.$filenamedefinitivo)) &&
		     $immaginefronteretro == $fronte)
		  {

//make some actions
   }
		 else{

//make some other actions
}
Weirdan | France, please use

Code: Select all

tags when posting php code[/color]

Posted: Sun Nov 14, 2004 4:13 am
by kettle_drum
Check out my file upload class in the code snippet section of the forums as it shoudl help you. Or use this code ive been working on - since im writing an image manipulation class:

Code: Select all

function resize($width, $height){
		$new_image = imagecreatetruecolor($width, $height);		#creates a new image
		imagecopyresized($new_image, $original_image, 0, 0, 0, 0, $width, $height, $original_image_width, $original_image_height);	#copies the image
	}
	
	function resize2width($width){
		$height=(int)(($width*$original_image_height)/$original_image_width);		#work out correct height value
		$this->resize($width, $height);															#resize the image
	}

	function resize2height($height){
		$width=(int)(($height*$original_image_width)/$original_image_height);		#work out correct width value
		$this->resize($width, $height);															#resize the image
	}
	
	function resize2percent($percent){
		$width = (int)($original_image_width/100)*$percent;							#work out correct width/height values for the % change
		$height = (int)($original_image_height/100)*$percent;
		$this->resize($width, $height);														#resize the image
	}
Have edited it a bit so that it doesnt use my class variables - but im sure you get the idea.