<?php
/**
*+----------------------------------------------------------------------+
*| Pic2X v1.5 - 11 Mar 2006 |
*+----------------------------------------------------------------------+
*| Copyright (c) 2005-2006 Diego do Nascimento Feitosa |
*| hide@address.com |
*| www.dnfeitosa.com |
*| São Paulo/SP - Brasil |
*+----------------------------------------------------------------------+
*| Thanks to Edison Abreu (hide@address.com) for help with |
*| some functions and the optimization!! |
*+----------------------------------------------------------------------+
*| Changelog: |
*| |
*| v1.0 -> v1.5 |
*| ---------- |
*| |
*| - Optimization of the algorithm (thanks Edison!!) |
*| - Better suport for images with transparency |
*| |
*| - Old versions can be found at www.dnfeitosa.com/pic2x/ |
*+----------------------------------------------------------------------+
*| Pic2X is free software; you can redistribute it and/or modify |
*| it under the terms of the GNU General Public License as published by |
*| the Free Software Foundation; either version 2 of the License, or |
*| (at your option) any later version. |
*| |
*| Pic2X is distributed in the hope that it will be useful, but |
*| WITHOUT ANY WARRANTY; without even the implied warranty of |
*| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
*| General Public License for more details. |
*| |
*| You should have received a copy of the GNU General Public License |
*| along with Pic2X; if not, write to the Free Software |
*| Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
*| 02111-1307 USA |
*+----------------------------------------------------------------------+
**/
class Pic2X {
var $resource;
var $charindex = array(
" ",".","+","@","#",'$',"%","&","*","=","-",
";",">",",","'",")","!","~","{","]","^","/",
"(","_",":","<","[","}","|","1","2","3","4",
"5","6","7","8","9","0","a","b","c","d","e",
"f","g","h","i","j","k","l","m","n","o","p",
"q","r","s","t","u","v","w","x","y","z","A",
"B","C","D","E","F","G","H","I","J","K","L",
"M","N","O","P","Q","R","S","T","U","V","W",
"X","Y","Z","`"
);
var $base;
var $pixsize = 1;
var $numColors;
var $palette;
var $image;
var $sections = null;
var $isBuilt = false;
var $header;
var $filename = "image";
function Pic2X($resource, $compression = true) {
$this->base = count($this->charindex);
if (is_resource($resource)){
$this->resource = $resource;
} else {
$this->filename = str_replace(".", "", $resource);
switch ($this->getExtension($resource)) {
case "jpg":
case "peg":
$this->resource = imagecreatefromjpeg($resource);
break;
case "png":
$this->resource = imagecreatefrompng($resource);
$compression = false;
break;
case "gif":
$this->resource = imagecreatefromgif($resource);
break;
default:
echo ERROR_0;
exit();
break;
}
}
if ($compression) {
$this->compress();
}
$this->width = imagesx($this->resource);
$this->height = imagesy($this->resource);
}
function setCharIndex($array) {
$this->charindex = $array;
$this->base = count($array);
}
function compress() {
imagetruecolortopalette($this->resource, true, 256);
}
function buildSections() {
// First, generate a matrix of colors...
$index = 0;
$colors = array();
for ($line = 0; $line < $this->height; $line++) {
for ($column = 0; $column < $this->width; $column++) {
$color = $this->getColorValue($line, $column);
if (!$colors[$color])
$colors[$color] = $index++;
$matrix[$line][$column] = $colors[$color];
}
}
$this->numColors = count($colors);
$this->pixsize = ceil(log($this->numColors, $this->base));
// Then, build the palette of colors...
$palette = array();
foreach ($colors as $value => $key) {
if ($value != "None") {
$palette[] = "\"" . $this->convertKey($key). "\t c #".$value . "\"";
} else {
$palette[] = "\"" . $this->convertKey($key). "\t c ".$value . "\"";
}
}
$this->palette = join(",\n", $palette);
// And then, build the XPM image
for ($line = 0; $line < $this->height; $line++) {
$linechars = "";
for ($column = 0; $column < $this->width; $column++) {
$linechars .= $this->convertKey($matrix[$line][$column]);
}
$this->imagematrix[] = "\"" . $linechars . "\"";
}
$this->image = join(",\n", $this->imagematrix);
// Header
$this->header = "/* XPM */
/**
*+---------------------------------------------------+
*| This image was generated by Pic2X v1.5 |
*| on ". date('r') ." |
*+---------------------------------------------------+
*| www.dnfeitosa.com |
*| hide@address.com |
*+---------------------------------------------------+
**/
static char * ".$this->filename."_xpm[]"." = {" . "
\"".$this->width." ".$this->height." ".$this->numColors." ".$this->pixsize."\"";
$this->sections["header"] = $this->header;
$this->sections["palette"] = $this->palette;
$this->sections["image"] = $this->image;
$this->isBuilt = true;
}
function convert() {
if (!$this->isBuilt) {
$this->buildSections();
}
$this->xpmImage = join(",\n", $this->sections) . "};";
return $this->xpmImage;
}
function convertKey($value) {
if ($this->cache[$value])
return $this->cache[$value];
$num = $value;
$conv = "";
$qtd = 0;
while ($num > 0) {
$conv = $this->charindex[$num % $this->base] . $conv;
$num = (int)($num / $this->base);
$qtd++;
}
while ($qtd++ < $this->pixsize)
$conv = $this->charindex[0] . $conv;
$this->cache[$value] = $conv;
return $conv;
}
function arrayKeySearch($array, $value) {
$key = array_keys($array, $value);
if ($key == null) {
return false;
} else {
return $key[0];
}
}
function getExtension($name) {
return substr($name, -3);
}
function getColorValue($line, $column) {
$pixel = imagecolorsforindex($this->resource, imagecolorat($this->resource, $column, $line));
if ($pixel['alpha'] > '50') {
return 'None';
} else {
return $this->rgbToHex($pixel);
}
}
function rgbToHex($array) {
$hexval = array();
foreach ($array as $key => $value) {
switch ($key) {
case "red":
case "green":
case "blue":
$hexval[] = str_pad(strtoupper(base_convert($value, 10, 16)), 2, 0, STR_PAD_LEFT);
break;
}
}
return join("", $hexval);
}
function free() {
imagedestroy($this->resource);
}
function save($filename) {
if (file_exists($filename."xpm"))
return false;
$ok = fwrite(fopen($filename, 'a'),$this->xpmImage);
return $ok;
}
}
?>