Displaying images using php function

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

Post Reply
Mel Voght
Forum Newbie
Posts: 5
Joined: Tue Nov 05, 2002 10:24 pm
Location: Rippon, WV
Contact:

Displaying images using php function

Post by Mel Voght »

I'm new....embarrassingly new to php.

I'm working on what conceptually is a very simple website design, but I'm running into syntax errors and language barriers :? .

So, what I'm trying to do is create a SIMPLE webpage that has images in a table (heck, to start with, I just want images to DISPLAY) and the lady who maintains the website will only have to update the mysql database with the new items. A bit like a catalog, but not one that you can order from.

My sql is:

Code: Select all

CREATE TABLE inventory
( ItemID int not null auto_increment primary key,
  CatID int,
  ImgSrc char(255),
  thumbnail char(255),
  Description char(100),
  Size char(100)
);

insert into inventory values
( NULL,1,"/images/1001.jpg","images/1001_sm.jpg","Pine","17w x 17d x 22h"),
( NULL,1,"/images/1003.jpg","images/1003_sm.jpg","Pine","17w x 16d x 30h");
This part works, just trying to give as much info as possible (and only 2 rows for space).

Now, since I'm such a newbie, I'm not trying to build Rome or anything, I just want to start by displaying that ImgSrc as a jpg. When I tried just using the imagecreatefromjpeg function, I got "can't open file" errors. SO, I went to the function that is shown below (and copied from the php manual) hoping that it had to do w/ the image creation and such, but now it doesn't crash, but does nothing.

I'm failing at getting a function to be recognized in another file...so I've ended up declaring the function in the main php file.

It looks like this:

Code: Select all

<html>
<head>
  <title>Erneys Results</title>
</head>
<body>
<h1>Test Connection Script</h1>

<?
function LoadJpeg ($imgname) 
&#123;
    $im = @imagecreatefromjpeg ($imgname); /* Attempt to open */
    if (!$im) &#123; /* See if it failed */
        $im  = imagecreate (150, 30); /* Create a blank image */
        $bgc = imagecolorallocate ($im, 255, 255, 255);
        $tc  = imagecolorallocate ($im, 0, 0, 0);
        imagefilledrectangle ($im, 0, 0, 150, 30, $bgc);
        /* Output an errmsg */
        imagestring ($im, 1, 5, 5, "Error loading $imgname", $tc);
    &#125;
    return $im;
&#125;?>

<?
  

  @ $db = mysql_pconnect("localhost", "erneys", "xSugD8VB");

  if (!$db)
  &#123;
     echo "Error: Could not connect to database.  Please try again later.";
     exit;
  &#125;

  mysql_select_db("erneys_com");
  $query = "select * from inventory";
  $result = mysql_query($query);

  $num_results = mysql_num_rows($result);

  echo "<p>Number of Items found: ".$num_results."</p>";
  //require ("imagefunctions.php");
  for ($i=0; $i <$num_results; $i++)

  &#123;
     $row = mysql_fetch_array($result);
     echo "</strong><br>Image source: ";
     echo stripslashes($row&#1111;"ImgSrc"]);
     LoadJpeg (($row&#1111;"ImgSrc"]));
     echo "<br>Description: ";
     echo stripslashes($row&#1111;"Description"]);
	 echo "<br>Size: ";
	 echo stripslashes($row&#1111;"Size"]);
     echo "</p>";
  &#125;

?>

</body>

</html>
At first, I didn't have the function in this same file I had undeclared function errors. The data displays fine, but the image display, I've got it all wrong.

Is it the image dimensions? The creation? Am I trying to use the wrong function?

Any suggestions are much appreciated.

Thanks,

Mel
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you're mixing html-document-data and imagedata.
ImgSrc seems to be the relative path to the image file.
And that's exactly what should be in the document.
replace
echo stripslashes($row["ImgSrc"]);
LoadJpeg (($row["ImgSrc"]));
by

Code: Select all

echo '&lt;img src="', $row&#1111;'ImgSrc'], '" /&gt;';
I got "can't open file" errors
the webserver/php-account has read-access to this directory/file?
Mel Voght
Forum Newbie
Posts: 5
Joined: Tue Nov 05, 2002 10:24 pm
Location: Rippon, WV
Contact:

Post by Mel Voght »

:D WAY COOL! You are the best!

I was making this entirely too complicated! It worked...(and he says, "well DUH" :P ).

Many, many thanks.

Now, just for my son's entertainment he likes this emoticon :evil: .

Thanks again, I'm sure I'll be back with more questions.

Mel
Post Reply