7.4.1.6 image
image(string $path, array $htmlAttributes = array())
Creates a formatted image tag. The path supplied should be relative to /app/webroot/img/.
<?php echo $html->image('cake_logo.png', array('alt' => 'CakePHP'))?>
<?php echo $html->image('cake_logo.png', array('alt' => 'CakePHP'))?>
Will output:
<img src="/img/cake_logo.png" alt="CakePHP" />
To create an image link specify the link destination using the url option in $htmlAttributes.
<?php echo $html->image("recipes/6.jpg", array(
"alt" => "Brownies",
'url' => array('controller' => 'recipes', 'action' => 'view', 6)
)); ?>
<?php echo $html->image("recipes/6.jpg", array("alt" => "Brownies",'url' => array('controller' => 'recipes', 'action' => 'view', 6))); ?>
Will output:
<a href="/recipes/view/6">
<img src="/img/recipes/6.jpg" alt="Brownies" />
</a>
You can also use this alternate method to create an image link, by assigning the image to a variable (e.g. $image), and passing it to $html->link() as the first argument:
<?php
$image = $html->image('recipes/6.jpg', array(
'alt' => 'Brownies',
));
//$image is passed as the first argument instead of link text
echo $html->link($image, array(
'controller' => 'recipies',
'action' => 'view',
6
),
array(
'escape' => false //important so htmlHelper doesn't escape you image link
)
);
?>
<?php$image = $html->image('recipes/6.jpg', array('alt' => 'Brownies',));//$image is passed as the first argument instead of link textecho $html->link($image, array('controller' => 'recipies','action' => 'view',6),array('escape' => false //important so htmlHelper doesn't escape you image link));?>
This is useful if you want to keep your link and image a bit more separate, or if you want to sneak some markup into your link. Be sure to pass 'escape' => false in the options array for $html->link($string, $url, $options) to prevent htmlHelper from escaping the code.


























