trying to load images from mySQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

sputnik577
Forum Newbie
Posts: 9
Joined: Mon Sep 18, 2006 10:32 am

trying to load images from mySQL

Post by sputnik577 »

feyd | Please use

Code: Select all

,

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

,

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]
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
sputnik577
Forum Newbie
Posts: 9
Joined: Mon Sep 18, 2006 10:32 am

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
sputnik577
Forum Newbie
Posts: 9
Joined: Mon Sep 18, 2006 10:32 am

Post 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) {
sputnik577
Forum Newbie
Posts: 9
Joined: Mon Sep 18, 2006 10:32 am

Post by sputnik577 »

ok i've posted my full code except the paging on the code critique forum

viewtopic.php?t=55683
sputnik577
Forum Newbie
Posts: 9
Joined: Mon Sep 18, 2006 10:32 am

Post 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  = '&nbsp;'; // we're on page one, don't print previous link
   $first = '&nbsp;'; // 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 = '&nbsp;'; // we're on the last page, don't print next link
   $last = '&nbsp;'; // nor the last page link
}

echo $first . $prev . $nav . $next . $last;


include 'uem/library/closedb.php';

?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Moving to PHP - Code forum.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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?
sputnik577
Forum Newbie
Posts: 9
Joined: Mon Sep 18, 2006 10:32 am

Post 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');
}
sputnik577
Forum Newbie
Posts: 9
Joined: Mon Sep 18, 2006 10:32 am

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
sputnik577
Forum Newbie
Posts: 9
Joined: Mon Sep 18, 2006 10:32 am

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

  1. image data cannot be sent in the same request stream as HTML
  2. open_image() is written in such a way that it requires a file, not raw image data from a database query.
Post Reply