NYCPHP Meetup

NYPHP.org

[nycphp-talk] PHPThumb -- how does it work??

Rob Marscher rmarscher at beaffinitive.com
Sat Jan 5 21:44:53 EST 2008


On Jan 4, 2008, at 6:18 AM, Jonathan Wagener wrote:
> Hi, I would like to know how I can make phpthumb work, I don't really
> understand how it works. How do I make it create thumbnails?

I haven't looked at phpthumb... but ImageMagick is sort of the  
"industry standard" for thumbnail creation.   Flickr uses it, for  
example.  It uses less system resources than GD and offers some easy  
methods to crop/constrain the thumbnails.

It's not actually part of php.  It's a standalone command-line  
application.  I believe there is a php extension for it... but it's  
not necessary, you can call command-line tools using the backtick  
operator.

The name command-line tool to create thumbnails with ImageMagick is  
called "convert" ("mogrify" can be used to resize a file and overwrite  
it).  Pretty much every *nix based host will have it available.  It's  
available for windows too -- http://www.imagemagick.org.

It's so simple... you'll love it.  Check this out:

<?php

$sourceImagePath = '/path/to/my/image.jpg';

// get string position of last dot (so we know where the file  
extension is)
$extPos = strrpos($sourceImagePath, '.');

// create thumbnail path by appending -thumb before the extension
// obviously can change this to work however you like
$thumbImagePath = substr($sourceImagePath, 0, $extPost) . "-thumb" .  
substr($sourceImagePath, $extPost);

// use ImageMagick to generate a thumbnail constrained to 100x100
$result = `convert $sourceImagePath -thumbnail 100x100 $thumbImagePath`;

?>

Want to get crazy and resize all jpg's to say 600x600 and create  
100x100 thumbnails and name them sequentially?

<?php

$sourceFolder = '/path/to/my/folder';
$result = `convert '$sourceFolder/*.jpg' -thumbnail 600x600 %03d- 
resized.jpg`;
$result = `convert '$sourceFolder/*-resized.jpg' -thumbnail 100x100  
%03d-thumb.jpg`;

?>

There's TONS more options for ImageMagick:  http://www.imagemagick.org/Usage

Good luck,
Rob




More information about the talk mailing list