Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script not showing created file 2

Status
Not open for further replies.

jriggs420

Programmer
Sep 30, 2005
116
US
I wasn't sure if I should post here, or in the cgi forums, but I think my problem is more perl than anything else. This script
Code:
#!/usr/bin/perl
use CGI qw(:standard);
use Image::Magick;

print header;
print "<link rel=\"stylesheet\" ";
print "type=\"text\/css\"  href=\"\/uploads/default\.css\">"."\n";
print "<BODY bgcolor=\"999966\">";

[...]

print "<h2>Files Currently in photo folder:</h2>\n";
opendir(DIR, "/var/[URL unfurl="true"]www/uploads");[/URL]
@photo=grep(/\.jpg/i,readdir(DIR));
closedir(DIR);
[...]
my $im = Image::Magick->new;#found this section over at perlmonks...

my (@photo) = @photo;
for (@photo) {
  if (/ /) {
    my $old = $_;
    tr, ,_,s;
    $_ .= ".jpg" unless /\.jpg$/;
    ! -e and rename $old, $_ or next;
    warn "renaming $old to $_\n";
  }
  next if /\.thumb\.jpg$/;
  my $thumb = "$_.thumb.jpg";
  next if -e $thumb;
  undef @$im;
  my $ret;
  $ret = $im->Read($_)
    and warn($ret), next;
  $ret = $im->Scale(geometry => '100x100')
    and warn($ret), next;
  $ret = $im->Write($thumb)
    and warn($ret), next;
  warn "thumbnail made for $_\n";
}
foreach $file (sort @photo){
	if ($file !~ /thumb/){print "<a href=\"\/uploads\/$file\">$file</a><br>";}
	if ($file =~ /thumb/){print "<img src=\"\/uploads\/$file\" border=0 width=\"40\" height=\"40\"><br>";}
	} 

print end_html;
goes through my uploads folder and if it finds a .jpg which hasn't been resized, will do so. That part works great. The foreach loop which displays the link/image, won't pick up the new thumbnail the first time around. But when I click 'refresh' on the browser, they are there.
Also should I separate the 'resize' portion from the cgi script? No issues yet, but maybe will cause page to load slower. I would prefer to have it all in one click if possible. Thanks for any suggestions-

Because a thing seems difficult for you, do not think it impossible for anyone to accomplish.
Marcus Aurelius
 
It won't pick up the thumbnails on the first iteration because the @photo array is populated before you made the thumbnails and isn't updated when you make each thumbnail.

After making the thumbnail, add the code in bold:
Code:
  $ret = $im->Write($thumb)
    and warn($ret), next;
  [b]push @photo, $thumb;[/b]
  warn "thumbnail made for $_\n";
 
@photo contains the file names from the first read of the dir, before tht thumbnails were created. You need to read the dir again after you write the thumbnails to list the new files or push the new thumbnail image file names into the @photo array while you create them
 
alternately, rescan the directory, by copying the opendir..closedir code to right before the display code:

Code:
  warn "thumbnail made for $_\n";
}
[b]opendir(DIR, "/var/[URL unfurl="true"]www/uploads");[/URL]
@photo=grep(/\.jpg/i,readdir(DIR));
closedir(DIR);[/b]

foreach $file (sort @photo){
 
Of course, order of operations. Sometimes I lose the forest for all the trees, or is it the other way around? Thanks for the advice.

Does anyone know a better way to write
Code:
{print "<img src=\"\/uploads\/$file\" border=0 width=\"40\" height=\"40\"><br>";}
I've not been able to get 'qw()' to work without having spacing issues.

Because a thing seems difficult for you, do not think it impossible for anyone to accomplish.
Marcus Aurelius
 
Try:
Code:
{ print '<img src="/uploads/'.$file.'" border="0" width="40"><br>';}
 
Use qq, not qw:

Code:
print qq~<img src="/uploads/$file" border="0" width="40"><br>~;

qw is for lists, qq is for double quoted strings. The '~' is an arbitrary delimiter I picked, you can use any valid characters as delimiters, but some must be in pairs, basically any set with a left and a right character: () [] {} <>
 
Follow-up:
In this directory I have 2 photos now, one is the thumbnail and the other is the actual fullsize jpg. I'm trying to create dynamic <a href's for the thumbs.
the files are formatted as such:
Code:
 himom.jpg
himom.jpg.thumb.jpg
So, as I'm looping thru, img src should be 'himom.jpg.thumb.jpg, and the link should be to 'himom.jpg'. I tossed this code into the loop which I thought would pull the name.
Code:
my ($short) = $pic =~ /^(\w+\.jpg)/i;
 print "\n<td> $short <\/td>";
Which works, but leaves me with the
Code:
 Use of uninitialized value in concatenation (.) or string at img line 17, <DATA>
error/warning? I can't figure out why. Any ideas?

Because a thing seems difficult for you, do not think it impossible for anyone to accomplish.
Marcus Aurelius
 
I didn't take a real close look, but I suspect your regexp is not matching because the \w class does not include the dot character. You probably should use underscores in your filenames instead of dots anyway:

himom_jpg_thumb.jpg

that is a much more conventional method and your regexp will work because the underscore is part of the \w class, but you can use dots if you want to, you maybe just need to change the regexp:

Code:
my ($short) = $pic =~ /^([\w.]+\.jpg)/i;
print "\n<td> $short <\/td>";



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top