Forum Moderators: coopster & phranque

Message Too Old, No Replies

Converting standard Perl to CGI

         

chekejuju

7:47 pm on Jan 16, 2004 (gmt 0)

10+ Year Member



Hello,

I'm not a great Perl programmer, but I oftenly use this language to do commun tasks on my Linux workstation (resize 100 pictures in a folder, parse HTML templates, rename files...)

Now I got the problem to have a very simple but effective script that checks if an image is larger than 130px, and if it does, runs "convert -resize x130 $myimage $myimage"

But this time, it's not running on my workstation, I need to use it as a CGI on a webserver, and I'm very confused with the usage of CGI, I need to exec this perl script from a PHP page giving only one argument (the image folder)

I know I should RTFM a bit more, but I'm so in rush... It took me 3 min to build up the Perl script, but now it's 2 hours I try to make it work as CGI, all I get is "server error..." and of course, my images remains too large...

thank you for any help, or URL link to a tutorial I should read...

chekejuju

8:48 pm on Jan 16, 2004 (gmt 0)

10+ Year Member



Help Help Help!

I Feel so Stupid! How the hell can I run a CGI script! it's so easy on linux just ./myscript.pl

how to lauch it from a HTML, php, whatever?
I find hundreds of sample scripts, but .. feel like I missed the first lesson.... how to run it?

I tried " <!-- #exec cgi="/cgi-bin/resize.cgi?MyArgument" -->

or <!-- #include cgi="/cgi-bin/resize.cgi?MyArgument" -->

so I thought I don't give the argument in the proper order, I've hardcoded it in the Perl script, not work again.....

Really sorry, noone talks about the command to run a CGI script from a web page... just coding help/samples...

again, I must be stupid, I missed the first lesson of the course, I can drive a car, but don't know how to start the engine... :(

chekejuju

8:57 pm on Jan 16, 2004 (gmt 0)

10+ Year Member



last one, here's my script... maybe it's totally wrong, and that's the problem?

#!/usr/bin/perl
#
# resize images from a given directory
# pass the directory name as argument,
# it will only resize the pictures bigger than 130 px high
#
use CGI;

$mydir = "$ENV{'QUERY_STRING'}";

opendir (ETC, $mydir);

#parse the directory
while ($name=readdir(ETC)){
#just read JPG images
if ($name =~ /.jpg/i){

#create the full path to the image
$myfullname= $mydir.$name;

# identify the size
($filename, $type, $sizes) = split ' ', `identify $myfullname`;

($sizes) = split '\+',$sizes;
($w, $h) = split 'x', $sizes;

# get the height of the picture, don't care of the width
if ($h > 130){
# bigger than 130 px? -> resize and overwrite!
$mycomm="convert -resize x130 $myfullname $myfullname";
system ($mycomm);
}
}
}
# close my folder
close (ETC);

phaze

10:09 pm on Jan 16, 2004 (gmt 0)

10+ Year Member



Hi juju,

I feel your pain. I wrote my first 12 years ago and struggled with this. CGI stands for common gateway interface and it describes the way a perl script or any other program, script or batch file talks to a web server. So you have to play by the rules.

You need one line of code in your script that will tell the web server what you are sending it.

print "Content-type: text/html\n\n";

Make sure you send the \n\n or it will break.

Then send some text to the server which will be sent to the browser. Start with:

print "Hello world!";

Make sure you first print Content-type or it will break.

Then you can do stuff like this:

print <<"EOF";
<html><head><title>test page</title></head><body>
This is my first real web page and I'm now a hardcore CGI developer.
</body></html>
EOF

I understand that you just want to execute something, but if your script is called by the web server, then it must send some sort of response back to the server or it will give you an internal server error.

If you are already scripting in PHP and want to call the perl script, then you must call it from PHP as an external program and once it has finished running, you must have PHP send the response back to the server.

If you've managed to succesfully write your script, I highly recommend getting the perl Template module from CPAN (Template Toolkit) and playing with that. It's the most popular templating system for perl and is VERY fast and very powerful.

These will get you started:
[google.com...]