Main Stories
Slash Boxes

Slash Open Source Project

Slashcode Log In

Log In

[ Create a new account ]

Adding a rand photo block

posted by Krow on 06:28 PM December 20th, 2002   Printer-friendly   Email story
Hello, i have written something for my slash site deporteyciencia.com for having a nice random photo block, i try to describe the process first, and later put all the code.
  1. (locally) you need a lot of free photos
  2. (locally) you need imagemagick
  3. (locally) run the script create_minis.pl for creating the thumbs. Take a look to the destination directories. You can create a captions.txt with the syntaxis: filename::caption(newline)... The captions will be drawn on the image!!
  4. put the original images and the thumbs in your site
  5. (remote) put the task rand.pl in your site (and change the directories as needed)
  6. (remote)configure crontab for running your task daily or hourly.
  7. (remote) write a block who shows rand_mini_foto.jpg and links to rand_foto.jpg
  8. do nothing and enjoy, or try to write a way for users uploading photos "one day they will appear"
The code: -------------------------------- CREATE_MINIS.PL (remember to change directories and have imagemagick installed) -------------------------------- #!/usr/bin/perl -w use strict; #-------- config my $use_captions=1; my $background="#006699"; my $fore="#ffffff"; my $width="190x190"; #-------- config end my @files=`ls *.jpg`; my %captions=(); if ($use_captions) { open F_captions,"captions.txt" or die "ERROR, lack of \"captions.txt\"\n This file has the following sintaxis:\nfilename1::caption1\nfilename2::caption 2\n..."; while () { chomp; my ($filename,$text)=split (/::/,$_); $captions{$filename}=$text; } close F_captions; } foreach (@files) { print $_; chomp; if ($use_captions) { system("convert -resize $width_height $_ minis_with_captions/$_"); if($captions{$_}) { system("convert -box \"$background\" -font helvetica -fill \"$fore\" -draw \"text 0,10 \\\" $captions{$_}\\\"\" $_ images_with_captions/$_"); system("convert -box \"$background\" -font helvetica -fill \"$fore\" -draw \"text 0,10 \\\" $captions{$_}\\\"\" minis_with_captions/$_ minis_with_captions/$_"); } else { print "Error: lack of captions for file $_\n"; } } else { system("convert -resize $width_height $_ minis/$_"); } } -------------------------------- rand.pl (remember to change directory names) -------------------------------- #!/usr/bin/perl -w #coge una imagen aleatoriamente y la copia en rand_foto.jpg #lo mismo para una foto de las pequeñas, que están en /minis #llamado cada dia por una tarea use strict; #--- config my $path_in="/home/xavi/slash_2.2.5/fotos_deportivas" ; my $path_out="/home/xavi/slash_2.2.5/images"; #--- config end my $num_files=`ls $path_in/*.jpg|wc -l`; my $file=int rand $num_files; my $count=0; my @files=`cd $path_in;ls *.jpg`; foreach (@files) { $count ++; if ($count == $file) { chomp; system("cp $path_in/$_ $path_out/rand_foto.jpg"); system("cp $path_in/minis/$_ $path_out/rand_mini_foto.jpg"); } } -------------------------- write the block and config crontab for yourself.
It's more easy than i can explain with my english level :)
Thanks Krow: Yes this is a format nightmare but I thought I would share the submission with people anyways.
This discussion has been archived. No new comments can be posted.
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login
Loading... please wait.
  • I wrote this, and i wasn't able to write it "as code". Yes i know i can put
    in all the lines, but i thought krow will have a "special way".
    sorry.
    Remember to revise the directories
    • As a stroy is difficult to ident code, but as a comment it's easy. Here you have create_minis.pl

      #!/usr/bin/perl -w
      use strict;

      #-------- config
      my $use_captions=1;
      my $background="#006699";
      my $fore="#ffffff";
      my $width="190x190";
      #-------- config end

      my @files=`ls *.jpg`;
      my %captions=();

      if ($use_captions) {
          open F_captions,"captions.txt" or die "ERROR, lack of \"captions.txt\"\n This file has the following sintaxis:\nfilename1::caption1\nfilename2::caption 2\n...";
          while (<F_capti
    • #!/usr/bin/perl -w
      #catch randomly a photo and copy in rand_foto.jpg
      #the same for mini photos
      #one task executes daily this file in the server

      use strict;

      #--- config
      my $path_in="/home/xavi/slash_2.2.5/fotos_deportivas" ;
      my $path_out="/home/xavi/slash_2.2.5/images";
      #--- config end

      my $num_files=`ls $path_in/*.jpg|wc -l`;
      my $file=int rand $num_files;
      my $count=0;
      my @files=`cd $path_in;ls *.jpg`;

      foreach (@files) {
          $count ++;
          if ($count == $file) {
              chomp;
              system("cp $path_in/$_ $path_out/rand_foto.jpg");
              system("cp $path_in/minis/$_ $path_out/rand_mini_foto.jpg");
          }
      }
  • Kinda cool, but I'm still waiting for the file/image support in slash. Krow hinted at it a while ago........
    --

    --
    It's either on the beat or off the beat, it's that easy.