Jump to content

generate thumbnails after upload


mallen

Recommended Posts

I am using this tutorial http://icant.co.uk/articles/phpthumbnails/ to generate thumbnail images. I got it working but wanted to improve on it. Currently if I want to run it, I have to open the page in the browser. Is there a way to call this page after I upload the images with another page? This way thumbnails are generated as soon as they are uploaded.

Link to comment
Share on other sites

I got it to work with a few changes. It uploads the files, renames the file and stores the thumbnails. Now I want to change the name of the thumbnail. Such as add a prefix to it like th_12345.jpg

 

<?php

define ("MAX_SIZE","50"); 

function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}

$errors=0;
//checks if the form has been submitted
if(isset($_POST['Submit'])) 
{

$image=$_FILES['image']['name'];
//if it is not empty
if ($image) 
{

$filename = stripslashes($_FILES['image']['name']);

$extension = getExtension($filename);
$extension = strtolower($extension);

if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
{
echo 'Unknown file extension! Only .gif, .jpg, and .png files are allowed to be uploaded </a>.';
$errors=1;
die ();
}

$size=filesize($_FILES['image']['tmp_name']);

if ($size > MAX_SIZE*1024)
{
echo 'You have exceeded the size limit!';
$errors=1;
die ();
}

$image_name=time().'.'.$extension;

$newname="uploads/images/".$image_name;

$copied = copy($_FILES['image']['tmp_name'], $newname);

if (!$copied) 
{
echo 'Copy unsuccessfull!';
$errors=1;
}

$query = "UPDATE users SET title='{$_POST['title']}' , description='{$_POST ['description']}' , images='{$image_name}', address='{$_POST ['address']}', price='{$_POST ['price']}', contact='{$_POST ['contact']}', phone='{$_POST ['phone']}' WHERE user_id='{$userdata['user_id']}'"; 

// Execute the query.
if (mysql_query ($query)) {
print '<p>Your property entry has been added.</p>';

} else {
print "<p>Could not add the entry because: <b>" . mysql_error() . "</b>. The query was $query.</p>";
}

mysql_close();

} 
}


if(isset($_POST['Submit']) && !$errors) 
{
echo "File Uploaded Successfully.";
echo($query); 
//*********Begin to generate thumbnails****************************************************************

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

      // 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}" );
    }
  }
  // close the directory
  closedir( $dir );
}
createThumbs("uploads/images/","uploads/images/thumbnails/",50);
}
?>

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.