[CLUG-chat] Intro into programming.

Neil Blakey-Milner nbm at mithrandr.moria.org
Thu Aug 3 12:18:25 SAST 2006

On Thu 2006-08-03 (09:37), Brendon Gleeson wrote:
> Jonathan Hitchcock wrote:
> ....
> >This keeps happening in these sorts of threads.  Somebody asks what 
> >language they should learn, somebody else recommends PHP, and then Neil 
> >or I (and a few others) wade in and try to explain how awful PHP is.
> ...
> I no longer use PHP, but basics like variables, functions, *ahem* classes 
> (maybe ignore that one), and basic flow of script can be learned in a down 
> and dirty way..
> I think you're right in saying how it promotes bad practice etc... but I 
> couldn't really grasp what Python did or what it was until I could write a 
> butt-ugly php script.. and I certainly wouldn't have known what Rails was 
> until I knew what sort of problems it solved. I think expecting someone to 
> grasp a web framework from day one is stretching it a little...
> Yep, try python. Write a little program that resizes a couple of images 
> places a watermark on them and uploads them to a FTP site in less than 10 
> lines... ok that's not really a challenge.. Yep try Python..

You mean:

#!/usr/bin/env python
import Image, ftplib, sys                           # import needed modules
fn, wfn = sys.argv[1:3]                             # get command line arguments
im = Image.open(fn)                                 # load original image
wim = Image.open(wfn)                               # load watermark file
mim = wim.convert("L")                              # convert to mask
im.paste(wim, None, mim)                            # apply watermark to image
im.save('out2.jpg', None)                           # save to temporary file (could use tempfile module)
site = ftplib.FTP('localhost')                      # connect to FTP site
site.storbinary("PUT out2.jpg", file('out2.jpg'))   # store file

Sorry, exactly 10 lines if you include the shebang path.  This will
raise an exception and quit as soon as the first error occurs.  This
exception can be caught.  This code can be put in a function, and will
not leak resources.  On caught exceptions, no spurious comments will be
sent to the screen/browser/log.

To do as much of the same as a PHP4 "script":

#!/usr/bin/env php
if (count($_SERVER['argv']) < 3) {
    echo "Not enough arguments given\n";
list($fn, $wfn) = array_slice($_SERVER['argv'], 1, 2);
$im = @imagecreatefromjpeg($fn);
if (!$im) {
    echo "Some error loading source file\n";
$wim = @imagecreatefrompng($wfn);
if (!$wim) {
    echo "Some error loading watermark file\n";
$width = imagesx($im);
$height = imagesy($im);
$res = @imagecopymerge($im, $wim, 0, 0, 0, 0, $width, $height, 100);
if (!$res) {
    echo "Some error merging images\n";
$res = @imagejpeg($im, 'out2.jpg');
if (!$res) {
    echo "Some error writing to JPEG\n";
$rConn = @ftp_connect('localhost');
if (!$rConn) {
    echo "Some error connecting to FTP server";
$res = @ftp_put($rConn, 'out2.jpg', 'out2.jpg', FTP_BINARY);
if (!$res) {
    echo "Some error putting file on server";

Neil Blakey-Milner
nbm at mithrandr.moria.org

More information about the clug-chat mailing list