Hi,
I am trying to upload an image, create a new file based on that image
and then store the base64 encoded image data in a database.
I dont really know where my code is going wrong so I will just display
it below:
The following code contains the script to process the uploaded file,
the form to upload the file and then a few links to view the file from
a php script called simpleimageviewer.php:
<?php
//this file includes the function, 'dbconn()', that allows you to
//connect to the database
include('functions.php');
?>
<html>
<body>
<?php
//
if (isset($_GET['action'])) {
if (isset($_GET['action']) == "add") {
$tmpfilesize = $_FILES['imgfile']['size'];
$tmpfilename = $_FILES['imgfile']['tmp_name'];
$tmpfiletype = $_FILES['imgfile']['type'];
if ($tmpfilesize > 0) {
if (substr($tmpfiletype, 0, 6) == 'image/') {
//create image from uploaded image
switch ($tmpfiletype)
{
case "image/jpeg":
case "image/pjpeg":
$img = imagecreatefromjpeg($tmpfilename);
break;
case "image/gif":
$img = imagecreatefromgif($tmpfilename);
break;
case "image/png":
$img = imagecreatefrompng($tmpfilename);
break;
}
//create new file
$newfile = tempnam("/tmp", "img_");
//create jpeg and save as newfile
imagejpeg($img, $newfile, 80);
//open new file
$nf = fopen($newfile, "r");
//get contents of opened file and store in var
$filecontents = fread($nf, filesize($newfile));
//destroy file
fclose($nf);
unlink($newfile);
if ($filecontents) {
$insert = "INSERT INTO imglib (imgid, imgdata, imgtype) VALUES
(null, '" . base64_encode($filecontents) . "', '" . $imgtype . "')";
$result = mysql_query($insert, $db);
if ($result) {
echo "<p>Result!</p>";
}else{
echo "<p>No, no, no ... it didnt work</p>";
}
}
} else {
echo "<p>Please upload an image - wrong file type (" .
$tmpfiletype . ")</p>";
}
}else{
echo "<p>File not uploaded</p>";
}
}
}
?>
<form enctype="multipart/form-data" name="" method="post"
action="simple.php?action=add">
<input type="hidden" name="MAX_FILE_SIZE" value="5000000"><!-- approx.
5MB -->
<input name="imgfile" type="file"><br />
<input name="submit" type="submit" value="Submit">
</form>
<?php
$select = "SELECT * FROM imglib";
$result = mysql_query($select, $db);
if (mysql_num_rows($result) > 0) {
echo "<ul>\n";
while ($row = mysql_fetch_array($result)) {
$imgid = $row['imgid'];
echo " <li><a href=\"simpleimageviewer.php?imgid=" . $imgid .
"\">Image " . $imgid . "</a></li>\n";
}
echo "</ul>";
}
?></body>
</html>
Sorry about the mass of code but I thought I should just provide it
all. I also at some point need to resize the uploaded image so I seemed
the best idea to create a temporary file that I could play around with
before writing.
The problem is that when I go to view the image using the following
script (simpleimageviewer.php):
include('functions.php');
//IMAGE OUTPUT
$select = "SELECT imgdata, imgtype FROM imglib WHERE imgid = " .
$_GET['imgid'];
$result = mysql_query($select, $db);
while ($row = mysql_fetch_array($result)) {
$imgdata = $row['imgdata'];
$imgtype = $row['imgtype'];
}
//header
header("Content-type: " . $imagetype);
echo base64_decode($imgdata);
flush();
It doesnt display all the image. See
http://www.martynbissett.co.uk/test/...er.php?imgid=5 to
see what I mean. When I highlight the image it does show a lot of white
area that appears to have been a problem when creating the initial
image from the uploaded one.
Can anyone tell from my code what might be the problem, cheers
Burnsy