Page 1 of 2
trying to load images from mySQL
Posted: Mon Sep 18, 2006 10:48 am
by sputnik577
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
How can i load a picture from a mySQL database using php. The picture is uploaded in a table and then dynamically loaded to a php page. It's attribute in the database table is longblob.
Code: Select all
while(list($name, $type, $size, $path) = mysql_fetch_array($imgResult))
{
$im = open_image($path);
if ($im === false) {
die ('Unable to open image');
}
header ('Content-Type: image/jpeg');
imagejpeg($path);
echo 'Opened image';
function open_image($path) {
# JPEG:
$im = @imagecreatefromjpeg($path);
if ($im !== false) { return $im; }
# GIF:
$im = @imagecreatefromgif($path);
if ($im !== false) { return $im; }
# PNG:
$im = @imagecreatefrompng($path);
if ($im !== false) { return $im; }
# GD File:
$im = @imagecreatefromgd($path);
if ($im !== false) { return $im; }
# GD2 File:
$im = @imagecreatefromgd2($path);
if ($im !== false) { return $im; }
# WBMP:
$im = @imagecreatefromwbmp($path);
if ($im !== false) { return $im; }
# XBM:
$im = @imagecreatefromxbm($path);
if ($im !== false) { return $im; }
# XPM:
$im = @imagecreatefromxpm($path);
if ($im !== false) { return $im; }
# Try and load from string:
$im = @imagecreatefromstring(file_get_contents($path));
if ($im !== false) { return $im; }
return false;
}
All i get when i load the dynamic page is Blank
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Mon Sep 18, 2006 11:14 am
by pickle
You want to call
imagejpeg() on $im, not $path.
PHP Manual wrote:bool
imagejpeg ( resource image [, string filename [, int quality]] )
imagejpeg() creates the JPEG file in filename from the image image. The image argument is the return from the
imagecreatetruecolor() function.
The filename argument is optional, and if left off, the raw image stream will be output directly.
Posted: Mon Sep 18, 2006 11:41 am
by sputnik577
ok so i changed it to
Code: Select all
$im = open_image(imagejpeg($path));
if ($im === false) {
die ('Unable to open image');
}
header ('Content-Type: image/jpeg');
imagejpeg($im);
echo 'Opened image';
$path is my jpeg image in the database, attribute longblob. It's still not displaying.
Posted: Mon Sep 18, 2006 12:42 pm
by pickle
Still not quite.
imagejpeg() takes an image argument. In this case, that would be the result of
imagecreate() or
imagecreatetruecolor(), or
imagecreatefromjpeg(), etc. So, in that code, you're passing to your open_image() function, an image resource. open_image() is wanting a path though.
Change your code to call open_image($path).
Also, if you use [syntax_=_"php"][/syntax_] (without the underscores) rather than [_code_][/code_], you'll get your PHP code syntax highlighted as opposed to being all green.
Posted: Mon Sep 18, 2006 12:52 pm
by sputnik577
ok so i changed it to what you said and still nothing.
Code: Select all
while(list($name, $type, $size, $path) = mysql_fetch_array($imgResult))
{
$im = open_image($path);
if ($im === false) {
die ('Unable to open image');
}
header ('Content-Type: image/jpeg');
imagejpeg($im);
echo 'Opened image';
function open_image($path) {
Posted: Mon Sep 18, 2006 12:54 pm
by sputnik577
ok i've posted my full code except the paging on the code critique forum
viewtopic.php?t=55683
Posted: Mon Sep 18, 2006 2:26 pm
by sputnik577
Ok here's the whole code, i can't figure out why any of the images are not showing. I've been to a few different forums and they are all saying the same. Please Help!!!!
Code: Select all
include 'uem/library/config.php';
include 'uem/library/opendb.php';
$rowsperpage = 1;
$pageNum = 1;
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
$offset = ($pageNum - 1) * $rowsperpage;
$query = "SELECT tblProjects.id, tblProjects.prjName, tblProjects.description, tblImages.id, tblImages.path, tblImages.projImgID FROM tblProjects, tblImages
LIMIT $offset, $rowsperpage";
$result = mysql_query($query) or die('Error, query2 failed');
while(list($id, $prjName, $description, $projImgID, $path) = mysql_fetch_array($result))
{
$imgQuery = "SELECT name, type, size, path FROM tblImages WHERE projImgID = '$id'";
$imgResult = mysql_query($imgQuery) or die('Error, imgQuery failed');
?>
<br>
<b><? echo "$prjName"; ?><br></b>
<? echo "$description"; ?><br>
</td>
</tr>
</table></td>
<td width="266"><table width="206" height="218" border="0" align="right">
<tr>
<td width="200" height="214">
<div align="right">
<p>
<?
//while($row = mysql_fetch_assoc($imgResult))
while(list($name, $type, $size, $path) = mysql_fetch_array($imgResult))
{
$im = open_image($path);
if ($im === false) {
die ('Unable to open image');
}
header ('Content-Type: image/jpeg');
imagejpeg($im);
echo 'Opened image';
function open_image($path) {
# JPEG:
$im = @imagecreatefromjpeg($path);
if ($im !== false) { return $im; }
# GIF:
$im = @imagecreatefromgif($path);
if ($im !== false) { return $im; }
# PNG:
$im = @imagecreatefrompng($path);
if ($im !== false) { return $im; }
# GD File:
$im = @imagecreatefromgd($path);
if ($im !== false) { return $im; }
# GD2 File:
$im = @imagecreatefromgd2($path);
if ($im !== false) { return $im; }
# WBMP:
$im = @imagecreatefromwbmp($path);
if ($im !== false) { return $im; }
# XBM:
$im = @imagecreatefromxbm($path);
if ($im !== false) { return $im; }
# XPM:
$im = @imagecreatefromxpm($path);
if ($im !== false) { return $im; }
# Try and load from string:
$im = @imagecreatefromstring(file_get_contents($path));
if ($im !== false) { return $im; }
return false;
}
//<img src= <? echo "$content"; width="200" height="175"> -->
}?>
</p>
<p> </p>
</div></td>
</tr>
</table></td>
</tr>
<tr>
<td colspan="2"><table width="521" border="0" align="center">
<tr>
<td width="515" height="42"><div align="center">
<?
}
$query2 = "SELECT COUNT(id) AS numrows FROM tblProjects";
$result = mysql_query($query2) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
$maxPage = ceil($numrows/$rowsperpage);
$self = $_SERVER['PHP_SELF'];
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= " $page "; // no need to create a link to current page
}
else
{
$nav .= " <a href=\"$self?page=$page\">$page</a> ";
}
}
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
$first = " <a href=\"$self?page=1\">[First Page]</a> ";
}
else
{
$prev = ' '; // we're on page one, don't print previous link
$first = ' '; // nor the first page link
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page\">[Next]</a> ";
$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
}
else
{
$next = ' '; // we're on the last page, don't print next link
$last = ' '; // nor the last page link
}
echo $first . $prev . $nav . $next . $last;
include 'uem/library/closedb.php';
?>
Posted: Mon Sep 18, 2006 2:49 pm
by pickle
The '@' symbol turns off error reporting. Take out that symbol & hopefully the various imagecreatefrom... functions will tell you something. Also, output $path to see if that's what you expect.
Posted: Mon Sep 18, 2006 2:54 pm
by pickle
Moving to PHP - Code forum.
Posted: Mon Sep 18, 2006 3:03 pm
by John Cartwright
better yet, why not simply store the path of the file referencing it's location on the filesystem to avoid the overhead involved in storing an image in the database?
Posted: Mon Sep 18, 2006 3:06 pm
by sputnik577
ok i'm not getting anything from (code below) and on
Code: Select all
while(list($name, $type, $size, $path) = mysql_fetch_array($imgResult))
{
$im = open_image($path);
echo $path;
if ($im === false) {
die ('Unable to open image');
}
Posted: Mon Sep 18, 2006 3:39 pm
by sputnik577
so i tried a couple different things. I echo $path before $im = open_image($path); like below
Code: Select all
while(list($name, $type, $size, $path) = mysql_fetch_array($imgResult))
{
echo $path;
$im = open_image($path);
if ($im === false) {
die ('Unable to open image');
}
header ('Content-Type: image/jpeg');
imagejpeg($im);
echo 'Opened image';
[
and all i get is the mumble from the blob datatype
if i put the code as below
Code: Select all
while(list($name, $type, $size, $path) = mysql_fetch_array($imgResult))
{
$im = open_image($path);
echo $path;
if ($im === false) {
die ('Unable to open image');
}
header ('Content-Type: image/jpeg');
imagejpeg($im);
echo 'Opened image';
i get absolutely nothing. hmmmm any suggestions
Posted: Mon Sep 18, 2006 3:42 pm
by pickle
Ok, maybe I'm not understanding your setup. Is the actual image data stored in the database or is the path to the image file stored in the database?
Posted: Mon Sep 18, 2006 3:44 pm
by sputnik577
the image data is stored in the database. It is a mediumblob datatype. The image has been uploaded to the database. It is not a file on the server.
Posted: Mon Sep 18, 2006 3:57 pm
by feyd
- image data cannot be sent in the same request stream as HTML
- open_image() is written in such a way that it requires a file, not raw image data from a database query.