Here's a simply view helper for the Zend Framework that can be used to display image tags. It checks to see if the image file exists and if not then it'll use the data url scheme to output a very simple image that, ironically, says 'NO IMG' on it.
Please note, though, that I've only seen Firefox support this scheme, as wonderful as it is!
PHP:
-
<?php
-
-
class Zend_View_Helper_Img
-
{
-
public $view;
-
-
/**
-
* Constructor
-
*/
-
public function __construct()
-
{
-
if (null === self::$_baseurl) {
-
$url = Zend_Controller_Front::getInstance()->getRequest()->getBaseUrl();
-
if ('/' == $root) {
-
$root = '';
-
}
-
self::$_baseurl = $root . '/';
-
}
-
}
-
-
/**
-
* Output the <img> tag
-
*
-
* @param string $path
-
* @param array $params
-
* @return string
-
*/
-
{
-
$paramstr = null;
-
}
-
$params['alt'] = '';
-
}
-
foreach ($params as $param => $value) {
-
$plist[] = $param . '="' . $this->view->escape($value) . '"';
-
}
-
return '<img src="' .
-
((self::$_cache[$path])
-
: 'data:image/gif;base64,R0lGODlhFAAUAIAAAAAAAP///yH5BAAAAAAALAAAAAAUABQAAAI5jI+pywv4DJiMyovTi1srHnTQd1BRSaKh6rHT2cTyHJqnVcPcDWZgJ0oBV7sb5jc6KldHUytHi0oLADs=') .
-
'"' . $paramstr . ' />';
-
}
-
-
/**
-
* Set the view object
-
*
-
* @param Zend_View_Interface $view
-
* @return void
-
*/
-
public function setView(Zend_View_Interface $view)
-
{
-
$this->view = $view;
-
}
-
}
Usage is really easy:




Just update the code so that it uses the escaping method of the view (rather than htmlentities directly) and also to always have an alt parameter, even if it’s empty.