Example 492. Determining Supported Features
You may wish to direct the user to specific areas of your site based on features supported by the device they are using. For instance, if a particular app works only in Flash, you might direct a non-Flash-capable device to a page indicating the application will not work on their device; or for a device not capable of HTTPS, you may indicate certain actions, such as purchases, are not available.
<?php
$userAgent = new Zend_Http_UserAgent();
$device = $userAgent->getDevice();
// Redirect to a Flash version of the app:
if ($device->hasFlashSupport()) {
header('Location: /flash/app');
exit;
}
// Determine whether to show a "purchase" link:
if ($device->httpsSupport()) {
echo '<a href="https://store-site.com/purchase">Purchase!</a>';
} else {
echo 'Purchasing is unavailable on this device.';
}
Example 493. Dynamically Scaling Images
You may wish to alter the image sizes present in order to achieve a specific design within mobile devices. You may use a variety of methods in the device object to make this happen.
<?php
$userAgent = new Zend_Http_UserAgent();
$device = $userAgent->getDevice();
// Get the maximum image width and height
$maxWidth = $device->getMaxImageWidth();
$maxHeight = $device->getMaxImageHeight();
// Create an <img> tag with appropriate sizes
echo '<img src="/images/foo.png"';
if ((null !== $maxWidth) && ($maxWidth < 328)) {
echo ' width="' . $maxWidth . '"';
}
if ((null !== $maxHeight) && ($maxHeight < 400)) {
echo ' height="' . $maxHeight . '"';
}
echo '/>';
Markup- based scaling is not ideal
Markup-based scaling such as in the example above is not the best approach, as it means that the full-sized image is still delivered to the device. A better approach is to pre-scale your images to a variety of sizes representing the devices you wish to support, and then using the device capabilities to determine which image to use.
Another approach is to use third-party services. Zend Framework provides one such capability via the TinySrc view helper.




