modxcms / pThumb

A better phpThumbOf
46 stars 28 forks source link

pThumb 2.3.1-pl

A fork of phpThumbOf 1.4.0. pThumb is a lightweight, efficient, and actively maintained replacement for phpThumbOf. It offers most of the functionality of its predecessor while adding new features, fixing bugs, and offering some potentially dramatic speed improvements on sites which use phpThumbOf heavily.

Curious how pThumb compares to phpThumbsUp, etc.? I’ve got a wiki page with some observations and thoughts. There are a few other worthwhile pages there too.

Вопросы? Проблемы? Пишите по-русски!

Installation

pThumb is a drop-in replacement for phpThumbOf. It uses the same namespace, settings and component names, so after it's installed any code using phpThumbOf will automatically use the new version instead, with no further changes to the site required.

  1. Download pThumb via Package Management.
  2. Uninstall phpThumbOf if it's installed.
  3. Install pThumb.

Your phpThumbOf cache will be cleared in the process, but since pThumb generates slightly different filenames the images would have to be regenerated anyway.

(Actually you don't have to uninstall phpThumbOf first, but it makes things less confusing. If you uninstall phpThumbOf later, you'll have to reinstall pThumb.)

Documentation

pThumb includes two snippets: phpthumbof and pthumb. They're exactly the same; use whichever snippet name you like best. phpthumbof is handy for an existing site that was already using phpthumbof; pthumb better for future compatibility and shorter too :-)

Official documentation for phpThumbOf and phpThumb.

pThumb adds the following system settings:

and several new properties to the phpthumbof/pthumb snippets:

New pThumb Features

Resizer

[Requires PHP 5.3 or higher]

pThumb comes bundled with Resizer, a lightweight modern alternative to phpThumb. Built on Imagine, Resizer supports the Gmagick, Imagick and GD extensions and is considerably faster than phpThumb for image sizing and cropping operations. Plus all the ZC options now work with GD.

See the Resizer documentation for more on its requirements and supported options. Note that it doesn’t support any of phpThumb’s filters, but many of these things can be done with CSS nowadays.

To enable it, go to System Settings and under phpthumbof, change Use Resizer to Yes. You don't need to make any other changes to your site; pThumb transparently handles switching between them. You can even override the system setting for a particular pthumb call by using the &useResizer property (1 for yes, 0 for no).

pThumb Cache

New in version 2.1 is the pThumb Cache, an option which allows cleaner, more semantic and SEO-friendly URLs. Instead of everything being lumped into a one-level directory and having a 32-character hash appended, the new cache system stores thumbnails in subdirectories which mirror part of the original image's path and adds only an 8-character hash to filenames. Plus, pThumb still offers the original phpThumbOf-style cache. You switch between the two via a system setting.

Cache operation is controlled by three settings (in System Settings under phpthumbof):

Example: You've set up a media source for all the content images on your site and they're all in assets/acme/images/ Use that for the Image Base Directory setting. Leading/trailing slashes don't matter; pThumb will deal with them either way. You leave Cache Location set to the default. First you call pthumb on this image: assets/acme/images/products/whiz-o-matic/exploded-view-1.jpg. To create a cache filename your Image Base Directory value—and anything in front of it—will be replaced with Cache Location and the rest of the path used for the name, meaning you'll end up with a thumbnail URL of /assets/image-cache/products/whiz-o-matic/exploded-view-1.a9b0032f.jpg. Now suppose you've got some oddball image in assets/misc/clutter/junky-junk.jpg (that is, outside the Image Base Directory you set). No problem, it'll just go to assets/image-cache/junky-junk.922ebc0b.jpg.

Note: Switching cache systems won't migrate your cached images from one cache to the other; images be regenerated as needed. But it won't delete existing images either, so if you switch back they'll still be there.

Amazon S3

Version 2.3 adds S3 support, with some improvements over phpThumbOf. See this page for instructions on how to use it.

Remote Images

pThumb goes to some lengths to handle remote images well, whether they’re coming from an S3 media source or from some other server. It uses cURL to download the original image to assets/components/phpthumbof/cache/remote-images/, then uses that local copy for all future operations, meaning things are quite fast after the first run. Version 2.3 improves file naming for remote images, so the output thumbnail file names will be the same as if the original images were local.

Be aware though that once the remote image has been cached, pThumb won't look at the original again unless you delete the cached copy. Or as an alternative, you may use a query parameter for cache busting. For example if you have a TV called someImageTV, you can do something like this: <img src="https://github.com/modxcms/pThumb/raw/pThumb-dev/[[pthumb? &input=`[[*someImageTV]]?v=1` &options=`h=150&w=150`]]">. The first time through this will cause pThumb to download a new copy of the remote image and generate a new thumbnail.

Image Dimensions

Starting in version 2.3.1 pThumb can also output image dimensions. It provides two ways of doing this. The first is the &toPlaceholder property described above. Sometimes though placeholders can be tricky, like when they’re being used in a chunk being iterated over by something like getResources. In certain cases—if you don’t give the placeholders distinct names like &toPlaceholder=`img[[+idx]]`—all the placeholders will be filled in with the values from the last iteration.

However there’s another way to get image dimensions which doesn’t involve placeholders: add dims=1 to the options string and pThumb will output src, width and height all together. Like this: <img [[*imagetv:pthumb=`w=200&dims=1`]] alt="test image"> becomes <img src="https://github.com/modxcms/pThumb/raw/pThumb-dev/image-cache/test.d39f9375.jpg" width="200" height="300" alt="test image">.

Troubleshooting

If pThumb runs into a problem during thumbnail creation, it will simply return the input file name and exit. Usually this happens because some part of the input path or file name is incorrect and pThumb can’t find the image. A broken image link on the front end is a sure sign of this, meaning your web browser can’t find the image either. pThumb will actually correct for several common configuration mistakes and find the image anyway, but in some cases it can’t. Check the MODX error log for more insight into what’s going awry.

Changes from phpThumbOf 1.4.0

pThumb addresses the following open phpThumbOf issues:

In addition to that it: