Jump to content

Picture Upload


coolphpdude

Recommended Posts

Hi people!

 

I've got this image upload script which im having a few problems with (please find the code below)

 

Form:

echo "<form enctype='multipart/form-data' method='post' action=uploadpicture.php'>";
echo	"<input type='hidden' name='max_file_size' value='10000000' />";
echo 	"<input name='uploadedfile' type='file'>";

echo "<br>";
echo "<br>";

echo 	"<input type='submit' value='Upload Picture' >";
echo "</form>";

 

Now  this should mean that the max file size of the picture that is to be uploaded should be no larger than 10mb.

 

Code:

$uploadedfile=$_POST["uploadedfile"];


function resampleimage($maxsize, $sourcefile, $destination, $imgcomp=0){
// SET THE IMAGE COMPRESSION
$g_imgcomp=100-$imgcomp;
  // CHECK TO SEE IF THE IMAGE EXISTS FIRST
  if(file_exists($sourcefile)){
  // FIRST WE GET THE CURRENT IMAGE SIZE
  $g_is=getimagesize($sourcefile);
    /********* CALCULATE THE WIDTH AND THE HEIGHT ***************/
    // CHECK TO SEE IF THE WIDTH AND HEIGHT ARE ALREADY SMALLER THAN THE MAX SIZE
    if($g_is[0] <= $maxsize && $g_is[1] <= $maxsize){
    // LEAVE WIDTH AND HEIGHT ALONE IF IMAGE IS SMALLER THAN MAXSIZE
    $new_width=$g_is[0];
    $new_height=$g_is[1];
    } else {
    // GET VALUE TO CALCULATE WIDTH AND HEIGHT
    $w_adjust = ($maxsize / $g_is[0]);
    $h_adjust = ($maxsize / $g_is[1]);
      // CHECK TO WHICH DIMENSION REQUIRES THE SMALLER ADJUSTMENT
      if($w_adjust <= $h_adjust){
      // CALCULATE WIDTH AND HEIGHT IF THE WIDTH VALUE IS SMALLER
      $new_width=($g_is[0]*$w_adjust);
      $new_height=($g_is[1]*$w_adjust);
      } else {
      // CALCULATE WIDTH AND HEIGHT IF THE HEIGHT VALUE IS SMALLER
      $new_width=($g_is[0]*$h_adjust);
      $new_height=($g_is[1]*$h_adjust);
      }
    }
  //SEARCHES IMAGE NAME STRING TO SELECT EXTENSION (EVERYTHING AFTER THE LAST "." )
  $image_type = strrchr($sourcefile, ".");

//changes the file extension from uppercase to lowercase
$lower = strtolower($image_type);

//SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION
switch($lower) {
	case '.jpg':
		$img_src = imagecreatefromjpeg($sourcefile);
		break;
	case '.jpeg':
		$img_src = imagecreatefromjpeg($sourcefile);
		break;
	case '.png':
		$img_src = imagecreatefrompng($sourcefile);
		break;
	case '.gif':
		$img_src = imagecreatefromgif($sourcefile);
		break;
	default:
		echo("Error Invalid Image Type");
		die;
		break;
}
  // CREATE THE TRUE COLOR IMAGE WITH NE WIDTH AND HEIGHT
  $img_dst=imagecreatetruecolor($new_width,$new_height);
  // RESAMPLE THE IMAGE TO NEW WIDTH AND HEIGHT
  imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $new_width, $new_height, $g_is[0], $g_is[1]);
  // OUTPUT THE IMAGE AS A JPEG.
  // THIS CAN BE CHANGED IF YOU WANT TRANSPARENCY OR PREFER ANOTHER FORMAT. MAKE SURE YOU CHANGE HEADER ABOVE.
  imagejpeg($img_dst, $destination, 100);
  // DESTROY THE NEW IMAGE
  imagedestroy($img_dst);
  return true;
  } else {
  return false;
  }
}

// INSERT PICTURE CODE - START
if ((($_FILES["uploadedfile"]["type"] == "image/pjpeg")
|| ($_FILES["uploadedfile"]["type"] == "image/jpeg")
|| ($_FILES["uploadedfile"]["type"] == "image/JPEG")
|| ($_FILES["uploadedfile"]["type"] == "image/png")
|| ($_FILES["uploadedfile"]["type"] == "image/bmp"))
&& ($_FILES["uploadedfile"]["size"] < 10000000))
{
                      echo "file uploaded";
             }
             else
             {
                      echo "file not uploaded";
             }

 

to me this all looks fine and i should be able to upload any picture with most image file types but they must be under 10Mb!! well for some reason im tryin to upload a .jpg thats 2.5mb and it won't work. Any ideas???

 

Cheers

Link to comment
https://forums.phpfreaks.com/topic/150886-picture-upload/
Share on other sites

POST:Array

(

    [max_file_size] => 100000000

)

FILES:Array

(

    [uploadedfile] => Array

        (

            [name] => DSCN0026.JPG

            [type] =>

            [tmp_name] =>

            [error] => 1

            => 0

        )

 

)

 

Link to comment
https://forums.phpfreaks.com/topic/150886-picture-upload/#findComment-792666
Share on other sites

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.