Jump to content

[SOLVED] PHP and image uploading


aarchaic

Recommended Posts

I have a script that needs alteration. and since i've never worked with images and php this is a complete nightmare and not really sure how i should approach this.

 

the code is as follows.... ( Found it here http://www.theopensurgery.com/29/php-upload-and-resize-image-script/ )

 

<form action="<?php echo $_SERVER['PHP_SELF'];  ?>" method="post" enctype="multipart/form-data" id="something" class="uniForm">
        <input name="new_image" id="new_image" size="30" type="file" class="fileUpload" />
        <button name="submit" type="submit" class="submitButton">Upload/Resize Image</button>
</form>
<?php
        if(isset($_POST['submit'])){
          if (isset ($_FILES['new_image'])){
	  	  if(!file_exists("images"))
			  {
			  mkdir("images" , "0744") or die("Could not create images folder");
			  die("folder created!");
			  } 
		  $imagename = basename($_FILES['new_image']['name']);
              $source = $_FILES['new_image']['tmp_name'];
              $target = "images/".$imagename;
              move_uploaded_file($source, $target);

              $imagepath = $imagename;
              $save = "images/" . $imagepath; //This is the new file you saving
              $file = "images/" . $imagepath; //This is the original file

              list($width, $height) = getimagesize($file) ; 

              $modwidth = 150; 

              $diff = $width / $modwidth;

              $modheight = $height / $diff; 
              $tn = imagecreatetruecolor($modwidth, $modheight) ; 
              $image = imagecreatefromjpeg($file) ; 
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

              imagejpeg($tn, $save, 100) ; 

              $save = "images/sml_" . $imagepath; //This is the new file you saving
              $file = "images/" . $imagepath; //This is the original file

              list($width, $height) = getimagesize($file) ; 

              $modwidth = 80; 

              $diff = $width / $modwidth;

              $modheight = $height / $diff; 
              $tn = imagecreatetruecolor($modwidth, $modheight) ; 
              $image = imagecreatefromjpeg($file) ; 
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

              imagejpeg($tn, $save, 100) ; 
            echo "Large image: <img src='images/".$imagepath."'><br>"; 
            echo "Thumbnail: <img src='images/sml_".$imagepath."'>"; 

          }
        }
?>

 

 

this is what i need it to do...

 

1. large image of 500 pixels on the long side

2. medium image of 195 pixels on the long side

3. the thumb of 80 pixels on the long side.

 

4 work in an image size checker not larger than 2 meg...

 

thanks for your help

Link to comment
https://forums.phpfreaks.com/topic/160174-solved-php-and-image-uploading/
Share on other sites

no i need help cause i really have no idea how to do it. i've been ok doing sessions cookies and database verification. but with images i have no experiences what so ever if some one can just give me some guide lines to how to it i'll give it a try.

 

So if someone can just help me to get started i'll really appreciate it!

 

 

 

ok after a weekend of struggling i manage to work it out!

 

here is the code...

 

<form action="<?php echo $_SERVER['PHP_SELF'];  ?>" method="post" enctype="multipart/form-data" id="something" class="uniForm">
        <input name="new_image" id="new_image" size="30" type="file" class="fileUpload" />
        <button name="submit" type="submit" class="submitButton">Upload/Resize Image</button>
</form>
<?php
        if(isset($_POST['submit'])){
          if (isset ($_FILES['new_image'])){
	  	  		  $imagename = basename($_FILES['new_image']['name']);
				  $source = $_FILES['new_image']['tmp_name'];
				  $target = "gfx/profilephotos/".$imagename;
				  move_uploaded_file($source, $target);

				  $imagepath = $imagename;
				  $save = "gfx/profilephotos/" . $imagepath; //This is the new file you saving
				  $file = "gfx/profilephotos/" . $imagepath; //This is the original file

				  list($width, $height) = getimagesize($file) ; 

				  $largeimage=500; //sets the longest side
				  
				  $imgratio=$width/$height; //works out the image ratio and if its a portrait or landscape
					if ($imgratio>1){ //tests if it is a landscape
						$newwidth = $largeimage; //sets the longest side for landscape
						$newheight = $largeimage/$imgratio; //calculate the shortest side for landscape
						}else{
								$newheight = $largeimage; //sets the longest side for Portrait
								$newwidth = $largeimage*$imgratio; //calculate the shortest side for Portrait
								}
				  
				  $tn = imagecreatetruecolor($newwidth, $newheight) ; 
				  $image = imagecreatefromjpeg($file) ; 
				  imagecopyresampled($tn, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height) ; 

				  imagejpeg($tn, $save, 100) ; 

				  $save = "gfx/profilephotos/med_" . $imagepath; //This is the new file you saving
				  $file = "gfx/profilephotos/" . $imagepath; //This is the original file

				  list($width, $height) = getimagesize($file) ; 

				  $mediumimage=195;
				  
				  $imgratio=$width/$height;
					if ($imgratio>1){
						$newwidth = $mediumimage;
						$newheight = $mediumimage/$imgratio;
						}else{
								$newheight = $mediumimage;
								$newwidth = $mediumimage*$imgratio;
								}

				  $tn = imagecreatetruecolor($newwidth, $newheight) ; 
				  $image = imagecreatefromjpeg($file) ; 
				  imagecopyresampled($tn, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height) ; 

				  imagejpeg($tn, $save, 100) ; 
				  
				  $save = "gfx/profilephotos/thumb_" . $imagepath; //This is the new file you saving
				  $file = "gfx/profilephotos/" . $imagepath; //This is the original file

				  list($width, $height) = getimagesize($file) ; 

				  $thumbimage=80;
				  
				  $imgratio=$width/$height;
					if ($imgratio>1){
						$newwidth = $thumbimage;
						$newheight = $thumbimage/$imgratio;
						}else{
								$newheight = $thumbimage;
								$newwidth = $thumbimage*$imgratio;
								}

				  $tn = imagecreatetruecolor($newwidth, $newheight) ; 
				  $image = imagecreatefromjpeg($file) ; 
				  imagecopyresampled($tn, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height) ; 

				  imagejpeg($tn, $save, 100) ; 
				  
				echo "Large image: <img src='gfx/profilephotos/".$imagepath."'><br>"; 
				echo "Medium: <img src='gfx/profilephotos/med_".$imagepath."'>";
				echo "Thumbnail: <img src='gfx/profilephotos/thumb_".$imagepath."'>"; 

          }
        }
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.