Sep
09
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
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
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