
In our last example, we retrieved the ten most recently uploaded images from Director and displayed them on a page. However, we could only display the original image, which is often times very large. Let's look at how we can return an optimized thumbnail from Director's on-demand image publishing system.
First, here is what we have now:
<?php
include('classes/DirectorPHP.php');
$director = new Director('your-api-key', 'your-api-path');
$recent = $director->content->all(array('limit' => 10, 'only_images' => true));
foreach($recent as $content) {
echo '<img src="' . $content->original->url . '" />';
}
?>
The DirectorPHP class contains a handy subclass known as Format. We'll use this to specify what versions of each image we want returned. The format class returns the URL for the optimized image as well as the width and height that the new version will be created at:
<?php
include('classes/DirectorPHP.php');
$director = new Director('your-api-key', 'your-api-path');
$format = array(
'name' => 'thumb',
'width' => '100',
'height' => '100',
'crop' => 1,
'quality' => 75,
'sharpening' => 1
);
$director->format->add($format);
$recent = $director->content->all(array('limit' => 10, 'only_images' => true));
foreach($recent as $content) {
echo '<img src="' . $content->thumb->url . '" width="' . $content-> thumb->width . '" height="' . $content-> thumb->height . '" />';
}
?>
You can specify as many formats as you like, just make sure you do so before you make any calls for data. In this example, we specify two different formats and link the thumbnail version to a larger version.
<?php
include('classes/DirectorPHP.php');
$director = new Director('your-api-key', 'your-api-path');
$format = array(
'name' => 'thumb',
'width' => '100',
'height' => '100',
'crop' => 1,
'quality' => 75,
'sharpening' => 1
);
$big = array(
'name' => 'big',
'width' => '1000',
'height' => '1000',
'crop' => 0,
'quality' => 95,
'sharpening' => 1
);
$director->format->add($format);
$director->format->add($big);
$recent = $director->content->all(array('limit' => 10, 'only_images' => true));
foreach($recent as $content) {
echo '<a href="' . $content->big->url . '">';
echo '<img src="' . $content->thumb->url . '" width="' . $content-> thumb->width . '" height="' . $content-> thumb->height . '" />';
echo '</a>';
}
?>