Hi,

I have a php form where I can upload 3 things into a database.

1. Image Description
2. Image category
3. Image (the image is uploaded in a folder with a unique random name, and the unique path name is inserted into the database)

My script generate something like : 3a54b42fa7103570ea1729e17fc9a207.jpg
in my picture folder when I hit the form upload button.

What I need now is a thumbnail version of that image created in the same folder but with a different file name and in smaller size , something like :
3a54b42fa7103570ea1729e17fc9a207_T.jpg

Anyone can help me? I kind of new with php,mysql.

My php code is:

<?php

// image directory
$uploadDir = ( $_SERVER['DOCUMENT_ROOT'].'/vitrail_path/photos/' );

if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$description = $_POST['description'];
$categorie = $_POST['categorie'];

    // get the file extension first
	$ext      = substr(strrchr($fileName, "."), 1); 
	
	// generate the random file name
	$randName = md5(rand() * time());
	
	// unique file name for the upload file
    $filePath = $uploadDir . $randName . '.' . $ext;

    // move the files to the specified directory with error reporting
    $result    = move_uploaded_file($tmpName, $filePath);
	if (!$result) {
		echo "Error uploading file";
		exit;
	}
 
  	if (file_exists(( $_SERVER['DOCUMENT_ROOT'].'/vitrail_path/photos/' ) . $_FILES['userfile']['tmp_name']))
      {
      echo $_FILES['userfile']['tmp_name'] . ' already exists. ';
      }

if(!$_POST['description'] | !$_POST['categorie']) {
die('Inscrire une description et une catégorie.');
}

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}
include 'library/config.php';
include 'library/opendb.php';

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

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

include 'library/closedb.php';

echo "<br>File $fileName uploaded<br>";
header('Location: upload.php');
} 

?>

And my form code is

<form method="post" enctype="multipart/form-data" name="uploadform">
<h4>
<table border="0" align = "center">
<tr align="left" valign="top"><td><br></td></tr>
<tr align="left" valign="middle" height="10"><td><b><font size="2" face="Georgia, Arial" color="black">Description de la 

photo :</font></b></td><td height="10">
<input type="text" name="description" maxlength="75" size="75">
</td></tr>
<tr align="left" valign="middle"><td><font size="2" face="Georgia, Arial" color="black"><b>Catégorie :</b></font></td><td>
<select name="categorie">
    <option selected>Animaux</option>
    <option>Bord de mer</option>
    <option>Décorations des fêtes</option>
    <option>Divers</option>
    <option>Fleurs</option>
    <option>Oiseaux</option>
    <option>Victorien</option>
    </select>
</td></tr>
<tr align="left" valign="middle">
  <td>&nbsp;</td>
  <td>&nbsp;</td>
</tr>
<tr align="left" valign="middle">
<td><font size="2" face="Georgia, Arial" color="black"><b>Sélection de la photo :</b></font><br>
  </td>
<td><input type="hidden" name="MAX_FILE_SIZE" value="60000000">
<input name="userfile" type="file" id="userfile" size="25"></td>
</tr>
<tr align="left" valign="middle"><td></td>
<td><br>
  <br><img src="images/transparent.gif" width="363" height="10" border=0><input name="upload" type="submit" class="box" 

id="upload" value=" Ajouter la photo "></td></tr>
</table>
</h4>

</form>
Member Avatar for diafol

You can use the GD2 library or ImageMagick to create a thumbnail on upload. There are loads of ready-made scripts out there.

consider using this function... works gud for me..

<?php
/*
	Function createthumb($name,$filename,$new_w,$new_h)
	creates a resized image
	variables:
	$name		Original filename
	$filename	Filename of the resized image
	$new_w		width of resized image
	$new_h		height of resized image
*/	
function createthumb($name,$filename,$new_w,$new_h)
{
	$system=explode(".",$name);
	if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
	if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
	if (preg_match("/gif/",$system[1])){$src_img=imagecreatefromgif($name);}
	$old_x=imageSX($src_img);
	$old_y=imageSY($src_img);
	if ($old_x > $old_y) 
	{
		$thumb_w=$new_w;
		$thumb_h=$old_y*($new_h/$old_x);
	}
	if ($old_x < $old_y) 
	{
		$thumb_w=$old_x*($new_w/$old_y);
		$thumb_h=$new_h;
	}
	if ($old_x == $old_y) 
	{
		$thumb_w=$new_w;
		$thumb_h=$new_h;
	}
	$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
	imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
	if (preg_match("/png/",$system[1])){
		imagepng($dst_img,$filename); 
                } elseif (preg_match("/gif/",$system[1]))  {
		imagegif($dst_img,$filename);
	} else {
		imagejpeg($dst_img,$filename); 
	}
	imagedestroy($dst_img); 
	imagedestroy($src_img); 
}
?>

Hope it helps

Problem solve, thanks to both of you for suggestion and code.

Member Avatar for diafol

You'll find the 'Mark as Solved' link somewhere.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.