Re: [PHP] Dynamic Display of Images Stored in DB
On 11/28/07, Chris [EMAIL PROTECTED] wrote: In my solution, I use two scripts. One for showing the image true size and another for generating a thumbnail -- I may be wrong, but I think it's better to generate a thumbnail as needed on the fly than it is to store both images (large and thumbnail) in the dB. Cache it on the filesystem even if it's for a short time (of course if the image is updated elsewhere the cache needs to be cleared as well..). ?php $file = '/path/to/cache/file.jpg'; if (file_exists($file)) { if (filemtime($file) strtotime('-30 minutes')) { $fp = fopen($file, 'rb'); fpassthru($fp); exit; } // the file is older than 30 minutes, kill it and start again. unlink($file); } // continue creating your thumbnail. $fp = fopen('/path/to/cache/' . $filename, 'wb'); fputs($fp, $imagecontents); fclose($fp); // display image -- Thanks for all the suggestions, guys. I was really trying to understand, through extending it, the code from the reference ( http://www.wellho.net/solutions/php-example-php-form-image-upload-store-in-mysql-database-retreive.html ). David
Re: [PHP] Dynamic Display of Images Stored in DB
On Thursday 29 November 2007 06:03:32 Chris wrote: In my solution, I use two scripts. One for showing the image true size and another for generating a thumbnail -- I may be wrong, but I think it's better to generate a thumbnail as needed on the fly than it is to store both images (large and thumbnail) in the dB. Cache it on the filesystem even if it's for a short time (of course if the image is updated elsewhere the cache needs to be cleared as well..). ?php $file = '/path/to/cache/file.jpg'; if (file_exists($file)) { if (filemtime($file) strtotime('-30 minutes')) { $fp = fopen($file, 'rb'); fpassthru($fp); exit; } // the file is older than 30 minutes, kill it and start again. unlink($file); } // continue creating your thumbnail. $fp = fopen('/path/to/cache/' . $filename, 'wb'); fputs($fp, $imagecontents); fclose($fp); // display image Hell, I'm all ok with this method... but does (different) webhotells take into account the amount used with cache/temp files. If so, some check should be used, and if not. Cache it all!, and remove the timelimit, some check for the change of image of course, but that all depends if you acctually change images from time to time.. -- Postgresql php tutorials http://www.designmagick.com/ -- --- Børge Holen http://www.arivene.net -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dynamic Display of Images Stored in DB
Hell, I'm all ok with this method... but does (different) webhotells take into account the amount used with cache/temp files. If so, some check should be used, and if not. Cache it all!, and remove the timelimit, some check for the change of image of course, but that all depends if you acctually change images from time to time.. If it's under your user account then there's no way for them to know which parts of your website are cache/temp folders and which aren't. So yes, caches temp files/folders will be included in your disk quota. -- Postgresql php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dynamic Display of Images Stored in DB
On Friday 30 November 2007 00:39:39 Chris wrote: Hell, I'm all ok with this method... but does (different) webhotells take into account the amount used with cache/temp files. If so, some check should be used, and if not. Cache it all!, and remove the timelimit, some check for the change of image of course, but that all depends if you acctually change images from time to time.. If it's under your user account then there's no way for them to know which parts of your website are cache/temp folders and which aren't. So yes, caches temp files/folders will be included in your disk quota. I mean't system wide, like the php temp files, or /tmp. -- --- Børge Holen http://www.arivene.net -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Dynamic Display of Images Stored in DB
Good Afternoon All, The recent threads about images got me to finally experiment with storing into and retrieving/displaying images from a database. Uploading and retrieval is fine, I'm just a bit uncertain about creating the dynamic display part. // getting the data out of the db $imageCountinDB = 0; $selectedData = @mysql_query(select title, imagedata from pictures order by pid desc); while ( $row = @mysql_fetch_assoc($selectedData )) { $title[] = htmlentities( $row['title'] ); $imageBytes[] = htmlentities( $row['imagedata'] ); $imageCountinDB++; } // creating the an html table with one img tag per cell if ( IsSet( $_GET['im'] ) $imageCountInDB 0 ) { $imageOutputStr = tabletr; for ( $i = 0; $i $imageCountinDB; $i++ ) { $setID = $i + 1; $imageOutputStr .= tdimg src=?im=$setID width=300/td; if ( $setID % 3 == 0 $imageCountinDB $setID ) { $imageOutputStr .= /trtr\n; } } $imageOutputStr .= /tr/table; } // associating the image data with the img tags switch ($_GET['im']) { case 1: header(Content-type: image/jpeg); print $bytes[0]; exit (); break; case 2: header(Content-type: image/jpeg); print $bytes[1]; exit (); break; (snip) } html body ?php echo $imageOutputStr; ? /body /html The question is, with all this happening in one page, is it possible to do the last bit dynamically? BTW, the core of the above was nicked from http://www.wellho.net/solutions/php-example-php-form-image-upload-store-in-mysql-database-retreive.html Thanks very much for reading this long post. David
Re: [PHP] Dynamic Display of Images Stored in DB
you need a *seperate* script that outputs the image data only (+ relevant headers) and then you refer to that script (passing it a suitable parameter so it knows which image to output) in the src attribute of an IMG tag. you seem to be trying to output image data and html in a single request, which won't work (well actually there is a way to embed the raw image data directly in the html but AFAICR, but that's somewhat too tricky to cover right now given that your still trying to grok the basics of this - no offence intended) or have I misunderstood your question? David Giragosian wrote: Good Afternoon All, The recent threads about images got me to finally experiment with storing into and retrieving/displaying images from a database. Uploading and retrieval is fine, I'm just a bit uncertain about creating the dynamic display part. // getting the data out of the db $imageCountinDB = 0; $selectedData = @mysql_query(select title, imagedata from pictures order by pid desc); while ( $row = @mysql_fetch_assoc($selectedData )) { $title[] = htmlentities( $row['title'] ); $imageBytes[] = htmlentities( $row['imagedata'] ); $imageCountinDB++; } // creating the an html table with one img tag per cell if ( IsSet( $_GET['im'] ) $imageCountInDB 0 ) { $imageOutputStr = tabletr; for ( $i = 0; $i $imageCountinDB; $i++ ) { $setID = $i + 1; $imageOutputStr .= tdimg src=?im=$setID width=300/td; if ( $setID % 3 == 0 $imageCountinDB $setID ) { $imageOutputStr .= /trtr\n; } } $imageOutputStr .= /tr/table; } // associating the image data with the img tags switch ($_GET['im']) { case 1: header(Content-type: image/jpeg); print $bytes[0]; exit (); break; case 2: header(Content-type: image/jpeg); print $bytes[1]; exit (); break; (snip) } html body ?php echo $imageOutputStr; ? /body /html The question is, with all this happening in one page, is it possible to do the last bit dynamically? BTW, the core of the above was nicked from http://www.wellho.net/solutions/php-example-php-form-image-upload-store-in-mysql-database-retreive.html Thanks very much for reading this long post. David -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dynamic Display of Images Stored in DB
On 11/28/07, Jochem Maas [EMAIL PROTECTED] wrote: you need a *seperate* script that outputs the image data only (+ relevant headers) and then you refer to that script (passing it a suitable parameter so it knows which image to output) in the src attribute of an IMG tag. you seem to be trying to output image data and html in a single request, which won't work (well actually there is a way to embed the raw image data directly in the html but AFAICR, but that's somewhat too tricky to cover right now given that your still trying to grok the basics of this - no offence intended) or have I misunderstood your question? David Giragosian wrote: Good Afternoon All, The recent threads about images got me to finally experiment with storing into and retrieving/displaying images from a database. Uploading and retrieval is fine, I'm just a bit uncertain about creating the dynamic display part. // getting the data out of the db $imageCountinDB = 0; $selectedData = @mysql_query(select title, imagedata from pictures order by pid desc); while ( $row = @mysql_fetch_assoc($selectedData )) { $title[] = htmlentities( $row['title'] ); $imageBytes[] = htmlentities( $row['imagedata'] ); $imageCountinDB++; } // creating the an html table with one img tag per cell if ( IsSet( $_GET['im'] ) $imageCountInDB 0 ) { $imageOutputStr = tabletr; for ( $i = 0; $i $imageCountinDB; $i++ ) { $setID = $i + 1; $imageOutputStr .= tdimg src=?im=$setID width=300/td; if ( $setID % 3 == 0 $imageCountinDB $setID ) { $imageOutputStr .= /trtr\n; } } $imageOutputStr .= /tr/table; } // associating the image data with the img tags switch ($_GET['im']) { case 1: header(Content-type: image/jpeg); print $bytes[0]; exit (); break; case 2: header(Content-type: image/jpeg); print $bytes[1]; exit (); break; (snip) } html body ?php echo $imageOutputStr; ? /body /html The question is, with all this happening in one page, is it possible to do the last bit dynamically? BTW, the core of the above was nicked from http://www.wellho.net/solutions/php-example-php-form-image-upload-store-in-mysql-database-retreive.html Thanks very much for reading this long post. David Thing is, the above works just fine as long as I hard code a switch case for every image file pulled from the db. What I can't seem to do is dynamically extend it to include newly uploaded images, i.e., for an image count greater than what I've hard coded. I thought maybe something like 'eval' might be used (I know, if eval is the answer, I'm asking the wrong question). To be honest, I don't even know _why_ the above works because it seems that stuff is happening as if in a while loop when there isn't one (the switch case part). For the moment, I'd be happy to just understand how it works as it does. Maybe as you suggest, there is raw image data somehow being embedded in the html. On a peripherally related note, I did learn that syntax like img src=?im=6 looks like this in the page source: img src=http://www.mypage.php?im=6. Never liked the short-hand stuff... Thanks, Jochem, for responding. David
Re: [PHP] Dynamic Display of Images Stored in DB
... http://www.wellho.net/solutions/php-example-php-form-image-upload-store-in-mysql-database-retreive.html having taken a quick look at that page I can only hope that you aspire to write alot better code than that! Thanks very much for reading this long post. David Thing is, the above works just fine as long as I hard code a switch case for every image file pulled from the db. What I can't seem to do is dynamically extend it to include newly uploaded images, i.e., for an image count greater than what I've hard coded. I thought maybe something like 'eval' might be used (I know, if eval is the answer, I'm asking the wrong question). no to eval(). period. I personally have never had any reason to use eval() there has always been a safer more performant way of achieving whatever it was. possibly someone on this list can give a realworld, valid example of using eval(), although I probably wouldn't put money on it. To be honest, I don't even know _why_ the above works because it seems that stuff is happening as if in a while loop when there isn't one (the switch case part). I reread the code and I understand what it does now. I don't think it's very good. to start I would split it up into 2 scripts. one to generate the HTML table and one to output image data (basically what the switch statement does). secondly when your building the html table you don't need to retrieve the image data - change your select statement so that it is not included, that will be more performant. the src attributes of the img tags should reference your new image output script something like this img src=img.php?im=$setID alt= / lastly the new script you create to output image data should use an SQL query of it's own to retrieve the image data relevant to the passed in id. something like this: img.php: ?php $id = $_GET['im']; if ($id ($row = mysql_fetch_assoc(mysql_query(select imagedata from pictures WHERE pid=$id { header(Content-type: image/jpeg); echo $row['image_data']; } exit; ? note I have not bothered to add much error checking of request variable sanitation ... that is left as an exercise for the reader. the whole example rests on the fact that the table containing the image data should have a primary key that you can reference (so don't use some incremented counter for the value of $setID !!!), additionally you will need to store the mime-type along with the image data in order to be able to output the correct Content-type header. one more thing - your a noob - noobs are not allowed (imho) to prefix expressions with the error repression symbol (the '@' symbol) ... you want to see/log errors, there is hardly ever any reason to use '@' to repress errors and if you really need it you will know exactly why. sounds harsh but it will save you headaches. For the moment, I'd be happy to just understand how it works as it does. Maybe as you suggest, there is raw image data somehow being embedded in the html. no, nothing is being embedded - the script is being called in 2 different contexts, firstly to output html, secondly to output image data - the script is incredibly inefficient - every call to the script is retrieving all the image data from the database, which hopefully you can understand is completely unnecessary. On a peripherally related note, I did learn that syntax like img src=?im=6 looks like this in the page source: img src=http://www.mypage.php?im=6. Never liked the short-hand stuff... Thanks, Jochem, for responding. David -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dynamic Display of Images Stored in DB
At 6:12 PM -0600 11/28/07, David Giragosian wrote: Thing is, the above works just fine as long as I hard code a switch case for every image file pulled from the db. What I can't seem to do is dynamically extend it to include newly uploaded images, i.e., for an image count greater than what I've hard coded. I thought maybe something like 'eval' might be used (I know, if eval is the answer, I'm asking the wrong question). To be honest, I don't even know _why_ the above works because it seems that stuff is happening as if in a while loop when there isn't one (the switch case part). For the moment, I'd be happy to just understand how it works as it does. Maybe as you suggest, there is raw image data somehow being embedded in the Unfortunately, my webbytedd.com site is down where I have an example, but I can't show code right now. But, what I did simply was to load the images into the dB and then pull them out as needed. As Jochem suggested, it's better to use a different script for inserting your images into a html table than it is to try to make it a function in your main script. That way you can buffer the image and call the image in via a img tag. In my solution, I use two scripts. One for showing the image true size and another for generating a thumbnail -- I may be wrong, but I think it's better to generate a thumbnail as needed on the fly than it is to store both images (large and thumbnail) in the dB. As for a counter, that value can come directly from your dB. You can either find out how many images there are in your dB and set a counter to that figure OR draw the images out via a LIMIT -- either way, there's no need to hard code a limit. You can still use your switch because it is based upon image type, which is a good idea -- if you're going to store different image types in your dB. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dynamic Display of Images Stored in DB
In my solution, I use two scripts. One for showing the image true size and another for generating a thumbnail -- I may be wrong, but I think it's better to generate a thumbnail as needed on the fly than it is to store both images (large and thumbnail) in the dB. Cache it on the filesystem even if it's for a short time (of course if the image is updated elsewhere the cache needs to be cleared as well..). ?php $file = '/path/to/cache/file.jpg'; if (file_exists($file)) { if (filemtime($file) strtotime('-30 minutes')) { $fp = fopen($file, 'rb'); fpassthru($fp); exit; } // the file is older than 30 minutes, kill it and start again. unlink($file); } // continue creating your thumbnail. $fp = fopen('/path/to/cache/' . $filename, 'wb'); fputs($fp, $imagecontents); fclose($fp); // display image -- Postgresql php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php