Images and local and net
Hi all
and want to read the data out and display the image.
Now heres the problem, I can get images to read in and out on the Delphi
application ok. And I have code which will read and write images from a
table via PHP on the net using the MySQL table.
But the images will not work if I upload them via the Delphi application. On
both the paradox table and the MySQL table I am using a blob field.
Below I have shown the code used on both the Delphi program and the PHP code
so you can see what's going on.
Sorry about cross posting this message but its both a Delphi and PHP
problem, any help in any way would be great
Brian
DELPHI PROCEDURES TO ASSIGN THE IMAGES TO THE TABLE AND GET THEM FROM THE
TABLE
procedure TMain.ChangeImageBtnClick(Sender: TObject);
begin
AssignImageTable(Data.gigs.FieldByName('poster'),P oster);
end;
================================================== ===========
procedure AssignImageTable(table:TField; Image:Timage);
var
JPEG: TJPEGImage;
JPEGStream: TBlobStream;
begin
If Main.OpenPictureDialog.Execute then
begin
try
JPEG := TJPEGImage.Create;
JPEG.LoadFromFile(Main.OpenPictureDialog1.FileName );
Image.Picture.Assign(JPEG);
try
try
JPEGStream := TBlobStream.Create(TBlobField(table),bmWrite);
JPEG.SaveToStream(JPEGStream);
except
On E: Exception do
ShowMessage(E.Message);
end;
finally
JPEGStream.Free;
JPEGStream := nil;
end;
finally
JPEG.Free;
JPEG := nil;
end;
end;
end;
================================================== ===========
procedure TData. DataChange(Sender: TObject; Field: TField);
begin
GetImageFromDB(Data.gigs.FieldByName('poster'), Main.poster);
end;
================================================== ===========
procedure GetImageFromDB(dbfield:TField; Image:Timage);
var memStream: TMemoryStream;
JPEG: TJPEGImage;
begin
If not dbfield.IsNull then
begin
try
JPEG := TJPEGImage.Create;
memStream := TMemoryStream.Create;
TBlobField(dbfield).SaveToStream(memStream);
memStream.Seek(0,0);
JPEG.LoadFromStream(memStream);
Image.Picture.Assign(JPEG);
finally
JPEG.Free;
memStream.Free;
JPEG := nil;
memStream := nil;
end;
end else begin
Image.Picture.LoadFromFile(AppDir+'noimage.bmp');
end;
end;
================================================== ===========
PHP CODE THAT ASSIGNED AND SAVES IMAGES TO MYSQL TABLE
Insert the image into a MySQL table via PHP
<form method="post" action="insert.php" enctype="multipart/form-data">
<h1>Upload an Image File</h1>
<h3>Please fill in the details below to upload your file.
Fields shown in <font color="red">red</font> are mandatory.</h3>
<table>
<col span="1" align="right">
<tr>
<td><font color="red">Short description:</font></td>
<td><input type="text" name="short" size=50></td>
</tr>
<tr>
<td><font color="red">File:</font></td>
<td><input name="userfile" type="file"></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
<input type="hidden" name="MAX_FILE_SIZE" value="30000">
</form>
<h3>Click <a href="index.php">here</a> to browse the images
instead.</h3>
</body>
</html>
<?php
}
else
{
$short = clean($short, 50);
$userfile = clean($userfile, 50);
if (!($connection = @ mysql_pconnect($hostName,
$username,
$password)))
showerror();
if (!mysql_select_db("files", $connection))
showerror();
// Was a file uploaded?
if (is_uploaded_file($userfile))
{
switch ($userfile_type)
{
case "image/gif";
$mimeName = "GIF Image";
break;
case "image/jpeg";
$mimeName = "JPEG Image";
break;
case "image/png";
$mimeName = "PNG Image";
break;
case "image/x-MS-bmp";
$mimeName = "Windows Bitmap";
break;
default:
$mimeName = "Unknown image type";
}
// Open the uploaded file
$file = fopen($userfile, "r");
// Read in the uploaded file
$fileContents = fread($file, filesize($userfile));
// Escape special characters in the file
$fileContents = AddSlashes($fileContents);
}
else
$fileContents = NULL;
$insertQuery = "INSERT INTO files VALUES (NULL, \"{$short}\",
\"{$userfile_type}\", \"{$mimeName}\", \"{$fileContents}\")";
if ((@ mysql_query ($insertQuery, $connection))
&& @ mysql_affected_rows() == 1)
header("Location: receipt.php?status=T&file="
. mysql_insert_id($connection));
else
header("Location: receipt.php?status=F&file="
. mysql_insert_id($connection));
} // if else empty()
?>
================================================== ===========
Get image from MySQL table via PHP
<?php // use this code to select which image you want
echo "<img src=\"view.php?file={$row["id"]}\">";
?>
<?php include 'db.inc'; $file = clean($file, 4); if (empty($file))
exit; if (!($connection = @ mysql_pconnect($hostName, $username,
$password))) showerror(); if (!mysql_select_db("files", $connection))
showerror(); $query = "SELECT mimeType, fileContents FROM files WHERE id
= $file"; if (!($result = @ mysql_query ($query,$connection))) showerror();
$data = @ mysql_fetch_array($result); if (!empty($data["fileContents"]))
{ // Output the MIME header header("Content-Type:
{$data["mimeType"]}"); // Output the image echo
$data["fileContents"]; }?>
|