Jump to content

Thumbnail (2 images) script problem


Mr Chris

Recommended Posts

Hi Guys,

I’ve got some problems with a script that has left me scratching my head:

1) I want to upload a image
2) This image is then saved as a 200 x 200 image in a chmod 777 folder named large
3) A second version of the image is saved as a 100 X 100 image in a chmod 777 folder named small

Now I go to upload the image, but the script does not seem to function properly and no images are saved and re-sized at all and I can’t see why.

Can anyone please advise?

[code]
<?php

if ( $submit ){

$thumb_height = '100';
$thumb_width = '100';

$normal_height = '200';
$normal_width = '200';

$img_name = $_FILES['filename']['name'];
$tempory_location = $_FILES['filename']['tmp_name'];
$thumb_location = 'small/'.$img_name;
$normal_location = 'large/'.$img_name;

move_uploaded_file( $img_name, $tempory_location );
$size=GetImageSize( $tempory_location );

$src_img = ImageCreateFromJPEG( $tempory_location );

// Create thumbnail
    $thumbnail = ImageCreateTrueColor( $thumb_width, $thumb_height );
    ImageCopyResampled( $thumbnail, $src_img, 0, 0, 0, 0, $thumb_width, $thumb_height, $size[0], $size[1] );
    ImageJPEG( $thumbnail, $thumb_location );
    ImageDestroy( $thumbnail );
    
// Create normal
    $normal = ImageCreateTrueColor( $normal_width, $normal_height );
    ImageCopyResampled( $normal, $src_img, 0, 0, 0, 0, $normal_width, $normal_height, $size[0], $size[1] );
    ImageJPEG( $normal, $normal_location );
    ImageDestroy( $normal );
}
else{
?>
<form enctype="multipart/form-data" action="<?php echo $PHP_SELF ?>" method="post">
File to upload: <input name="filename" type="file" /><br />
<input type="submit" name="submit" value="Upload" />
</form>
<?php } ?>
[/code]
Link to comment
Share on other sites

Here's one I use. The resize function in this case, keeps the original aspect ratio, thereby avoiding distortions.

[code]<?PHP
session_start();
header("Cache-control: private");

function Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path) {
    $s_path = trim($s_path);
    $o_path = trim($o_path);
    $save = $s_path . $save;
    $file = $o_path . $file;
    $ext = strtolower(end(explode('.',$save)));
    list($width, $height) = getimagesize($file);
    if(($width>$t_w) OR ($height>$t_h)) {
        $r1 = $t_w/$width;
        $r2 = $t_h/$height;
        if($r1<$r2) {
          $size = $t_w/$width;
        }else{
          $size = $t_h/$height;
        }
    }else{
        $size=1;
    }
    $modwidth = $width * $size;
    $modheight = $height * $size;
    $tn = imagecreatetruecolor($modwidth, $modheight);
    switch ($ext) {
        case 'jpg':
        case 'jpeg':
                    $image = imagecreatefromjpeg($file);
        break;
        case 'gif':
                    $image = imagecreatefromgif($file);
        break;
        case 'png':
                    $image = imagecreatefrompng($file);
        break;
    }
    imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
    imagejpeg($tn, $save, 100);
    return;
}

function write_beg($filename, $data){
  $handle = fopen ($filename, "r");
  $old_content = fread ($handle, filesize ($filename));
  fclose ($handle);
  $final_content = $data.$old_content;
  $handle2 = fopen ($filename, "w");
  $finalwrite = fwrite ($handle2, $final_content);
  fclose ($handle2);
}  


# check to see if form submitted
# if yes, process form variables

# if no, display form

