Archive for April, 2009

Cakephp ThumbnailHelper

Tuesday, April 7th, 2009

This thumbnail helper is based on the one published by Studio Canaria. Additional features are multiple size caching and an extra method which is a truly drop-in replacement for $html->image().
Usage:

<?php

echo $thumbnail->image('picture.jpg', array(
 	'alt' => 'test thumbnail',
 	'width' => '100',
 	'height' => '75'
 	)
 );

// result:
// <img src="/img/thumbs/100x75/picture.jpg"
//	alt="test thumbnail" height="75" width="100">

?>

Installation:

  • Download thumbnail.txt
  • Rename it to thumbnail.php and place it in /app/views/helpers/
  • download phpThumb()
  • Extract the phpThumb archive in a directory named /app/vendors/phpthumb/
  • Add ‘Thumbnail’ to the ‘helpers’ array your controller
  • Create a directory named ‘thumbs’ in /app/webroot/img/. Make sure it’s writable for the webserver.

That’s it!
In the above example, the thumb helper will first check if there is a file named /app/webroot/img/thumbs/100×75/picture.jpg. If it isn’t there yet, it will instantiate phpThumb and create the thumbnail.
For more advanced usage, there’s also the render() method. It will give you the flexibility to use images located outside /app/webroot/img and also make use of most of the phpThumb options.
Advanced example:

<?php

$thumb = $thumbnail->render(
	$data['Picture']['path'],
	array(
		'w' => 320,
		'h' => 200,
		'ra' => 10
	),
	'5271a9e66c99c84c092d7fd81cebcd57'
);

echo $html->image($thumb);

?>

This will generate a picture named /app/webroot/img/thumbs/320×200/5271a9e66c99c84c092d7fd81cebcd57.jpg, which is a 10 degrees rotated thumbnail of the original image specified with $data['Picture']['path'].

explorerTree demo files

Monday, April 6th, 2009

tree.png
To save you some typing, this archive contains a working example of the tree helper code described in the bakery. The archive includes cake 1.2, three images and jquery. In the js-code, some hard-coded paths were corrected. It also contains a file named tree.sql which contains a dump of the database including some sample data.

cakephp, user_dir and mod_rewrite

Thursday, April 2nd, 2009

Here’s a little note on getting a cakephp app working in your public_html directory. In order to get the mod_rewrite configuration working, add a RewriteBase rule to the .htaccess files in the cakephp source. If your app is located in /home/me/public_html/cakesite/ and the URL to the site is http://host/~me/cakesite you’ll need to change the following .htaccess files:

/home/me/public_html/cakesite/.htaccess
/home/me/public_html/cakesite/app/webroot/.htaccess

Add the following line to these files, just below the line stating “RewriteEnginge On”:

RewriteBase /~me/cakesite/

For example, the first .htaccess file will look like:

<ifModule mod_rewrite.c>
	RewriteEngine on
	RewriteBase /~me/cakesite/
	RewriteRule    ^$ app/webroot/    [L]
	RewriteRule    (.*) app/webroot/$1 [L]
</ifModule>

Bonus notes with instructions for Debian/Ubuntu systems
You should have mod_rewrite enabled:

$ sudo a2enmod rewrite
$ sudo /etc/init.d/apache2 force-reload

To make sure the use of .htaccess files is actually allowed. In the Directory section of your apache configuration, there should be a line like:

AllowOverride FileInfo

Also, be sure to force-reload Apache after making this change.