lilixiaocc
lilixiaocc

Reputation: 343

Use HtmlHelper to add attribute for html tags, CakePhp

I am working on a project with cakephp right now. And I am wondering if there is a way to add attribute for a img tag, this is in my view:

Here is my code:

echo '<img id="image">';
echo $this->Js->get('#image', 
                     array('htmlAttributes' => array('src' => 'somesourcehere')));

Edit:

I found another way to do this without using JsHelper, but use HtmlHelper,

echo $this->Html->image('somesourcehere',array('alt' => 'CakePHP'));

But the issue is the src attribute would be url like, while I was trying to set the src as base64 data, so it would be something like this in HTML

<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==">

However, I can only get

<img src="/img/data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==">

Is there a way to get rid of the /img/part in my src ? So I can make the image show up

Update I am too dumb, only need to do

echo '<img src="data:image/png;base64,'.$source.'">';

Upvotes: 1

Views: 639

Answers (1)

Salines
Salines

Reputation: 5767

CakePHP Html image method produce relative or absolute src path to images.

In your case if need to use Helper, then use HtmlHelper method called tag:

<?php
    echo $this->Html->tag(
        'img', 
         null, 
         array(
            'id' => 'image',
            'src' => 'data:image/png;base64,'.$source'
        )
   );
?>

or just simple combine html and php:

<img src="data:image/png;base64, <?php echo $source; ?>">

Upvotes: 1

Related Questions