if($_POST["action"] == "Upload Image"){

  # define the constant variables

  $uploadDir = 'images/';    // main picture folder
  $max_height = 600;           // largest height you allowed; 0 means any
  $max_width = 800;            //  largest width you allowed; 0 means any
  $max_file = 2000000;         // set the max file size in bytes
  $image_overwrite = 1;        // 0 means overwite; 1 means new name
  $allowed_type01 = array(
     "image/gif",
     "image/pjpeg",
     "image/jpeg",
     "image/png",  
     "image/x-png",
     "image/jpg");           // add or delete allowed image types
  $do_thumb = 1;               // 1 make thumbnails; 0 means do NOT make
  $thumbDir = "thumbs/";          // thumbnail folder
  $thumb_prefix = "";       // prefix for thumbnails
  $thumb_width = 120;        // max thumb width
  $thumb_height = 120;         // max thumb height
  $flat_file = 1;              // 1 flat file for data; 0 database
  $what_error = array();

  #  get basic info about uploaded file
  $original_name = $_FILES['imagename']['name'];
  $original_tmp = $_FILES['imagename']['tmp_name'];
  $original_size = $_FILES['imagename']['size'];
  $original_type = $_FILES['imagename']['type'];

  #  do some basic error trapping and cleanup
  if($original_size>$max_file) {
     //too large, go back to form
     array_push($what_error, "File Size Exceeds Limit");
  }

  if( $original_size<1) {
     //no file was uploaded
     array_push($what_error, "Please Select A File To Upload");
  }

  if(!in_array($original_type, $allowed_type01)) {
     // wrong file type
     array_push($what_error, "File Type Is Not Allowed");
  }

  if(count($what_error)>0) {
     // send to error page
     $_SESSION["resultset"] = $what_error;
     echo '<meta http-equiv="refresh" content="0;URL=mpg_error.php"> ';
     exit();
  }

  #  check to see if file already exists in the folder
  #  if it exists AND rename option is , create new name
  $does_it = FALSE;
    $original_name = strtolower($original_name);
  if(file_exists($uploadDir . $original_name)) {
     if($image_overwrite == 1) {
       //rename
       while(!$does_it) {
          $add_this_name = rand(1,200);
          $original_name = $add_this_name . $original_name;
          if(!file_exists($uploadDir . $original_name)) {
               $does_it = TRUE;
          }
       }
     }
  }

  #  attempt to MOVE the image to its proper location

  $uploadFile = $uploadDir . $original_name;
  if (move_uploaded_file($_FILES['imagename']['tmp_name'], $uploadFile) ) {
     // continue
  } else {
     // send to error page
     array_push($what_error, "error - unknown cause");
     $_SESSION["resultset"] = $what_error;
     echo '<meta http-equiv="refresh" content="0;URL=mpg_error.php"> ';
     exit();
  }

  #  check to see if image needs resizing and act accordingly

  list($original_width, $original_height) = getimagesize($uploadDir . $original_name);

  if($max_height<$original_height OR $max_width<$original_width) {
     // dimensions are too large - resize
    Resize_Image($original_name,$original_name,$max_width,$max_height,$uploadDir,$uploadDir);
  }

  #  check to see if make thumbnails is true
  #  if yes make and store thumb
  $thumb_name = $thumb_prefix . $original_name;

  if($do_thumb == 1) {
    // make a thumb
    Resize_Image($thumb_name,$original_name,$thumb_width,$thumb_height,$thumbDir,$uploadDir);
  }

echo '<meta http-equiv="refresh" content="0;URL=add_pic.php"> ';

/*
  #  gather and sanitize data

  $new_date = date("m d, Y");
  $artist = stripslashes(trim($_POST['artistname']));
  $image_title = stripslashes(trim($_POST['imagetitle']));
  $image_description = stripslashes(trim($_POST['imagedescript']));

  #  check if using flat file or database

  #  if flat file store data
  # date and time of upload|image path and name|thumbnail path and name|submitter|image title|image description

  if($flat_file == 1) {
    // store data in flat file
    $file = "mpg.dat";
    $lines = file($file);
    $store_line = $new_date . "|" . $uploadDir . $original_name . "|" . $thumbDir . $thumb_name . "|" . $artist . "|" . $image_title;
    $store_line = $store_line . "|" . $image_description . "\n";
    write_beg($file, $store_line);
  }else{
    // store data in database
  }
*/

}else{
?>
<html>
<head>
<title>My Photo Gallery Upload Form</title>
<link rel="stylesheet" type="text/css" href="shared_files/textsize.css">

</head>
<body bgcolor="#ffffff" text="#0000a6" link="#0000a6" vlink="#0000a6" alink="#0000a6">
<?PHP
// include ('shared_files/head00.php');
?>
<div style="position: absolute; top: 50px; left: 300px; width:90%; font-size:10pt; padding:10px;filter:shadow(color:gray);"><div style="HEIGHT:250px; border : solid 2px #0000a0; padding : 4px; WIDTH:380px; OVERFLOW:auto; background-color:#fcfcfc; layer-background-color:#fcfcfc; visibility: visible; ">
<form action="<?php echo $PHP_SELF ?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
Browse a File to Upload: <br>
<input type="file" name="imagename"><br>
Enter a title for the picture<br>
<input type="text" name="imagetitle" size="40" maxlength="80" value=""><br>
Enter your name<br>
<input type="text" name="artistname" size="40" maxlength="80" value=""><br>
Enter a description for the picture<br>
<input type="text" name="imagedescript" size="40" maxlength="250" value=""><br>
<input type="submit" value="Upload Image" name="action"><br><br>
note: <font color="#ff0080">max file size is: 2megs</font>
</div></div>
</body>
</html>
<?PHP
}

