Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP Absolute Image URLs

Tags:

url

cakephp

I wish to use the $html->image(...) helper function in CakePHP to output images, but I need it to produce an img tag using an absolute rather than relative URL (the resulting HTML will be downloaded and emailed round in a company newsletter). Is this possible?

It's not documented, but I notice from looking at the source code that the image function can take an array as its first argument. It's not entirely clear to me how to get this working though - a naive attempt to do it this way produces image URLs relative to the current page rather than within the webroot/img folder.

like image 807
andygeers Avatar asked Jun 24 '09 14:06

andygeers


3 Answers

In CakePHP 1.x the method is:

$this->Html->image($this->Html->url('/path_to_image/image.png',true));

In CakePHP 2.x the method is:

$this->Html->image('/path_to_image/image.png', array('fullBase' => true));
like image 90
JD Isaacks Avatar answered Oct 09 '22 20:10

JD Isaacks


The correct way to do this is in CakePHP 2.x:

<?php
echo $this->Html->image("logo.png", array('fullBase' => true));

Code pulled direct from the cookbook: http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html

like image 29
WebbedIT Avatar answered Oct 09 '22 21:10

WebbedIT


$html->image('http://example.com/path_to_image/image.png'); should do the trick.

like image 1
dhofstet Avatar answered Oct 09 '22 21:10

dhofstet