Jump to content

Creating thumbnails


supermerc

Recommended Posts

Hey,

I have a code for people to upload pictures on my site,

[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]

But I want to also create a thumbnail and save it into a folder called userthumbs and save only the thumb nail path to the database.

If anyone knows how to do so please help me.
Link to comment
https://forums.phpfreaks.com/topic/32337-creating-thumbnails/
Share on other sites

I just found this code the other day. Works great. It is written to create thumbnails for an entire directory. Maybe you could modify it for your purposes. I did not see the author's name or any restrictions on its use. Place it in your regular picture directory. Running it will create a thumbnail with the prefix "tn_" for each image and place it in a directory within the regular picture directory called "thumbs". You'd just have to change the name in this line of code to reflect whatever you want your thumb directory called:

createthumb($p,"thumbs/tn_".$p,150,150);

I don't keep thumb info in my database. Just info for the full-sized images. When I want to work with a thumb I start with the filename of the full-sized image, like this:

$ImageFile_thumb = "tn_" . $ImageFileName;


Also I could not get this to work without making the thumb directory permissions 777.

Here is the code:


<?php


$imagefolder='.';
$thumbsfolder='.';
$pics=directory($imagefolder,"jpg,JPG,JPEG,jpeg,png,PNG");
$pics=ditchtn($pics,"tn_");
if ($pics[0]!="")
{
foreach ($pics as $p)
{
createthumb($p,"thumbs/tn_".$p,150,150);
}
}



//************************** D I T C H  T N *****************************
// filters out thumbnails

function ditchtn($arr,$thumbname)
{
foreach ($arr as $item)
{
if (!preg_match("/^".$thumbname."/",$item)){$tmparr[]=$item;}
}
return $tmparr;
}

// ************************ C R E A T E  T H U M B **********************
// 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);}
$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);
}

else
{
 
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}

//*********************** D I R E C T O R Y *****************************
//  reads the content of $directory, takes the files that apply to $filter
// and returns an array of the filenames.
//  You can specify which files to read, for example
//  $files = directory(".","jpg,gif");
//  gets all jpg and gif files in this directory.
//  $files = directory(".","all");
//  gets all files.


function directory($dir,$filters)
{
$handle=opendir($dir);
$files=array();
if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] = $file;}}
if ($filters != "all")
{
$filters=explode(",",$filters);
while (($file = readdir($handle))!==false)
{
for ($f=0;$f<sizeof($filters);$f++):
$system=explode(".",$file);
if ($system[1] == $filters[$f]){$files[] = $file;}
endfor;
}
}
closedir($handle);
return $files;
}
?>
Link to comment
https://forums.phpfreaks.com/topic/32337-creating-thumbnails/#findComment-150264
Share on other sites

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.