?>[/code]


Lite...
Link to comment
Share on other sites

WOW!!

That's awesomely awesome [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /] [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /] [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]

Just one thing. I've been playing with the form you kindly posted to see if it's the kind of thing i'm looking for and rather than post the data to a file I will to post it to mysql database. I've set it up as:

[code]
  }else{
   include("to_connect_to_database_files file for inclusion");
   $query = "INSERT INTO pictures(new_date,uploadDir,original_name,thumbDir,thumb_name,artist,image_title) VALUES
   ('$new_date','$uploadDir','$original_name','$thumbDir','$thumb_name','$artist','$image_title')";

     $link = mysql_connect;
     mysql_select_db($db);
     $result = mysql_query($query) or die('Query failed: ' . mysql_error());
     mysql_close();
  }
[/code]

With my table structure as:

pic_id
new_date
upload_Dir
original_name
thumbDir
thumb_name
artist
image_title

(same as variable names, but with an auto inc pic_id added)

...and also set $flat_file = 0;

But it does not save the data in the database?

Many Thanks

Chris
Link to comment
Share on other sites

Hi Litebearer,

Yep, done that. Could you possibly run your eye over this quickly please

Many Thanks

Chris

[code]
<?PHP

function Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path) {
    $s_path = trim($s_path);
    $o_path = trim($o_path);
    $save = $s_path . $save;
    $file = $o_path . $file;
    $ext = strtolower(end(explode('.',$save)));
    list($width, $height) = getimagesize($file);
    if(($width>$t_w) OR ($height>$t_h)) {
        $r1 = $t_w/$width;
        $r2 = $t_h/$height;
        if($r1<$r2) {
          $size = $t_w/$width;
        }else{
          $size = $t_h/$height;
        }
    }else{
        $size=1;
    }
    $modwidth = $width * $size;
    $modheight = $height * $size;
    $tn = imagecreatetruecolor($modwidth, $modheight);
    switch ($ext) {
        case 'jpg':
        case 'jpeg':
                    $image = imagecreatefromjpeg($file);
        break;
        case 'gif':
                    $image = imagecreatefromgif($file);
        break;
        case 'png':
                    $image = imagecreatefrompng($file);
        break;
    }
    imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
    imagejpeg($tn, $save, 100);
    return;
}

function write_beg($filename, $data){
  $handle = fopen ($filename, "r");
  $old_content = fread ($handle, filesize ($filename));
  fclose ($handle);
  $final_content = $data.$old_content;
  $handle2 = fopen ($filename, "w");
  $finalwrite = fwrite ($handle2, $final_content);
  fclose ($handle2);
}  


# check to see if form submitted
# if yes, process form variables

# if no, display form

