Posts Categorized: php

Image Color Dominator

Most anyone who’s used iTunes’ album view will know about their color picking mechanism.  I was curious about how this might be done and found this question on Stack Overflow: How does the algorithm to color the song list in iTunes 11 work?

So I set about creating an equivalent in PHP.  I settled on using the IMagick PHP extension for grabbing pixel data and doing blurs.

The Results

mizu Mizu (Water)

moon Tsuki (Moon)

fall Aki (Fall)

Chibi Chibi ^_^

The How

It’s largely based on the accepted answer given on the Stack Overflow question mentioned earlier. Except I separate potential highlights from the selected background based on a luminance ratio which produces more readable text, and there’s some additional logic for trying to pick contrasting highlight colors.

The Source

Source is available on BitBucket: Not much in the way of documentation, so hopefully the comments on the class aren’t horrifically confusing. I’ll re-read them later and maybe make changes.

Repeatable Pseudo Random Number Generator (PHP)

I wrote a simple class to get repeatable random numbers out of an input seed.  I’ll post it here in case someone else would find such a thing useful.  It is based on SHA1 rounds. After a number of any kind is fetched, another round of SHA1 is applied.

Download it here: RepeatablePseudoRNG

The class has 3 different methods for retrieving random numbers.

Continue reading

TCPDF Does not Support Indic scripts

The other day I ran across an interesting question on Stack Overflow regarding the use of indic fonts with TCPDF.  Languages like Tamil and Malayalam fall into this category. [How can I create Malayalam PDF using TCPDF in PHP?] At first I thought it was just a simple font issue, but that was not the case.  Even with font subsetting off, and trying a number of different Malayalam capable fonts, I was having the same problem the question asker was facing. I began googling for information regarding Malayalam and TCPDF, but to no avail.  I found out that Tamil was a related script, so I did searches on that and the outlook was not good regarding proper rendering in TCPDF.

Finally after switching my google queries to specifically search for information about indic script support in TCPDF, I found this comment by a person called “Santhosh” regarding TCPDF’s lack of indic script support:

It is technical limitation. For TCPDF, the true type font need to be converted to afm format first, then for each script, the diacritics or ligature rules are implemented in tcpdf itself. That is what done for adding Arabic/Persian support. For complex scripts this is not a correct approach. Indic shaping engines like Pango has evolved by taking about 10 years. The shaping logic is very complex and duplicating it inside a PDF library is wrong approach. Instead the PDF library should depend on Pango or the upcoming Harfbuzz rendering engines. The PDF export library in Mediawiki uses reportlab pdf library. That also attempts to the rendering by itself. And ended up in having no support for any Indic languages and many bugs for Arabic scripts(Note that this extension is disabled in many Indian wikiprojects). Fonts are not enough for rendering, a shaping engine is also required for complex script to interpret the glyph formation rules. This is what PyPDFLib is trying to solve by using Pango for script rendering and Cairo for graphics.

I’m happy to have come across his posts as I might’ve banged my head against a wall trying to answer this Stack Overflow question for a while.  I don’t know anything about these languages so I don’t think I could have answered the question as quickly or as thoroughly as I did if it weren’t for finding this.

You can read the full blog post and comment thread here: Creating a new Language ecosystem- Sourashtra as example

ImageMagick: Rounded-corner PHP function

I’ve converted my rounded-corner bash script into a PHP function that’ll use the Image Magick command line tools to do the same. With some added functionality to handle automatically creating thumbnails and applying a background color to the source image itself. This also allows for the insertion of basic pre-processing and post-processing arguments to IM.

For more information and output examples see. the bash version.

To see an example of thumbnails generated with some pre-processing done:

First Preprocessor Test

This is a sample done with thumbnail pre-processing. That is processing done before the rounded corners are made.

With this sample, I switched colorspace to HSL and then set the hue and saturation to a constant before applying the glass-bubble rounded corners.

Known bug: sometimes the glass-bubble method will add a single pixel to the width, which is transparent. I believe passing -trim in the postprocessing option may trim that off for you, though I have not thoroughly tested it.

Also: I have no idea if this will work on a Windows server.

Continue reading

PHP: Get zipcode for an Address

I needed a small php function that could take an address (which lacked a zip code) and fetch a best guess postal code for it. I had a database full of addresses with city and state, but no zip code. Thankfully, if you have enough information in your address to locate it with the Google maps geocoding service, you have enough information to get the zip code. If not the correct zip code, it should pull one that’s relatively close assuming you’ve specified the correct city and state in your address.

This is possible because the geocoding service will return matches with full address data available. If we assume that the first match is most likely the correct match, we can just extract it’s postal code. I’ve only tested this with US addresses, but it may work in other countries where Google can geocode an address.

Continue reading

I feel cryptastic

Looking for a quick and easy to use encryption class for PHP?  I stumbled upon Cryptastic in my searches for example code to use with mcrypt for a personal project.  I must say it’s quite handy. It implements a message integrity check and handles the initialization vector stuff for you making encrypting data as easy as a single call.