Installing GD2 on Linux and Windows

PHP: GD2 Not installed:|| File and or directory is invalid

First you may be wondering what GD2 is… the official explanation is this “GD is an open source code library for the dynamic creation of images by programmers.” If you have ever come across this error it is probably because you have not installed the GD2 extension in PHP when you first setup your server. So I will let you know how install it in Linux and Windows.

Under Linux: The easiest way is to use your package manager to install it. For example in Debian… “apt-get install php5-gd” or Redhat… “—with-libdir=lib64 —with-gd —with-jpeg-dir —with-png-dir

Under Windows: You will have to download the .tar archive because the binary doesn’t include any extensions. So first make sure that in the “C:\PHP\ext” direct that you see “php_gd2.dll” if you do not see you will have to download it. You can obtain a copy by downloading the PHP5 .tar. Next you have to open your “php.ini” file search for “Dynamic Extensions” and just under that point PHP to the library file like this “extension=php_gd2.dll”

Note: make sure that your php.ini file is configured to look in the “C:\PHP\ext” folder for the extensions. Search in the file for “extension_dir”.

And there you have it you now have full GD2 support for PHP. You can now resize, scale, flip images on the fly.

Python on AIX 6.1

To start, I'm not an AIX expert. I'm just a hobbiest that wanted to get a python/django/apache/mysql (db2 down the road) environment work in AIX.

Installing python:
Download and compile the latest python source.

# cd ~/
# wget http://www.python.org/ftp/python/2.6.4/Python-2.6.4.tgz
# gunzip Python-2.6.4.tgz
# tar -xvf Python-2.6.4.tgz
# cd Python-2.6.4
# export PATH=/usr/bin:/usr/vacpp/bin

for 32bit:
# ./configure

for 64bit:
# ./configure —with-gcc=”xlc_r -q64” —with-cxx=”xlC_r -q64” —disable-ipv6 AR=”ar -X64”

# make
# su
# make install


If no problems crept up, the python executable should be available here:
/usr/local/bin/python


The apache installation to next to come!

HTML to PDF in Django

I’ve been involved in a number of project where I need to create pdf reports from a set of data using the Django framework. The easiest way for me to do this is generate the pdf directly from html.

I will provide a list of software required, and how to program a django view to generate the pdf.

Here’s the software you’ll need:

pisa: http://pypi.python.org/pypi/pisa/

ReportLab: http://www.reportlab.org/downloads.html

html5lib: http://code.google.com/p/html5lib/downloads/list

Here’s a simple view to generate the pdf.

#view.py

import cStringIO as StringIO
import ho.pisa as pisa
from django.http import HttpResponse
from django.template.loader import render_to_string

def my_view(request, *args, **kwargs):

    html = render_to_string('some_template.html', RequestContext(request, {
        'title': 'This is a test PDF document'
    }))
    result = StringIO.StringIO()
    pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)
    response = HttpResponse(result.getvalue(), mimetype='application/pdf')
    response['Content-Disposition'] = 'attachment; filename=document.pdf'
    return response

Enjoy

Setup git on Centos 5.3, Centos 5.4 and make public over ssh

update oct 22. 2009: This method also works in Centos 5.4. I’ll post the epel think below once they’re available

I setup git on a Centos 5.3 server the other day, accessible over ssh. Here are the steps I followed to get things up and running.

Centos’s yum repository does not have git in it, so setup fedoras epel (Extra Packages for Enterprise Linux) repo.

i386:

    rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm

x86_64:

    rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/x86_64/epel-release-5-3.noarch.rpm

As root run this command:

    yum install git

Next I’m going to setup a new repository and make it accessible over ssh:

    mdkir /home/username/repo #create directory for new repository

    cd /home/username/repo

    git init

We’ll create a dummy file to get started. If you trying to clone (checkout) an empty git repository, you’ll just get errors:

    touch firstfile

Add all files in this directory to your git repository:

    git add .

Commit the changes you’ve made to the repo:

    git commit

Next we’ll create a clone of the repo and configure it to be public:

    cd /home/username

    git clone --bare ./repo repo.git

    touch  repo.git/git-daemon-export-ok

you can copy your repo.git directory to where you want to make the repo public

Now we want to checkout a copy of the new repository from a different server.

    git clone ssh://yourserveraddress/home/username/repo.git

You should now have a new directory labeled repo which contains the file ‘firstfile’

To add a new file to the repo:

    cd repo

    touch secondfile

    git add .

    git commit

Now we want to submit the changes back to the git server:

    git push

You’ll be prompted for your password.

Enjoy.

Add confirm dialog to a link with jQuery

Here’s another simple jQuery snippet I like to add to my projects. This will allow you to present a javascript confirm dialog when a user clicks on a link or a submit button with a confirm attribute it. The value of the confirm attribute will be the text displayed in the confirm dialog box. I often use this for delete button so the user has a chance to back out.

$(function() {
    $("a[confirm], input[type=submit][confirm]").click(function() {
        conf = confirm($(this).attr("confirm"));
        if(!conf)
            return false;
    });
}

Turn table rows and buttons into clickable links with jQuery

This simple little script often comes in quite handy when I’m working on a web app. It allow you to add an href attribute to tr, td, or input[type=button] tags to make them clickable links

$(function() {
    $("input[type=button][href], tr[href], td[href]").click(function() {
        location.href = $(this).attr("href");
    });
});

Dynamically change field attributes in a Django ModelForm

I’ve encountered situations using Django’s ModelForm where I’ve needed the form to validate differently then the default behavior in various instances. Here’s an example of how I changed all the fields from the default of not being required to all being required without much effort:

class AddressRequiredForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(AddressForm, self).__init__(*args, **kwargs)
        for name, field in self.fields.iteritems():
            if name not in ['address2',]:
                field.required = True

    class Meta:
        model = Address

This is pretty simple, but it saved me a lot of time by not having to re-type each field name with a new set of properties.

Hello World

I’m testing out this Django based blog engine named byteflow. I was considering starting my own from scratch, but I really don’t have the time to pump out anything half decent. I came across this engine and decided to start from there.

The only requirement I had for a blog engine was for it to use the Django framework. I spend (almost) every day writing code using django, so I wanted something I would feel comfortable modifying. So here I am.. hopefully this site will help me to organize my thoughts.

This is primarily a place for me to record and recall ideas and information, so content you may see here (if any) will likely be scattered with no underlying theme.

I will probably keep comments open for anyone to post until I start seeing spam. So feel free to share your thoughts.


 Luke