14. marts 2006 - 21:04
#3
Jeg har haft brugt denne kode:
<?php
/******************************************\
* *
* CREATE TABLE pictures ( *
* pid int(11) NOT NULL auto_increment, *
* title text, *
* imgdata blob, *
* PRIMARY KEY (pid) *
* ) ENGINE=MyISAM; *
* *
\******************************************/
require("./../connect.php");
function resize( $filename, $newfilename, $maxw, $maxh )
{
$result = false;
$srcim = imagecreatefromjpeg( $filename );
$ow = imagesx( $srcim );
$oh = imagesy( $srcim );
$wscale = $maxw / $ow;
$hscale = $maxh / $oh;
$scale = ( $hscale < $wscale ? $hscale : $wscale );
$nw = round( $ow * $scale, 0 );
$nh = round( $oh * $scale, 0 );
$dstim = imagecreatetruecolor( $nw, $nh );
imagecopyresampled( $dstim, $srcim, 0, 0, 0, 0, $nw, $nh, $ow, $oh );
$result = imagejpeg( $dstim, $newfilename, 85 );
imagedestroy( $dstim );
imagedestroy( $srcim );
return $result;
}
$allowPics = array("jpg","jpeg","jpe");
if ($_POST['completed'] == 1)
{
// Need to add - check for large upload. Otherwise the code
// will just duplicate old file ;-)
// ALSO - note that latest.img must be public write and in a
// live appliaction should be in another (safe!) directory.
if($_FILES['imagefile']['size'] > 1024000)
{
$errmsg = "Too large!";
}
else
{
$extension = strtolower(end(explode(".",$_FILES['imagefile']['name'])));
if(!in_array($extension,$allowPics))
{
$errmsg = "Wrong filetype!";
}
else
{
resize($_FILES['imagefile']['tmp_name'],"latest.img",300,300);
$instr = fopen("latest.img","rb");
$image = addslashes(fread($instr,filesize("latest.img")));
mysql_query ("insert into pictures (title, imgdata) values (\"".$_POST['whatsit']."\", \"".$image."\")") or die(mysql_error());
}
}
}
// Find out about latest image
$gotten = mysql_query("select * from pictures order by pid desc limit 1") or die(mysql_error());
if(mysql_num_rows($gotten)>0)
{
$row = mysql_fetch_assoc($gotten);
$title = htmlspecialchars($row['title']);
$bytes = $row['imgdata'];
}
else
{
$errmsg = "There is no image in the database yet";
$title = "no database image available";
// Put up a picture of our training centre
$instr = fopen("./images.jpg","rb");
$bytes = fread($instr,filesize("./images.jpg"));
}
// If this is the image request, send out the image
if ($_GET['gim'] == 1)
{
header("Content-type: image/jpeg");
print $bytes;
exit ();
}
?>
<html>
<head>
<title>Upload an image to a database</title>
<body bgcolor=white><h2>Here's the latest picture</h2>
<font color=red><?= $errmsg ?></font>
<center><img src=?gim=1><br>
<b><?= $title ?></center>
<hr>
<h2>Please upload a new picture and title</h2>
<form enctype="multipart/form-data" method="post">
<input type="hidden" name="completed" value="1">
Please choose an image to upload: <input type="file" name="imagefile"><br>
Please enter the title of that picture: <input name="whatsit"><br>
then: <input type="submit"></form><br>
</body>
</html>