Re: reading all images in a directory
by halley (Prior) on Jun 24, 2005 at 17:29 UTC
|
Before you go off and design a page which builds up a fixed row/column scheme for images, please consider doing it with a free-flowing approach, with CSS. An example is my "Quick Pix" image gallery. I didn't invent the CSS methods to do it, but the code and the markup is pretty straightforward.
Stretch the page's width and watch it lay out different numbers of columns. Unless you REALLY need something to be tabular, try to avoid <table>...</table> markup.
As for the thumbnails, you should also probably do that work once, instead of having the web server do that work thousands of times. I do it on my local machine, as a part of the process of uploading the images. I don't expect my web server to do any real work beyond delivering bits.
-- [ e d @ h a l l e y . c c ]
| [reply] [d/l] |
|
|
The do thumbnail at loading suggestion is great -- it may seem like a small task to generate the thumbnails dynamically but it always turns out that it is the straw that breaks the camels back when the site gets busy. The two ways I do it (they have different advantages and weaknesses) are: 1: as images are uploaded generate the thumbnails needed (usually in my case a few different sizes for different views). 2: Allow uploads to happen without creating thumbnails, but on first access to each type of thumbnail generate and cache it.
The first way works better when the addition of images to a site are controlled only by your app -- such as with a cgi form. The second works well when users can place images in a directory structure or in some other way to insert into the site without your app knowing about them.
| [reply] |
Re: reading all images in a directory
by davidrw (Prior) on Jun 24, 2005 at 17:07 UTC
|
Why not use Image::Magick (specifically the composite method to build a single large image) or GD ?
Also note that "resizing" using HTML is generally not the best way -- it's better to physically resize the image. For example, if your image is 500k at 800x600 and you have <img src="..." width="200" height="150">, yes it will be displayed at 200x150 (though it's up to the browser to scale it properly w/o making it look bad), but the client still has to pull down the 500k image as opposed to maybe 25k if it were physically resized to 200x150.
As for the HTML table, HTML::Table might be what you need to save the trouble of writing the loops yourself. | [reply] [d/l] [select] |
Re: reading all images in a directory
by injunjoel (Priest) on Jun 24, 2005 at 17:05 UTC
|
Greetings,
I would use HTML::Template to set up a table for image display, 5 per row, as many rows as needed. Something like
<table border="0" cellpadding="4" cellspacing="1" bgcolor="#000000">
<TMPL_LOOP name="rowloop">
<tr bgcolor="#ffffff">
<td valign="top">
<TMPL_IF name="img1src">
<img src="/cgi-bin/thumbnail.pl?img=<TMPL_VAR name="img1src">">
</TMPL_IF>
</td>
<td valign="top">
<TMPL_IF name="img2src">
<img src="/cgi-bin/thumbnail.pl?img=<TMPL_VAR name="img2src">">
</TMPL_IF>
</td>
<td valign="top">
<TMPL_IF name="img3src">
<img src="/cgi-bin/thumbnail.pl?img=<TMPL_VAR name="img3src">">
</TMPL_IF>
</td>
<td valign="top">
<TMPL_IF name="img4src">
<img src="/cgi-bin/thumbnail.pl?img=<TMPL_VAR name="img4src">">
</TMPL_IF>
</td>
<td valign="top">
<TMPL_IF name="img5src">
<img src="/cgi-bin/thumbnail.pl?img=<TMPL_VAR name="img5src">">
</TMPL_IF>
</td>
</tr>
</TMPL_LOOP>
</table>
As for resizing you could create a script that reads in a single image and outputs the resized version, like a thumbnail, You will need GD or Image::Magic for this. Then with your gallery script fill the HTML::Template with image tags that call the thumbnail script i.e. <image src="/cgi-bin/thumbnail.pl?img=foobar.jpg" /> That way you don't have to slurp the entire directory of images in, just their names.
Update
Fixed Template formatting for readability
-InjunJoel
"I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo
| [reply] [d/l] [select] |
Re: reading all images in a directory
by Transient (Hermit) on Jun 24, 2005 at 17:01 UTC
|
$i = 1;
print "<tr>";
for ( @images ) {
print "<td>";
# print image
print "</td>"
print "</tr><tr>" if !$i++%5;
}
print "</tr>";
(Untested) | [reply] [d/l] |
|
|
$i = 0;
$open = 0;
foreach (@images)
# or: while ($sth->fetch)
# or: while (readdir(IMAGE_DIR))
{
(print "<tr>", $open = 1) unless $open;
print "<td>";
# print image
print "</td>"
(print "</tr>", $open = 0) unless ++$i % 5;
}
print "<tr>" if $open;
As a bonus, I wrote the fix such that you don't have to know how many images there are in advance (just like in the original solution). This is very useful when reading from a database.
| [reply] [d/l] |
Re: reading all images in a directory
by izut (Chaplain) on Jun 24, 2005 at 17:08 UTC
|
$count = 0;
while(my $file = readdir(DIR)) {
print "$file ";
# can change \n to </tr><tr> here. remember
# to surround the while with <tr> and </tr>
print "\n" unless (++$count % 5);
}
surrender to perl. your code, your rules.
| [reply] [d/l] |
Re: reading all images in a directory
by jhourcle (Prior) on Jun 24, 2005 at 19:19 UTC
|
I'd really suggest following halley's suggestion to not use tables, but in response to the last question:
Also, is there any way to resize images WITHOUT Image::Magic or the GD library?
There are plenty of ways, but they're not all scriptable. I've lately been using sips, but ImageMagick is more cross-platform. (and sips won't handle gifs in MacOS 10.3) There are also programs like GraphicConverter, GIMP, Photoshop, etc.
| [reply] |
Re: reading all images in a directory
by TedPride (Priest) on Jun 24, 2005 at 19:47 UTC
|
Either you want your design to be fixed-width, in which case tables are fine, or you don't, in which case you don't have to bother with formatting at all, tables or CSS. Just set a hspace and vspace on each image that's half of the space you want between images, then put all your image tags smack up next to one another. The browser will automatically wrap for you. I posted a demonstration here:
http://www.home-school.com/test/imagewrap/
This should work on any browser from completely modern to horribly archaic, and the beautiful thing is that it takes LESS page formatting, not more. | [reply] |
|
|
| [reply] |
Re: reading all images in a directory
by TedPride (Priest) on Jun 25, 2005 at 07:15 UTC
|
True, but he just wants to display thumbnails of all the images in the directory. He didn't say anything about captions. | [reply] |