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.
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.
Source is available on BitBucket: https://bitbucket.org/epb9000/image-color-dominator/ 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.
I produced an entry for this month’s Ludum Dare. It’s a traversal memory game where you have to find your way back home, but you can only pick one path forward, picking wrong forces you to reset.
A brief description:
Marvie is spacefaring alien and can only travel along asteroids. But Marvie has a problem.
He kind of disintegrates every asteroid he touches. He can’t help it. His psychologist says the condition will only get worse with age. How tragic.
Link to Play: http://iris.kiristar.com/mv3/
Link to Source: https://bitbucket.org/epb9000/marvie/
There may be times you need to find the bounding box of an SVG path. One way to find it would be to parse the path and then apply any group transformation on it to find the range of X and Y coordinates the object occupies. However that’s a bit of code, and I’m quite a bit lazy, so I decided to try another approach: rasterize the path object by itself and find where in the image it was drawn.
I really don’t have a shorter description for this. I was working on a personal project the other day and realized that this might be a nifty function. Basically, you know all those snippets to make cropped thumbnails (squares and the like), and how most of them either center or crop to the top of an image. I wanted a new function that would crop to most any size I desired and any position on the image I desired without the need to change any code, only parameters. And thus img_resize_crop was born.
An odd quest perhaps: but adding album art to an MP3 (in ID3 tags) is readily doable with the KTaglib extension for PHP. Once you have it, you can use a script like this to add artwork to an MP3:
$mp3 = new KTaglib_MPEG_File('2.mp3');
$idv2 = $mp3->getID3v2Tag(true);
$album = new KTaglib_ID3v2_AttachedPictureFrame();
Assuming no exceptions are thrown, this should allow you to set an image of your choosing as a FrontCover image (of course replacing my example values with your own real values), consult the KTaglib documentation for more details and check “Predefined Constants” for the other image types.
You can do quite a bit of unorthodox stuff with PHP. If you head over to epbsoft.com, you’ll find several flash coloring sheets I made with PHP, not Flash. (Sort of anyway.) I make them by first creating specially formatted SVG (Scalable Vector Graphics) files and feeding them to a PHP script that writes ActionScript files that are then compiled by MotionTwin ActionScript compiler. As far as capabilities, it’s not all that capable. I have to jump through a couple of hoops to make an SVG that my ActionScript generator can actually parse properly, but when it works, I like the results.
svg-to-coloring.php.zip <– The source code is available if you want to take a look at how it works.