if($_POST["action"] == "Upload Image"){

  # define the constant variables

  $uploadDir = 'pics/';    // main picture folder
  $max_height = 200;           // largest height you allowed; 0 means any
  $max_width = 800;            //  largest width you allowed; 0 means any
  $max_file = 2000000;         // set the max file size in bytes
  $image_overwrite = 1;        // 0 means overwite; 1 means new name
  $allowed_type01 = array(
     "image/gif",
     "image/pjpeg",
     "image/jpeg",
     "image/png",  
     "image/x-png",
     "image/jpg");           // add or delete allowed image types
  $do_thumb = 1;               // 1 make thumbnails; 0 means do NOT make
  $thumbDir = "small/";          // thumbnail folder
  $thumb_prefix = "";       // prefix for thumbnails
  $thumb_width = 100;        // max thumb width
  $thumb_height = 300;         // max thumb height
  $flat_file = 0;              // 1 flat file for data; 0 database
  $what_error = array();

  #  get basic info about uploaded file
  $original_name = $_FILES['imagename']['name'];
  $original_tmp = $_FILES['imagename']['tmp_name'];
  $original_size = $_FILES['imagename']['size'];
  $original_type = $_FILES['imagename']['type'];

  #  do some basic error trapping and cleanup
  if($original_size>$max_file) {
     //too large, go back to form
     array_push($what_error, "File Size Exceeds Limit");
  }

  if( $original_size<1) {
     //no file was uploaded
     array_push($what_error, "Please Select A File To Upload");
  }

  if(!in_array($original_type, $allowed_type01)) {
     // wrong file type
     array_push($what_error, "File Type Is Not Allowed");
  }

  if(count($what_error)>0) {
     // send to error page
     $_SESSION["resultset"] = $what_error;
     echo '<meta http-equiv="refresh" content="0;URL=form.php"> ';
     exit();
  }

  #  check to see if file already exists in the folder
  #  if it exists AND rename option is , create new name
  $does_it = FALSE;
    $original_name = strtolower($original_name);
  if(file_exists($uploadDir . $original_name)) {
     if($image_overwrite == 1) {
       //rename
       while(!$does_it) {
          $add_this_name = rand(1,200);
          $original_name = $add_this_name . $original_name;
          if(!file_exists($uploadDir . $original_name)) {
               $does_it = TRUE;
          }
       }
     }
  }

  #  attempt to MOVE the image to its proper location

  $uploadFile = $uploadDir . $original_name;
  if (move_uploaded_file($_FILES['imagename']['tmp_name'], $uploadFile) ) {
     // continue
  } else {
     // send to error page
     array_push($what_error, "error - unknown cause");
     $_SESSION["resultset"] = $what_error;
     echo '<meta http-equiv="refresh" content="0;URL=mpg_error.php"> ';
     exit();
  }

  #  check to see if image needs resizing and act accordingly

  list($original_width, $original_height) = getimagesize($uploadDir . $original_name);

  if($max_height<$original_height OR $max_width<$original_width) {
     // dimensions are too large - resize
    Resize_Image($original_name,$original_name,$max_width,$max_height,$uploadDir,$uploadDir);
  }

  #  check to see if make thumbnails is true
  #  if yes make and store thumb
  $thumb_name = $thumb_prefix . $original_name;

  if($do_thumb == 1) {
    // make a thumb
    Resize_Image($thumb_name,$original_name,$thumb_width,$thumb_height,$thumbDir,$uploadDir);
  }

echo '<meta http-equiv="refresh" content="0;URL=add_pic.php"> ';

  #  gather and sanitize data

  $new_date = date("m d, Y");
  $artist = stripslashes(trim($_POST['artistname']));
  $image_title = stripslashes(trim($_POST['imagetitle']));
  $image_description = stripslashes(trim($_POST['imagedescript']));

  #  check if using flat file or database

  #  if flat file store data
  # date and time of upload|image path and name|thumbnail path and name|submitter|image title|image description

  if($flat_file == 1) {
    // store data in flat file
    $file = "mpg.dat";
    $lines = file($file);
    $store_line = $new_date . "|" . $uploadDir . $original_name . "|" . $thumbDir . $thumb_name . "|" . $artist . "|" . $image_title;
    $store_line = $store_line . "|" . $image_description . "\n";
    write_beg($file, $store_line);
  }else{
   include("../************.ini");
   $query = "INSERT INTO pictures(new_date,uploadDir,original_name,thumbDir,thumb_name,artist,image_title) VALUES
   ('$new_date','$uploadDir','$original_name','$thumbDir','$thumb_name','$artist','$image_title')";

    $link = mysql_connect;
    mysql_select_db($db);
    $result = mysql_query($query) or die('Query failed: ' . mysql_error());
    mysql_close();
  }


}else{
?>
<html>
<head>
<title>My Photo Gallery Upload Form</title>
</head>
<body bgcolor="#ffffff" text="#0000a6" link="#0000a6" vlink="#0000a6" alink="#0000a6">
<form action="<?php echo $PHP_SELF ?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
Browse a File to Upload: <br>
<input type="file" name="imagename"><br>
Enter a title for the picture<br>
<input type="text" name="imagetitle" size="40" maxlength="80" value=""><br>
Enter your name<br>
<input type="text" name="artistname" size="40" maxlength="80" value=""><br>
Enter a description for the picture<br>
<input type="text" name="imagedescript" size="40" maxlength="250" value=""><br>
<input type="submit" value="Upload Image" name="action"><br><br>
note: <font color="#ff0080">max file size is: 2megs</font>
</form>
</body>
</html>
<?PHP
}

?>
[/code]
Link to comment
Share on other sites

Ok, I believe I found the problem. When I originally uploaded the script for you I wasn't sure how or if you were storing the data (name, url etc etc), so I had added a piece of code to send the script back to itself IMMEDIATELY AFTER it made and saved the thumbnail...

Therefore you need to remove or comment out the following line...

[code]
    echo '<meta http-equiv="refresh" content="0;URL=add_pic.php"> ';
[/code]

I think that should do it.

Lite...
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.