Do you have some feature in mind that you'd love to see in Couch? Let us know.
9 posts Page 1 of 1
Hi there,

Wouldn't be great to convert small images to base64 in order to minimize server requests?
I like the idea of an addon, with a custom tag or extension of current ones.
Solution, can be used if you know, that files are small:

usual couch code:
Code: Select all
<img src="<cms:show logo />" alt="" title="" />

for base64 encoding output use this:
Code: Select all
<img src="data:image/png;base64,<cms:php>echo base64_encode(file_get_contents("<cms:show logo />"));</cms:php>">


Hope it helps to save valuable milliseconds on cheap hosting :D
I also assume there should a way to tweak a <cms:thumbnail> addon to create a custom Couch tag, like <cms:base64>.

PS
@KK, I tried this approach in dev environment with no issues, but on production it didn't work out of the box. What would you suggest?
but on production it didn't work out of the box
I'm not surprised. The 'file_get_contents' function you used is normally restricted from accessing files using 'http://'.

We'll need to convert the URL of the image to its local physical path and then it should work everywhere.

My suggestion is - why don't you try creating a new Couch tag for base64 encoding?
The existing code for <cms:thumbnail> (tag.php - function thumbnail()) can be tweaked to create this tag.
convert the URL of the image to its local physical path

Just checked, everything is working as supposed on production with relative path like /upload/image.png.

I'm sorry, instead of <cms:thumbnail>, I wanted to mention <cms:watermark>, my bad :)
I will try to play with :thumbnail, though my php is totally based on stackoverflow.com :lol:

Thank you, KK
Got to get back here.
This code works fine to auto-convert image to base64.

Code: Select all
<img src="data:image/png;base64,<cms:php>echo base64_encode(file_get_contents(ltrim(parse_url('<cms:show logo />', PHP_URL_PATH), '/')));</cms:php>"/>

First, we get a full URL of the logo image by cms:show logo
Result: http://mysite.my/upload/image/other/logo.png

Then, I use parse_url function from php. I make it extract only path without domain.
Result: /upload/image/other/logo.png

Then we need to remove a slash on the left with the php function ltrim.
Result: upload/image/other/logo.png

After such mod everything else works great. Even large images can be converted on the fly.
I checked with over 1mb of images and total like 10 images at page load. Didn't notice slow downs. Worked pretty fast. But I'm only using it for a bunch of small pics, like icons and logo. Saved requests and 503 errors with cheap hosting.
Hey guys so it's very very very cool! :shock:
So, you found this helpful :D
trendoman wrote: Hi there,

Wouldn't be great to convert small images to base64 in order to minimize server requests?
I like the idea of an addon, with a custom tag or extension of current ones.


Naturally this should be done only for very small images, like icon and so on... ;)
9 posts Page 1 of 1

Who is online

In total there is 1 user online :: 0 registered, 0 hidden and 1 guest
(based on users active over the past 5 minutes)

Users browsing this forum: No registered users and 1 guest

cron