This is to upload pictube in DB and resize if size is bigger then 800x600(you can change it or delete)
<?php
mysql_connect($dbHost, $dbUser, $dbPas) or
die("Could not connect: " . mysql_error());
mysql_select_db($dbName);
elseif ($_POST)
{
$userfile = $filetoupload;
$image = fread(fopen($userfile, "r"), filesize($userfile));
//-------------------------------
//Set a maximum height and width
$width = 800;
$height = 600;
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($userfile);
if ($width && ($width_orig < $height_orig)) {
$width = ($height / $height_orig) * $width_orig;
} else {
$height = ($width / $width_orig) * $height_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromstring($image);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
ob_start();
imagejpeg($image_p, '', 100);
$image_string = ob_get_contents();
ob_end_clean();
$image_string = addslashes($image_string);
$query = "INSERT into filebin (BBinary, width, height) VALUES ('$image_string', '$width, '$height')";
$result = mysql_query($query);
//-------------------------------
}
else
{
//begin of else
?>
<html><head><title></title>
<meta http-equiv='Cover-Type' ; charset=windows-1251'>
</head>
<form name="upload" method="post">
<input type="file" name="filetoupload">
<INPUT name="submit" type=submit value="Send"></p>
</form>
</TABLE>
</FORM>
<?
}
to retrive
<img src='../imagescript.php?imgid=$ImgID&width=117&height=161'>
imagescript.php
<?php
mysql_connect($dbHost, $dbUser, $dbPas) or
die("Could not connect: " . mysql_error());
mysql_select_db($dbName);
global $HTTP_POST_VARS;
$imgid = "$_GET[imgid]";
$new_width = "$_GET[width]";
$new_height = "$_GET[height]";
header('Content-type: image/jpeg');
$query = "SELECT BBinary, width, heightfrom filebin where BId='$imgid'";
$result = mysql_query($query);
list ($file, $width, $height) = mysql_fetch_row($result);
$img = imagecreatefromstring($file);
$img_p = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($img_p, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
imagejpeg($img_p, null, 100);
?>