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


upload and resize and image with PHP

Posted on 2007-10-16
Medium Priority
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?


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

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!";
Question by:roscoeh23
LVL 15

Expert Comment

ID: 20083961
this php class can suite your requirement

Accepted Solution

siva_siva earned 2000 total points
ID: 20083974
// 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");

$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>";

            <title>Resize Image</title>
            <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!">

                        <td><?php echo $filelist ?></td>

Expert Comment

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.

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