Home | Reviews | GUIpedia | Forum | Fun500


pharoahPHP Image to QML Converter
I wrote a PHP script a while ago that uses a lookup table to convert images to QML on the fly. It's relatively fast, but the quality is inferior to the results produced by icon. I could improve the quality by making the LUT bigger, but I thought 800 k file (for a 64 x 64 x 64) LUT was large enough. Anyway, here is the code:
function render_image($url) { $lutfh = fopen('lut.bin', 'r'); // Load the file into a string, so we can use imagecreatefromstring and not have to check the file type $data = @file_get_contents($url); if (!$data) { return false; } // Some kind of problem occurred // Create the image buffer and resize it to be 78 pixels wide $source = imagecreatefromstring($data); $ratio = 78 / imagesx($source); $img = imagecreatetruecolor(78, round(imagesy($source) * $ratio) / 2); imagecopyresampled($img, $source, 0, 0, 0, 0, 78, imagesy($img), imagesx($source), imagesy($source)); // Convert each pixel's color value to a QML character using a fast lookup table $fg = 7; $bg = 0; $nl = true; $height = imagesy($img); $output = ''; for ($y = 0; $y < $height; $y ++) { for ($x = 0; $x < 78; $x ++) { $rgb = imagecolorat($img, $x, $y); $r = ($rgb >> 18) & 0x3F; $g = ($rgb >> 10) & 0x3F; $b = ($rgb >> 2) & 0x3F; fseek($lutfh, 3 * (4096 * $r + 64 * $g + $b)); $matchChar = fread($lutfh, 1); $matchFG = ord(fread($lutfh, 1)); $matchBG = ord(fread($lutfh, 1)); if ($fg != $matchFG) { $fg = $matchFG; $output .= "\n#$fg"; $nl = true; } if ($bg != $matchBG) { $bg = $matchBG; $output .= "\n:$bg"; $nl = true; } if ($nl) { $output .= "\n$"; } $output .= $matchChar; $nl = false; } $nl = true; $output .= "\n|"; } return $output; } You would also need the lookup table to make this work. Feel free to use this in your site, although keep in mind that you'll get better results if you use icon to preconvert an image that you're planning on using a lot (e.g. a banner), and you won't put so much load on your server. This is better for converting images from other sources (which can also be cached).
2011-03-293:58 PM

tacodrake95Re:PHP Image to QML Converter
Where do you get icon?
2011-03-303:05 PM

pharoahRe:PHP Image to QML Converter
You can search for it on cursor, it's a QML site. The URL is http://pharoah.xetaspace.net/icon. (It's what Brandon used for the QML GUI Blog logo).
2011-03-303:17 PM

tacodrake95Re:PHP Image to QML Converter
Ok, i think that i will use it for some cool stuff then.
2011-03-3111:47 AM

Other


2021 Brandon Cornell