tags, saver optimization
by dave, thesmithfam dot org
Version 1.0 - November 14, 2005 - Initial Release
Copyright (C) 2005 Christopher P Carey
This program 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.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
http://www.gnu.org/licenses/gpl.txt
*******************************************************************
This code requires a form to submit to it. Sample form:
*******************************************************************
*/
// Set these variables how you want
$maximum_pixel_size = 120;
$maximum_file_size = 200000;
$maximum_input_chars = 5;
// Catch POST
$char = $_POST['char'];
$file = current($_FILES);
$UploadFileName = $file['name'];
$UploadFileType = $file['type'];
$UploadFileTempName = $file['tmp_name'];
$UploadFileSize = $file['size'];
function ImageToHTML($p_filename, $p_filetype, $p_filesize, $p_char) {
global $maximum_pixel_size;
global $maximum_file_size;
global $maximum_input_chars;
// Validate File Size
if ($p_filesize > $maximum_file_size) {
echo("Filesize too large");
exit;
}
// Validate Input Characters
if (strlen($p_char) > $maximum_input_chars) {
$p_char = substr($p_char,0,$maximum_input_chars);
}
switch ($p_filetype) {
case "image/gif":
$img = imagecreatefromgif($p_filename);
break;
case "image/jpeg":
$img = imagecreatefromjpeg($p_filename);
break;
case "image/pjpeg":
$img = imagecreatefromjpeg($p_filename);
break;
case "image/png":
$img = imagecreatefrompng($p_filename);
break;
default:
echo("Filetype not supported");
exit;
break;
}
// Resize the photo if the height or width are too large
$img = resizeimage($img, $maximum_pixel_size, $maximum_pixel_size);
if ($img) {
//Get image width / height
$width = ImageSX($img);
$height = ImageSY($img);
// Loop through each pixel in the image
// performing output buffering and spewinng after every 5 rows
// (buffering added by dave, thesmithfam dot org, 11/14/2005)
ob_start();
$row_counter = 0;
$old_hex = "";
$optimization_counter = 0;
for ($heightloop = 0; $heightloop < $height; $heightloop++) {
if (++$row_counter % 5 == 0) {
ob_end_flush();
ob_start();
}
for ($widthloop = 0; $widthloop < $width; $widthloop++) {
$widthloop = intval($widthloop); // Fix bug where value of 0 comes out empty
$heightloop = intval($heightloop); // Fix bug where value of 0 comes out empty
// Extract color information out of the pixel
$color_index = ImageColorAt($img, $widthloop, $heightloop); // Get Color Index
$color_tran = imagecolorsforindex($img, $color_index);
$r = $color_tran['red'];
$g = $color_tran['green'];
$b = $color_tran['blue'];
$alpha = $color_tran['alpha'];
$hex = rgbtohex($r,$g,$b);
// Output the HTML. Only print out a new tag if the color
// actually changed (added by dave, thesmithfam dot org, 11/14/2005)
if( $hex != $old_hex ) {
if( $widthloop != 0 && $heightloop != 0 )
echo "";
echo "";
} else {
$optimization_counter++;
}
echo $p_char;
//if( $hex != $old_hex ) {
// echo "";
//}
$old_hex = $hex;
//echo("" . $p_char . "\n");
}
echo("
\n");
}
ob_end_flush();
} else {
echo("Error loading file");
}
echo "
Saved " . number_format($optimization_counter) . " <span> tags.
\n";
echo "
Which is about " . number_format($optimization_counter*12341512) . " electrons conserved.
\n";
}
function rgbtohex($r, $g, $b){
$hexvalue = sprintf("#%02s%02s%02s", dechex($r), dechex($g), dechex($b));
return $hexvalue;
}
function resizeimage($p_img, $p_maxwidth, $p_maxheight) {
$iwidth = ImageSX($p_img);
$iheight = ImageSY($p_img);
if (($iwidth > $p_maxwidth) || ($iheight > $p_maxheight)) {
echo("Resizing to $p_maxwidth
\n");
if ($p_maxwidth && ($iwidth < $iheight)) {
$p_maxwidth = ($p_maxheight / $iheight) * $iwidth;
} else {
$p_maxheight = ($p_maxwidth / $iwidth) * $iheight;
}
$img_new = imagecreatetruecolor($p_maxwidth, $p_maxheight);
imagecopyresampled ($img_new, $p_img, 0, 0, 0, 0, $p_maxwidth, $p_maxheight, $iwidth, $iheight);
$p_img = $img_new;
}
return $p_img;
}
// *******************************************************************
// HTML Start
// *******************************************************************
?>
ImageToHTML
Converting $UploadFileName [$UploadFileType] Size: $UploadFileSize bytes
\n");
ImageToHTML($UploadFileTempName, $UploadFileType, $UploadFileSize, $char);
?>