aruns4u;10924450 wrote:could any one clarify above statements???i am new to php and i want to show the uploaded images such that when i click'em, a small thumbnail of the same is generated(which is created in real-time) which on click shows the full sized version of the image
thanks in advance
I use this because my host provider has a file count limit and i like to keep the numbers down.
<?php
// //WORKING EXAMPLE HERE: [url]http://tmgraphics.biz/test/base64TEST/[/url]
/*
basically i am going to create a DB with 4 columns
- location of image
- date pof image
- blob thumb of image sm
- extra field for alt sizes
then i will have a sub routine that calls on a thumb based on the image full url location
sunroutine checks the date modified and either create, recreates or serves the base 64 data from the DB
*/
// //db pass log in once
mysql_connect("localhost", "USERNAME", "PASS") or die(mysql_error());
mysql_select_db("DATABASE") or die(mysql_error());
function random_string($length)
{
$key = '';
$keys = array_merge(range(0, 9) , range('a', 'z'));
for ($i = 0; $i < $length; $i++)
{
$key.= $keys[array_rand($keys) ];
}
return $key;
}
// /////START thumbDB FUNCTION
function dbthumb($loc, $sz)
{
$fullpath = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
$fullpath = explode(basename($_SERVER['SCRIPT_NAME']) , $fullpath);
$fullpath = $fullpath[0] . $loc;
$create_new = false;
$curr_mod_date = filemtime($loc);
$curr_mod_date = date("l dS \of F Y h:i:s A", $curr_mod_date);
// //checks exist thumb in DB base on location
$result = mysql_query("SELECT loc, mod_date, basedata, addinfo FROM thumbDB WHERE `loc` = '$fullpath'");
if (!$result)
{
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
if ($row != "")
{
$base64 = $row[2];
// /thumb as base64
}
else
{
$create_new = true;
}
if ($row[1] != $curr_mod_date || $create_new = true)
{
// // create or recreate thumb //imagick
$output = "thumb_" . random_string(13) . ".png";
// random string can be any number
exec("convert -thumbnail x300 $loc $output");
$imagedata = file_get_contents($output);
$base64 = base64_encode($imagedata);
// /store in db
unlink($output);
$addinfo = "";
mysql_query("INSERT INTO thumbDB (loc, mod_date, basedata, addinfo) VALUES ('$fullpath', '$curr_mod_date', '$base64', '$addinfo' ) ON DUPLICATE KEY UPDATE
loc = '$fullpath', basedata = '$base64', mod_date = '$curr_mod_date', addinfo = '$addinfo'") or die(mysql_error());
}
// // serves thumb as base 64
return "<img src=\"data:image/jpg;base64," . $base64 . "\" " . $sz . " >";
}
// /////END thumbDB FUNCTION
// ///USAGE
$loc = "brand.jpg";
$sz = "height = \"100\"";
// height = \"100\" width = \"100\" ///this can be anything dealing with size /// this is just display size / and can range from 10-300 max ///thue thumb is created at 300
echo "<a href = \"" . $loc . "\">" . dbthumb($loc, $sz) . "</a>";
?>
...your welcome