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
https://forums.phpfreaks.com/topic/71181-generate-thumbnails-after-upload/
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);
}
?>

Archived

This topic is now archived and is closed to further replies.

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