Jump to content

[SOLVED] Thumbnail form


jaxdevil

Recommended Posts

I am trying to create a form that will upload an image and place one copy in a folder and then a thumbnail version in a folder inside that folder called 'thumbs' It says it is uploaded but only the main image is uploaded, the thumbnail never arrives on the server, even though the script completes and displays the success message. Any ideas? I posted the form first below and the upload and convert code below that.

 

<form action="inventory.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<input type="hidden" name="action" value="Upload">
<input type="hidden" name="image" value="One">
<input type="hidden" name="mod" value="<?=$mod_num?>">
<input type="hidden" name="f_name" value="<? echo time(); ?>">
<table width="300">
<tr>
<td>
Select Image 1:
</td>
<td>
<input name="ufile[]" type="file" id="ufile[]" size="17" />
</td>
</tr>
</table>
<center>
<input type="submit" value="upload">
</center>
</form>

 

<?php mkdir($_SERVER['DOCUMENT_ROOT']."/prod_images/$mod", 0777); ?>
<?php mkdir($_SERVER['DOCUMENT_ROOT']."/prod_images/$mod/thumbs", 0777); ?>
<?
$path1= $_SERVER['DOCUMENT_ROOT']."/prod_images/temp/".$HTTP_POST_FILES['ufile']['name'][0];
copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
chmod("$path1", 0755);
$orig_filename = "$path1";
$new_filename = $_SERVER['DOCUMENT_ROOT']."/prod_images/".$mod."/".$f_name.".jpg";
echo "f_name:".$f_name;
echo "mod:".$mod;
echo "../prod_images/".$mod."/".$f_name.".jpg";
$status = rename($orig_filename, $new_filename) or exit("Could not add the item");
echo "Item added successfully!";
?>
<?php
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
  // open the directory
  $dir = opendir( $pathToImages );

  // loop through it, looking for any/all JPG files:
  while (false !== ($fname = readdir( $dir ))) {
    // parse path for the extension
    $info = pathinfo($pathToImages . $fname);
    // continue only if this is a JPEG image
    if ( strtolower($info['extension']) == 'jpg' )
    {
      // load image and get image size
      $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
      $width = imagesx( $img );
      $height = imagesy( $img );

      // calculate thumbnail size
      $new_width = $thumbWidth;
      $new_height = floor( $height * ( $thumbWidth / $width ) );

      // create a new temporary image
      $tmp_img = imagecreatetruecolor( $new_width, $new_height );

      // copy and resize old image into new image
      imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

      // save thumbnail into a file
      imagejpeg( $tmp_img,"{$pathToThumbs}{$fname}") or die ("Could not create image from JPEG");
    }
  }
  // close the directory
  closedir( $dir );
}
// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
createThumbs("../prod_images/".$mod,"../prod_images/".$mod."/thumbs/","100");
chmod("http://".$_SERVER['HTTP_HOST']."/prod_images/".$mod."/thumbs/".$f_name.".jpg", 0755);
$orig_filename = "http://".$_SERVER['HTTP_HOST']."/prod_images/".$mod."/".$f_name.".jpg";
echo $orig_filename;
$new_filename = "http://".$_SERVER['HTTP_HOST']."/prod_images/".$mod."/thumbs/".$f_name.".jpg";
echo $new_filename;
echo "Full Size Image Added successfully!";
$query = "UPDATE images SET `image_one`='$f_name' WHERE `mod`='$mod'";
mysql_query($query) or die( "An error has ocured: " .mysql_error (). ":" .mysql_errno ());
print "<h1>Inventory Update Successful</h1><br>data<br /><br /><em>Updated!</em><br /><br />";
?>

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.