upload and resize and image with PHP

I am trying to upload an image to a folder and resize it to 400px by 300px on the way. At the moment it just upload the full size image to my folder. Can anybody help?





$property_id=$_GET['property_id'];

//home many images are in the table
$result = mysql_query("SELECT * FROM images WHERE property_id='$property_id'");
$num_rows = mysql_num_rows($result);


if(!(is_dir('property_images/$property_id')))
{
mkdir("../property_images/$property_id", 0700);

}

//upload the images

if(isset($_POST['_upload'])  && $_FILES['userfile']['size'] > 0)
{




$target_path = "../property_images/$property_id/".basename( $_FILES['userfile']['name']);

$img_url= $property_id."/".basename( $_FILES['userfile']['name']);

if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path)) {
  /*  echo "The file ".  basename( $_FILES['userfile']['name']).
    " has been uploaded";*/
} else{
    echo "There was an error uploading the file, please try again!";
}
roscoeh23Asked:
Who is Participating?
 
siva_sivaCommented:
<?php
// Configuration
$uploaddir = "resize/";

/************************************************************
* Create upload Directory
************************************************************/
if (!is_dir("resize")) {
      if (!mkdir($uploaddir))
            die ("upload_files directory doesn't exist and creation failed");
      if (!chmod($uploaddir,0755))
            die ("change permission to 755 failed.");
}

if(isset($_POST['upload'])) {
      $image_name = $_FILES['file']['tmp_name'];
      $new_side = 100;

      // Get new dimensions
      list($width, $height) = getimagesize($image_name);

      $w = $width;
      $h = $height;

      if ($h > $w) {
            $new_width = ($new_side / $h) * $w;
            $new_height = $new_side;      
      } else {
            $new_height = ($new_side / $w) * $h;
            $new_width = $new_side;
      }

      // Resample
      $image_p = imagecreatetruecolor($new_width, $new_height);
      $image = imagecreatefromjpeg($image_name);
      imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
      $upload_image = $_FILES['file']['name'];
      imagejpeg($image_p, "$uploaddir$upload_image");
}

$handle=opendir($uploaddir);
$filelist = "";
$pattern="(\.jpg$|\.jpeg$|\.gif$)"; //valid image extensions
while ($file = readdir($handle)) {
      if(!is_dir($file) &&!is_link($file)) {
            if(eregi($pattern, $file)) {
                  $filelist .= "<img src='$uploaddir$file'>";
                  $filelist .="<br>";
            }
      }
}
?>

<html>
      <head>
            <title>Resize Image</title>
      </head>
      <body>
            <form action="resize.php" method="post" ENCTYPE="multipart/form-data">
                  File: <input type="file" name="file" size="30">
                  <input type="submit" name="upload" value="Upload!">
            </form>

            <table>
                  <tr>
                        <td><?php echo $filelist ?></td>
                  </tr>
            </table>
      </body>
</html>
0
 
babuno5Commented:
this php class can suite your requirement
http://www.phpclasses.org/browse/package/1476.html
0
 
tbeasley123Commented:
What could be the case if the accepted solution script above and similar like are crashing my server? I am using the code above that "siva siva" entered.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.