From the course: PHP Tips, Tricks, and Techniques

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Batch convert images to data URIs

Batch convert images to data URIs

- [David] Hi, I'm David Powers and welcome to this week's edition of PHP Tips and Tricks designed to help you become a smarter, more productive PHP developer Choose Cheese. Well, tempting though that prospective is the focus this week is on these icons of national flags of various countries. At the moment they're separate images and each one requires a separate request to the server. To reduce the load on the server they could be combined into a CSS sprite but that involves troublesome calculations to get the correct background position. The alternative is to use what's known as a data URI. This involves encoding the graphic data for each image as a string ready to embed it in either a style sheet or into HTML. Using PHP to batch process images to data URIs is quick and easy, so that's what we'll do this week. The format of a data URI is simple. It begins with data followed by a colon. That's followed by the MIME type and a semicolon. So, if it's a JPEG, the MIME type is image/jpeg…

Contents