Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

upload and resize and image with PHP

Posted on 2007-10-16
3
Medium Priority
?
720 Views
Last Modified: 2013-12-13
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!";
}
0
Comment
Question by:roscoeh23
3 Comments
 
LVL 15

Expert Comment

by:babuno5
ID: 20083961
this php class can suite your requirement
http://www.phpclasses.org/browse/package/1476.html
0
 
LVL 5

Accepted Solution

by:
siva_siva earned 2000 total points
ID: 20083974
<?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
 

Expert Comment

by:tbeasley123
ID: 21060838
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question