Archive for August, 2007

Ajax Comment Posting

Tuesday, August 14th, 2007

When I installed WordPress a few weeks ago, I didn’t think it would actually be my blogging system (as I don’t like people who write a script and don’t update the code for 10 years saying “we’re making WordPress backwards compatible!”). However, I eventually liked it. And I have an old custom - whenever I like something, I write a plugin to make it even better ;) And so I did.

Ajax Comment Posting will post your comments in an Ajax-way, without refreshing the page. It’ll also validate the comment form on the client and server side. I also worked a bit on the compatibility with older WP version, and so even v1.5.2 users can enjoy ACP! It was supposed to be just a simple comment-posting plugin, but I’ve got some emails complaining that it and the Ajax Edit Comments plugin can’t co-exist, so in a month’s time ACP’s functionality will be extended to allow users and admins to manage comments (in an easy, Ajax way). Thanks to Gene Steinberg for suggesting the further development of the plugin, and to Rayne Bair for pointing some errors - they all will be fixed in the forthcoming version of ACP.

You can read more about the plugin and see some screenshot on its page on regua.biz or in the WP plugin directory. Any suggestions are welcome.

Edit: ACP has been downloaded over 3,000 times! Thanks for your support and help with the plugin.

[siː dʒiː aɪ]

Tuesday, August 14th, 2007

Note: if you can’t see the title, it means your browser can’t display Unicode characters properly.

The Common Gateway Interface (CGI) is a standard protocol for interfacing external application software with an information server, commonly a web server. This allows the server to pass requests from a client web browser to the external application. The web server can then return the output from the application to the web browser.

Wikipedia

Today, I’ll explain how to generate Web pages using Python - the easiest programming language ever. They won’t be just static pages, however; in the article there will be examples of CGI scripts processing data from HTML forms, uploading files to the server, some GET and POST requests and even a simple how-to showing how to set up a small CGI server. However, this article is about using CGI in Python, so I assume you have some knowledge of the Python syntax.

The simpliest of the simpliest

Let’s start with a simple static page. The first line of any CGI script has to point to the location of Python. It’ll usually be /usr/bin/python on most Unix-based servers. Next, we have to inform Python that the output of the script is actually HTML (and put an \n at the end of this line)- and the rest of the code is up to you. Remember that all CGI scripts need permissions at least equal to chmod 755.

#!/usr/bin/python
print "Content-type: text/html\\n"
print "OMG, it's actually working!"

You can see the script working.
Of course, this script doesn’t even include the most basic html tags such as <html> and <body>, so have a look at a second example:

#!/usr/bin/python
print "Content-type: text/html\\n"
print """<html>
<head>
<title>OMG</title>
</head>
<body>
<h2>It's actually working!</h2>
<img src="some_crappy_image_source">
</body>
</html>"""

Again, you can see how it works.

(more…)

PHP can do graphics, too!

Monday, August 6th, 2007

Yeah, I still like PHP. All the drawbacks haven’t really convinced me to stop writing in PHP, as they all will be corrected either in the next updates of PHP 5 or in the forthcoming PHP 6. That’s why, despite many people saying “stp wrting php, u n00b!“, I still want to use this language and I still want to share my knowledge ;) So I decided to write a short tutorial explaining how to generate images using PHP.

Introduction

Among many PHP graphic extensions, two are the most functional: GD and Imlib2. However, only the former will be described here, as Imlib2 actually sucks. And so, you’ll have to check whether your server supports GD. Create a file where you call the function phpinfo(), open it and find the GD section. The table shows if GD is enabled on your server (if it isn’t, go to official GD site and download it or contact your admin) and what file types it supports.

Now that you’re sure that your server can actually do all the things mentioned in this post, let’s continue with it. But before we can get into the image generation, I have to make sure you take one thing into consideration: a site consisting of some text and two images sends three HTTP requests to the server. Each image makes the server process one more request, as it’s content type is other than the site’s (images use e.g. image/png type, whereas the whole pages use text/html or application/xhtml+xml). And so you must remember that you can’t print text and images within the same PHP file, as it will cause errors.

Let’s start with some simple image generation. First, we’ll use some simple GD colouring and drawing functions. ImageCreate(width, height) creates an image. Then, we’ll define some colours and assign them to variables with ImageColorAllocate(image, red, green, blue) and finally we’ll create a red rectangle using the ImageFilledRectangle(image, top_left_x, top_left_y, bottom_right_x, bottom_right_y, colour) function (its arguments are the top left and bottom right points’ coordinates). Next, we’ll have to send the header() to the server to tell it that the file’s an image, not text. Eventually, we’ll use a the ImagePNG(image [, filename]) creating the actual image.

<?php
$image = ImageCreate(150, 150);
$white = ImageColorAllocate($image, 255, 255, 255);
$red = ImageColorAllocate($image, 255, 0, 0);
ImageFilledRectangle($image, 50, 50, 150, 150, $red);
header('Content-Type: image/png');
ImagePNG($image);
?>

(more…)