Jump to content

Need Help - Thumbnail Generation


selliott

Recommended Posts

Hey Everyone.  I need to modify this code to upload the photo and create a thumbnail of that image in the thumbnails folder and was wondering of someone could help.  I first tried to just make it place the same image in both folders, but I don't know what I'm doing. lol.  It only follows the first upload command (either one places the image where it should, when it's first) and ignores the second...but I actually want it to generate a resized thumbnail anyway. 

 

<?php

if ($_FILES['Filedata']['name']) {

//-------Upload photo
$uploadDir = getcwd() . "/../photos" . $_REQUEST['id'] . "/";
$uploadPhoto = $uploadDir . basename($_FILES['Filedata']['name']);

    move_uploaded_file($_FILES['Filedata']['tmp_name'], $uploadPhoto);

//-------Upload thumbnail
$uploadDir = getcwd() . "/../photos" . $_REQUEST['id'] . "/" . "thumbnails" . "/";
$uploadThumbnail = $uploadDir . basename($_FILES['Filedata']['name']);

    move_uploaded_file($_FILES['Filedata']['tmp_name'], $uploadThumbnail);

//--------define connectivity information
$database = "dbname";
$table = "photos" . $_REQUEST['id'];
$user = "dbuser";
$password = "dbpassword";
//-------connect to database
mysql_connect("mysql15.siteprotect.com", $user, $password) or die("Could not connect to database");
mysql_select_db($database) or die("Could not select database");

$photo = $table . "/" . basename($_FILES['Filedata']['name']);   // name of the photo as it appears in db

$thumbnail = $table . "/" . "thumbnails" . "/" . basename($_FILES['Filedata']['name']);   // name of the thumbnail as it appears in db

$query = "INSERT INTO $table VALUES ( '', '$photo', '', '$thumbnail' )";       // add name of photo to database

$myQuery = mysql_query($query);

}


?>

Link to comment
Share on other sites

Hi try this. All you have to do is make a folder named THUMBS and IMAGES. Then,

 

thumbs.php

 

<?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' )

    {

      echo "Creating thumbnail for {$fname} <br />";

 

      // 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 ) );

 

    $new_width = $thumbWidth;

      $new_height = "100";

 

      // create a new tempopary 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}" );

    }

  }

  // close the directory

  closedir( $dir );

}

 

function createGallery( $pathToImages, $pathToThumbs )

{

  echo "Creating gallery.html <br />";

 

  $output = "<html>";

  $output .= "<head><title>Thumbnails</title></head>";

  $output .= "<body bgcolor=\"#000000\">";

  $output .= "<table align=\"center\" cellspacing=\"0\" cellpadding=\"2\" width=\"500\">";

  $output .= "<tr>";

 

  // open the directory

  $dir = opendir( $pathToThumbs );

 

  $counter = 0;

  // loop through the directory

  while (false !== ($fname = readdir($dir)))

  {

    // strip the . and .. entries out

    if ($fname != '.' && $fname != '..')

    {

      $output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname}\">";

      $output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"0\" />";

      $output .= "</a></td>";

 

      $counter += 1;

      if ( $counter % 5 == 0 ) { $output .= "</tr><tr>"; }

    }

  }

  // close the directory

  closedir( $dir );

 

  $output .= "</tr>";

  $output .= "</table>";

  $output .= "</body>";

  $output .= "</html>";

 

  // open the file

  $fhandle = fopen( "gallery.html", "w" );

  // write the contents of the $output variable to the file

  fwrite( $fhandle, $output );

  // close the file

  fclose( $fhandle );

}

createThumbs("images/","thumbs/",100);

createGallery("images/","thumbs/");

?>

 

* Note: the thumbs.php, images and thumbs folder located in same directory. Hope it work... ;)

Link to comment
Share on other sites

Is it possible to place this inside my existing upload script somehow (so it processes automatically, without needing to use the separate upload function through the other page)?  My admin/upload area is Flash based, and it executes this upload.php file to place the image in the photos folder (which a flash photo gallery is setup to pull from).  I've modified the scripts to create a "thumbnails" folder inside the "photos" folder, but don't really know enough yet to change all the multiple scripts in order to change the folder names to "images" and "thumbs".

Link to comment
Share on other sites

By "run thumbs.php", do you mean the photos have to be uploaded, then the user will have to go to the thumbs.php url to run the script?  I need it to be as easy as possible for the person I'm building this for, so all they have to do is:

 

click Browse, upload the pic, then click the upload button and be done. 

 

This is how it currently works, so when I add the thumbnail system, it needs to work the same.

Link to comment
Share on other sites

ok thats what you mean. after browsing the images it will stored automatically on the dbase or folder when the user click upload.  :)

 

as what ive said in your directory you created 2 folders namely thumbs and images.

 

upload.php

 

<?php
$filesize = $_FILES['userfile']['size']; 
$filetype = $_FILES['userfile']['type']; 
$nameuse = filename;
IF (($filesize <= 102000) AND ($filetype == 'image/pjpeg') OR ($filetype == 'image/jpeg'))
{

$nameuse = $_FILES['userfile']['name'];
//$nameuse  = gmdate("YmdHis");
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.
$uploaddir = 'your_directory\images\';
$uploadfile = $uploaddir . $nameuse;
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) 
	{

   		echo "File is valid, and was successfully uploaded.\n";
	} 
else 
	{
   		echo "Possible file upload attack!";
	}
}
IF (($filetype != 'image/pjpeg') AND ($filetype != 'image/jpeg'))
	{
   		echo "Your Image is the wrong file type, it must be a jpg image<BR>your image is $filetype";
	}
IF ($filesize > 102000)
{
echo "Your Image is to large to upload";
}
?> 


<form method="POST" enctype="multipart/form-data" action="upload.php">
<p>
<input type="file" name="userfile" size="20">
<input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2">
</p>
</form>

 

Then add the script of thumbs.php indside of this codes. put a link or re-direct the user to the gallery.html after uploading the images. :)

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.