Archive

Posts Tagged ‘PHP’

array() == false

March 7th, 2009

Yesterday I’ve noticed that too many queries are sent to the database server, even though the result should be cached in memcache. After digging into the source and testing, I’ve discovered something like this:

  1. $results = $memcache->get('key');
  2. if (!$results)
  3. {
  4.   $results = $this->fetchFromDb();
  5.   $memcache->set('key', $results);
  6. }
  7. return $results;

The fetchFromDb() method returned an empty array, which was stored in memcache. But, as it turns out, the bug was in checking of $results. An empty array evaluates to false when checking for true/false. I knew that already, but I missed this one.

So, the script issued a query, even though it already had a result. Luckily, this only happened with empty result sets, so the query was fast, and didn’t overload the server.

The correct code would be:

  1. if ($results === false)

I’ve lost quite some time over this, so I’m posting this as a reminder: if possible, use strict checking.

Author: Categories: Thoughts Tags: ,

WideImage 1.0 beta 2 released

February 19th, 2009

I’ve just released it. You can download it here. Optionally, you can use the 1.0-beta-2 svn tag.

This is the last release before RC1, which is coming soon (Q1 2009 expected). The main purpose for this release is stabilization, new unit tests, bug fixes, and some features. The online documentation is left behind (mostly still for beta 1), but no major API changes were made. The inline docs hold a more up-to-date documentation.

Magic dimensions
WideImage now supports an extended notation of dimensions for some operations (resize, crop, merge, …). The possible uses are:

  • integer/float – literal value is taken for coordinate
  • percent string – the coordinate in percent of the dimension
  • center-relative coordinate – specify the coordinate relative to center, rather than absolute

Examples:

// crops a 50x40 rectangle, starting at 10, 20
$cropped = $img->crop(10, 20, 50, 40);

// crops a 50% (half of the width) x20 rectangle,
//   starting at 0, 20% (20% of the height)
$cropped = $img->crop(0, '20%', '50%', 20);

// crops a 100x50 rectangle directly
//   in the middle of the image (center-relative coordinates are used)
$cropped = $img->crop('c-50', 'c-25', 100, 50);

These “magic dimensions” will acquire a less silly name (smart coordinates) in RC1, and will be developed further.

Loading from files without extension/mime-type
Is now supported. That’s it :) . I’ve decided to use imagecreatefromstring(file_get_contents($filename)), so now the image format is detected automatically. No more format or mime-type hinting is necessary. The other side-effect is that now WideImage suddenly supports all GD2-supported formats for reading. For writing, it’s still GIF, JPEG, PNG, GD, GD2. BMP is scheduled for RC1, but may end up in RC2.

Other stuff

  • AutoCrop — See demos in the package for details.
  • PS font support added
  • Canvas magic supported: the Canvas object now supports all functions that start with “image”, and you don’t have to pass an image handle to it.

    For example, to draw a red line:

    // you had to do this:
    imageline($image->getHandle(), 0, 0, 50, 50, $image->allocateColor(255, 0, 0));
    // now you can do this:
    $canvas = $image->getCanvas();
    $canvas->line(0, 0, 50, 50, $image->allocateColor(255, 0, 0));
    

    At first, it may not seem much, and maybe it isn’t. But at least I can say that Canvas supports custom drawing. :)

All in all, this is not a feature release. If you’re annoyed by bugs in beta1, or are looking forward to try AutoCrop, then upgrade, otherwise it’s not really necessary. :)

Author: Categories: WideImage Tags: ,