简体   繁体   中英

base64 encoding for image src

When I echo the following:

$im_dec = base64_decode($row['image']);

I obtain the desired URL:

https://www.ft.com/__origami/service/image/v2/images/raw/http%3A%2F%2Fprod-upp-image-read.ft.com%2F1263ad72-2d9a-11e7-9555-23ef563ecf9a?source=next&fit=scale-down&compression=best&width=210

Then I use this URL to be the src of my img:

$newImage = $dom->createElement('img');
$newImage->setAttribute("src", $im_dec);
$newImage->setAttribute("class", "articleImage");
$newTitle->appendChild($newImage);

And when I check the src attribute in my html document, I get a modified url where & is replaced by & for example and many more weird stuff..

Some characters were modified and I don't know how to avoid it. I tried many things but I thought base64 encoding would work...

Help please!

You can convert an image to base64 encoding with the following example:

$path = 'myfolder/myimage.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM