Jump to content

adding thumbnail creation to upload script


supermerc

Recommended Posts

Im wondering if someone could help me or refer me to something to help me incorporate a way to create a thumbnail to the image the user uploads and store the path of the thumbnail to database.

So basicly I want the orinal image to be stored in the folder and database, which is all already in the code, I also want it to create a thumbnail and save the thumbnail path to database too

this is my code:

[code]<?php
session_start();
include 'artmenu.php';
?>
<form method="post" enctype="multipart/form-data">
  <table width="272" border="0">
  <tr>
    <td width="266"><label>Description:
        <input type="text" name="description" id="description"/>
    </label>
      <br />
      <input name="userfile" type="file" id="userfile" />
          <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
        <br />
      <input name="upload" type="submit" class="box" id="upload" value=" Upload " /></td>
  </tr>
</table>
</form>

<?php
$uploadDir = 'submitedart/';

if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$user= $_SESSION['s_username'];
$description = $_POST['description'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}

include 'config.php';

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$query = "INSERT INTO userart (name, size, type, path, user, description) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$user', '$description')";

mysql_query($query) or die('Error, query failed : ' . mysql_error());

echo "<br>Files uploaded<br>";

}
?>[/code]
This function will resample a jpg
[code]<?php
function resampimagejpg($forcedwidth, $forcedheight, $sourcefile, $destfile, $imgcomp)
  {
  $g_imgcomp=100-$imgcomp;
  $g_srcfile=$sourcefile;
  $g_dstfile=$destfile;
  $g_fw=$forcedwidth;
  $g_fh=$forcedheight;

  if(file_exists($g_srcfile))
      {
      $g_is=getimagesize($g_srcfile);
      if(($g_is[0]-$g_fw)>=($g_is[1]-$g_fh))
          {
          $g_iw=$g_fw;
          $g_ih=($g_fw/$g_is[0])*$g_is[1];
          }
          else
          {
          $g_ih=$g_fh;
          $g_iw=($g_ih/$g_is[1])*$g_is[0];
          }
      $img_src=imagecreatefromjpeg($g_srcfile);
      $img_dst=imagecreatetruecolor($g_iw,$g_ih);
      imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $g_iw, $g_ih, $g_is[0], $g_is[1]);
      imagejpeg($img_dst, $g_dstfile, $g_imgcomp);
      imagedestroy($img_dst);
      return true;
      }
      else
      return false;
  }
?>[/code]

Now you can create the file by calling the function. Just set the $file_path, $thumb_path, and $fiel_name
[code]resampimagejpg(200,200,$file_path/$file_name,"$thumb_path/$file_name",0);[/code]

Ray
If the pictures that are being uploaded were taken with modern digital cameras, these files often contain a thumbnail embedded in the file. Use the function [url=http://www.php.net/exif_thumbnail]exif_thumbnail()[/url] to extract it.

Ken

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.