rss
logo

I provide consulting and custom development for Natural Language Processing, Information Extraction and Search solutions.Self Picture


 learn more   get in touch 

Logo - I Build Search
Sep
09

Using PHP and ImageMagick to resize images digg

Today, I had to write some code to generate thumbnails in PHP. The php-gd library wasn’t installed and I had to work with ImageMagick. Not the most elegant of solutions, but it works:

functions.php
1
2
3
4
5
6
7
8
9
10
11
define('PRAVIN_THUMBNAIL_DIR', '/home/firedev/public_html/wp-content/cache/thumbnails/');
function pravin_resize($img_path, $width, $height) {
    $resolution = '"' . $width . 'x' . $height . '"';
    $output_path = PRAVIN_THUMBNAIL_DIR . md5($img_path) . "-$resolution.jpg";
    // If file does not exist OR the thumbnail was generated more than 
    // 5 mins (5 x 60 sec) then re-create the thumbnail
    if(!file_exists($output_path) || (time() - filemtime($output_path)) > (5 * 60)) {
        system("/usr/bin/convert -resize $resolution $img_path $output_path");
    }
    return $output_path;
}
Mar
28

Hacking wp-syntax plugin to show header digg

I was recently asked how I got the wp-syntax plugin to show a header like so:

test.cpp
1
2
3
int main() {
	return 0;
}

To show the test.cpp file name, I modified the wp-syntax.php file (present in /wp-content/plugins/wp-syntax/) like so:

Changed the regular expression in the wp_syntax_before_filter function from:

wp-syntax.php
function wp_syntax_before_filter($content)
{
    return preg_replace_callback(
        "/\s*<pre(?:lang=[\"']([\w-]*)[\"']|line=[\"'](\d*)[\"']|escaped=[\"'](true|false)?[\"']|\s)+>(.*)<\/pre>\s*/siU",
        "wp_syntax_substitute",
        $content
    );
}

to

wp-syntax.php
function wp_syntax_before_filter($content)
{
    return preg_replace_callback(
        "/\s*<pre(?:lang=[\"']([\w-]*)[\"']|line=[\"'](\d*)[\"']|escaped=[\"'](true|false)?[\"']|header=[\"']([\w-\. ]*)[\"']|\s)+>(.*)<\/pre>\s*/siU",
        "wp_syntax_substitute",
        $content
    );
}

And the wp_syntax_highlight function to:

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
function wp_syntax_highlight($match)
{
    global $wp_syntax_matches;
 
    $i = intval($match[1]);
    $match = $wp_syntax_matches[$i];
 
    $language = strtolower(trim($match[1]));
    $line = trim($match[2]);
    $escaped = trim($match[3]);
    $header = trim($match[4]);
    $code = wp_syntax_code_trim($match[5]);
    if ($escaped == "true") $code = htmlspecialchars_decode($code);
 
    $geshi = new GeSHi($code, $language);
    $geshi->enable_keyword_links(false);
    do_action_ref_array('wp_syntax_init_geshi', array(&$geshi));
 
    $output = "\n<div class=\"wp_syntax\">";
 
    if($header) {
        $output .= "<div class=\"wp_syn_hdr\">" . $header . "</div>";
    }

Node the addition of lines 104 and 114-116

All you have to do is add another attribute header="header-text" in your pre tag. ex. <pre lang="php" line="1" header="wp-syntax.php">

Dec
28

Limerick Generator – Part I digg

Synopsis

The following code demonstrates an alien limerick generator. Alien because it does not generate meaningful words. In part II, we’ll see how we can add meaningful words.

Code

You can view the code and see it in action here.

Sample Output

orraxa iz hoxori upifoc awhapo ed
ceva ovat ujbazo arli du caded
uc iqek suenvo
zaek ammiip edbeuvo
moix beak kuefyo irogno iluyox xoiqged

Latest Articles

Feb
19

Join a list of integers in Python

How do you run a string join on a list of integers in Python? After googling for about 10 mins, I gave up and did this. I am sure there is a better way of doing it! [Read More]
Jan
21

Writing a spider in 10 mins using Scrapy

I came across Scrapy a few days back and have grown to really love it. This tutorial will illustrate how you can write a simple spider using Scrapy to scrape data off Paul Smith. All this in 10 minutes. [Read More]

Featured Projects

Document Tagger

Document Tagger

DocTagger lets you automatically classify text documents. Use this as a starting point to write apps that can sort through volumes of unorganized data.

[Read More]

NLP classes for PHP

NLP classes for PHP

This is an ongoing project to develop a set of classes for Natural Language Processing. Some code would be ported from the NLTK project.

[Read More]

This page and its contents are copyright © 2010, Pravin Paratey.