No GD PNG picture generation object

Source: Internet
Author: User
Tags 0xc0 chr ord pack






<?php


/*


* PNG Image interface file


*


* This is not a free program


*


* @ Development time: 20080109


* @ Developer: Zhangxuyue [Email]cszx2007@gmail.com[/email]


* @ Version: 1.0


* @ Copyright: Zhangxuyue hold


*/


Interface Zxpnginterface


{


/* Generate pictures (picture width, picture height, picture content) * *


function Build ($width, $height, $content);


}


/*


* PNG image Generation Program


*


* This is not a free program


*


* @ Development time: January 8, 2008 18:14


* @ Developer: ZX


* @ Version: 1.0


* @ Copyright: Zhangxuyue hold


*/


Class Zxpng implements Zxpnginterface


{


Private $kernel;


/*


* Library Master File


*


* @var Kernel


* @access Private


*/


Private $status;


/*


* Processing Status Description


*


* @var BOOL


* @access Private


*/





Private $info;


/*


* PNG image Information collection


*


* @var Array


* @access Private


*/





Public function __construct ($kernel)


{


$this->kernel = & $kernel;


}





/* Generate pictures (picture width, picture height, picture content) * *


Public function Build ($width, $height, $content)


{


Self::createbackground ($width, $height);


$contentLength = strlen ($content);


$table = Self::table ();


$space = 12;





for ($i = 0; $i < $contentLength; $i + +)


{


Self::d Raw ($table [$content [$i]], $width, $height, $space);


$space + 12;


}





Self::crctable ();


$data = Self::headtag ();


$data. = Self::ihdr ($width, $height);


$data. = Self::idat ($width, $height);


$data. = Self::iend ();


return ($DATA);


}





/* Create background color (width, height) * *


Private Function Createbackground ($width, $height)


{


$rnd 1 = Mt_rand (90, 0);


$rnd 2 = Mt_rand (90, 0);


$rnd 3 = Mt_rand (90, 0);


$data = ';





for ($i = 0; $i < $height; $i + +)


{


for ($j = 0; $j < $width; $j + +)


{


$data. = Chr (($i * $j) + $width + $height + $rnd 1) & 255). Chr ($rnd 2 & 255). Chr ($rnd 3 & 255);


}


}


Return (Self::save (' img ', $data));


}





/* Draw pixel points (x, y, width, height, r, G, B, pixel size) * *


Private Function Drawpixel ($x, $y, $width, $height, $r =0, $g =0, $b =0, $size =1)


{


$img = Self::read (' img ');


$offset = 3 * ($width * $y + $x);





for ($i = 0; $i < $size;)


{


$img [$offset + $i] = Chr ($r);


$i + +;


$img [$offset + $i] = Chr ($g);


$i + +;


$img [$offset + $i] = Chr ($b);


$i + +;


}





Return (Self::save (' img ', $img));


}





/* Draw characters (font, width, height, interval) * *


Private function Draw ($table, $width, $height, $space =16)


{


for ($i = 0; $i < $i + +)


{


for ($j = 0; $j < 8; $j + +)


{


if ($table [$i] >> (7-$j)) & 0x01)


{


Self::d rawpixel ($space + $j, $i + 2, $width, $height, 255, 255, 255, 1);


}


}


}





return (true);


}





/*ascii Code Font Form * *


Private Function table ()


{


$ascii = array


(


' 0 ' => array (0x00,0x00,0x38,0x6c,0xc6,0xc6,0xd6,0xd6,0xc6,0xc6,0x6c,0x38,0x00,0x00,0x00,0x00),


' 1 ' => array (0x00,0x00,0x18,0x38,0x78,0x18,0x18,0x18,0x18,0x18,0x18,0x7e,0x00,0x00,0x00,0x00),


' 2 ' => Array (0x00,0x00,0x7c,0xc6,0x06,0x0c,0x18,0x30,0x60,0xc0,0xc6,0xfe,0x00,0x00,0x00,0x00),


' 3 ' => Array (0x00,0x00,0x7c,0xc6,0x06,0x06,0x3c,0x06,0x06,0x06,0xc6,0x7c,0x00,0x00,0x00,0x00),


' 4 ' => Array (0x00,0x00,0x0c,0x1c,0x3c,0x6c,0xcc,0xfe,0x0c,0x0c,0x0c,0x1e,0x00,0x00,0x00,0x00),


' 5 ' => Array (0x00,0x00,0xfe,0xc0,0xc0,0xc0,0xfc,0x06,0x06,0x06,0xc6,0x7c,0x00,0x00,0x00,0x00),


' 6 ' => Array (0x00,0x00,0x38,0x60,0xc0,0xc0,0xfc,0xc6,0xc6,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00),


' 7 ' => Array (0x00,0x00,0xfe,0xc6,0x06,0x06,0x0c,0x18,0x30,0x30,0x30,0x30,0x00,0x00,0x00,0x00),


' 8 ' => Array (0x00,0x00,0x7c,0xc6,0xc6,0xc6,0x7c,0xc6,0xc6,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00),


' 9 ' => Array (0x00,0x00,0x7c,0xc6,0xc6,0xc6,0x7e,0x06,0x06,0x06,0x0c,0x78,0x00,0x00,0x00,0x00),


' A ' => Array (0x00,0x00,0x10,0x38,0x6c,0xc6,0xc6,0xfe,0xc6,0xc6,0xc6,0xc6,0x00,0x00,0x00,0x00),


' B ' => Array (0x00,0x00,0xfc,0x66,0x66,0x66,0x7c,0x66,0x66,0x66,0x66,0xfc,0x00,0x00,0x00,0x00),


' C ' => Array (0x00,0x00,0x3c,0x66,0xc2,0xc0,0xc0,0xc0,0xc0,0xc2,0x66,0x3c,0x00,0x00,0x00,0x00),


' D ' => Array (0x00,0x00,0xf8,0x6c,0x66,0x66,0x66,0x66,0x66,0x66,0x6c,0xf8,0x00,0x00,0x00,0x00),


' E ' => Array (0x00,0x00,0xfe,0x66,0x62,0x68,0x78,0x68,0x60,0x62,0x66,0xfe,0x00,0x00,0x00,0x00),


' F ' => Array (0x00,0x00,0xfe,0x66,0x62,0x68,0x78,0x68,0x60,0x60,0x60,0xf0,0x00,0x00,0x00,0x00),


' G ' => Array (0x00,0x00,0x3c,0x66,0xc2,0xc0,0xc0,0xde,0xc6,0xc6,0x66,0x3a,0x00,0x00,0x00,0x00),


' H ' => Array (0x00,0x00,0xc6,0xc6,0xc6,0xc6,0xfe,0xc6,0xc6,0xc6,0xc6,0xc6,0x00,0x00,0x00,0x00),


' I ' => Array (0x00,0x00,0x3c,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x3c,0x00,0x00,0x00,0x00),


' J ' => Array (0x00,0x00,0x1e,0x0c,0x0c,0x0c,0x0c,0x0c,0xcc,0xcc,0xcc,0x78,0x00,0x00,0x00,0x00),


' K ' => Array (0x00,0x00,0xe6,0x66,0x66,0x6c,0x78,0x78,0x6c,0x66,0x66,0xe6,0x00,0x00,0x00,0x00),


' L ' => Array (0x00,0x00,0xf0,0x60,0x60,0x60,0x60,0x60,0x60,0x62,0x66,0xfe,0x00,0x00,0x00,0x00),


' M ' => Array (0x00,0x00,0xc6,0xee,0xfe,0xfe,0xd6,0xc6,0xc6,0xc6,0xc6,0xc6,0x00,0x00,0x00,0x00),


' N ' => Array (0x00,0x00,0xc6,0xe6,0xf6,0xfe,0xde,0xce,0xc6,0xc6,0xc6,0xc6,0x00,0x00,0x00,0x00),


' O ' => Array (0x00,0x00,0x7c,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00),


' P ' => Array (0x00,0x00,0xfc,0x66,0x66,0x66,0x7c,0x60,0x60,0x60,0x60,0xf0,0x00,0x00,0x00,0x00),


' Q ' => Array (0x00,0x00,0x7c,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0xd6,0xde,0x7c,0x0c,0x0e,0x00,0x00),


' R ' => Array (0x00,0x00,0xfc,0x66,0x66,0x66,0x7c,0x6c,0x66,0x66,0x66,0xe6,0x00,0x00,0x00,0x00),


' S ' => Array (0x00,0x00,0x7c,0xc6,0xc6,0x60,0x38,0x0c,0x06,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00),


' T ' => Array (0x00,0x00,0x7e,0x7e,0x5a,0x18,0x18,0x18,0x18,0x18,0x18,0x3c,0x00,0x00,0x00,0x00),


' U ' => Array (0x00,0x00,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00),


' V ' => Array (0x00,0x00,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0x6c,0x38,0x10,0x00,0x00,0x00,0x00),


' W ' => Array (0x00,0x00,0xc6,0xc6,0xc6,0xc6,0xd6,0xd6,0xd6,0xfe,0xee,0x6c,0x00,0x00,0x00,0x00),


' X ' => Array (0x00,0x00,0xc6,0xc6,0x6c,0x7c,0x38,0x38,0x7c,0x6c,0xc6,0xc6,0x00,0x00,0x00,0x00),


' Y ' => Array (0x00,0x00,0x66,0x66,0x66,0x66,0x3c,0x18,0x18,0x18,0x18,0x3c,0x00,0x00,0x00,0x00),


' Z ' => Array (0x00,0x00,0xfe,0xc6,0x86,0x0c,0x18,0x30,0x60,0xc2,0xc6,0xfe,0x00,0x00,0x00,0x00),


' A ' => array (0x00,0x00,0x00,0x00,0x00,0x78,0x0c,0x7c,0xcc,0xcc,0xcc,0x76,0x00,0x00,0x00,0x00),


' B ' => Array (0x00,0x00,0xe0,0x60,0x60,0x78,0x6c,0x66,0x66,0x66,0x66,0x7c,0x00,0x00,0x00,0x00),


' C ' => Array (0x00,0x00,0x00,0x00,0x00,0x7c,0xc6,0xc0,0xc0,0xc0,0xc6,0x7c,0x00,0x00,0x00,0x00),


' d ' => Array (0x00,0x00,0x1c,0x0c,0x0c,0x3c,0x6c,0xcc,0xcc,0xcc,0xcc,0x76,0x00,0x00,0x00,0x00),


' E ' => Array (0x00,0x00,0x00,0x00,0x00,0x7c,0xc6,0xfe,0xc0,0xc0,0xc6,0x7c,0x00,0x00,0x00,0x00),


' F ' => Array (0x00,0x00,0x38,0x6c,0x64,0x60,0xf0,0x60,0x60,0x60,0x60,0xf0,0x00,0x00,0x00,0x00),


' G ' => Array (0x00,0x00,0x00,0x00,0x00,0x76,0xcc,0xcc,0xcc,0xcc,0xcc,0x7c,0x0c,0xcc,0x78,0x00),


' H ' => Array (0x00,0x00,0xe0,0x60,0x60,0x6c,0x76,0x66,0x66,0x66,0x66,0xe6,0x00,0x00,0x00,0x00),


' I ' => Array (0x00,0x00,0x18,0x18,0x00,0x38,0x18,0x18,0x18,0x18,0x18,0x3c,0x00,0x00,0x00,0x00),


' J ' => Array (0x00,0x00,0x06,0x06,0x00,0x0e,0x06,0x06,0x06,0x06,0x06,0x06,0x66,0x66,0x3c,0x00),


' K ' => Array (0x00,0x00,0xe0,0x60,0x60,0x66,0x6c,0x78,0x78,0x6c,0x66,0xe6,0x00,0x00,0x00,0x00),


' L ' => Array (0x00,0x00,0x38,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x3c,0x00,0x00,0x00,0x00),


' m ' => Array (0x00,0x00,0x00,0x00,0x00,0xec,0xfe,0xd6,0xd6,0xd6,0xd6,0xc6,0x00,0x00,0x00,0x00),


' N ' => Array (0x00,0x00,0x00,0x00,0x00,0xdc,0x66,0x66,0x66,0x66,0x66,0x66,0x00,0x00,0x00,0x00),


' O ' => Array (0x00,0x00,0x00,0x00,0x00,0x7c,0xc6,0xc6,0xc6,0xc6,0xc6,0x7c,0x00,0x00,0x00,0x00),


' P ' => Array (0x00,0x00,0x00,0x00,0x00,0xdc,0x66,0x66,0x66,0x66,0x66,0x7c,0x60,0x60,0xf0,0x00),


' Q ' => array (0x00,0x00,0x00,0x00,0x00,0x76,0xcc,0xcc,0xcc,0xcc,0xcc,0x7c,0x0c,0x0c,0x1e,0x00),


' R ' => Array (0x00,0x00,0x00,0x00,0x00,0xdc,0x76,0x66,0x60,0x60,0x60,0xf0,0x00,0x00,0x00,0x00),


' s ' => Array (0x00,0x00,0x00,0x00,0x00,0x7c,0xc6,0x60,0x38,0x0c,0xc6,0x7c,0x00,0x00,0x00,0x00),


' t ' => Array (0x00,0x00,0x10,0x30,0x30,0xfc,0x30,0x30,0x30,0x30,0x36,0x1c,0x00,0x00,0x00,0x00),


' U ' => Array (0x00,0x00,0x00,0x00,0x00,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0x76,0x00,0x00,0x00,0x00),


' V ' => Array (0x00,0x00,0x00,0x00,0x00,0x66,0x66,0x66,0x66,0x66,0x3c,0x18,0x00,0x00,0x00,0x00),


' W ' => Array (0x00,0x00,0x00,0x00,0x00,0xc6,0xc6,0xd6,0xd6,0xd6,0xfe,0x6c,0x00,0x00,0x00,0x00),


' X ' => array (0x00,0x00,0x00,0x00,0x00,0xc6,0x6c,0x38,0x38,0x38,0x6c,0xc6,0x00,0x00,0x00,0x00),


' Y ' => Array (0x00,0x00,0x00,0x00,0x00,0xc6,0xc6,0xc6,0xc6,0xc6,0xc6,0x7e,0x06,0x0c,0xf8,0x00),


' Z ' => Array (0x00,0x00,0x00,0x00,0x00,0xfe,0xcc,0x18,0x30,0x60,0xc6,0xfe,0x00,0x00,0x00,0x00)


);





return ($ASCII);


}





/* Create a CRC check form * *


Private Function crctable ()


{


$z =-306674912; = 0xedb88320





for ($n = 0; $n < 256; $n + +)


{


$c = $n;





for ($k = 0; $k < 8; $k + +)


{


$c = ($c & 1)? $z ^ (($c >> 1) & 0x7fffffff): ($c >> 1) & 0x7fffffff;


}





$crcTable [$n] = $c;


}





Return (Self::save (' crctable ', $crcTable));


}





/*png picture File Head * *


Private Function Headtag ()


{


Return (Pack ("c*", 137, 80, 78, 71, 13, 10, 26, 10));


}





/*IHDR Data block information (width, height) * *


Private Function Ihdr ($width, $height)


{


$body = Pack ("c*", ($width >>) & 255, ($width >>) & 255, ($width >> 8) & 255, $width & Amp 255, ($height >>) & 255, ($height >>) & 255, ($height >> 8) & 255, $height & 255, Self::d epth (), Self::type (), 0, 0, 0);


Return (Self::chunk ($body, "IHDR"));


}





/* Image Depth * *


Private Function Depth ()


{


return (8);


}





/* Image Type * *


Private function type ()


{


return (2);


}





/* Data block (data, type) * *


Private function Chunk ($data, $type)


{


$len = strlen ($data);


$chunk = Pack ("c*", ($len >>) & 255, ($len >>) & 255, ($len >> 8) & 255, $len & 25 5). $type. $data;


$c = $z = 16777215 | 255 << 24;


$chunkLength = strlen ($chunk);


$crcTable = Self::read (' crctable ');





for ($i = 4; $i < $chunkLength; $i + +)


{


$z 8 = ($z >> 8) & 0xFFFFFF;


$z = $crcTable [($z ^ ord ($chunk [$i])) & 0xFF] ^ $z 8;


}





$CRC = $c ^ $z;


$chunk. = Chr ($CRC >>) & 255). Chr ($CRC >>) & 255). Chr (($CRC >> 8) & 255). Chr ($CRC & 255);


return ($chunk);


}





/*idat data block (width, height) * *


Private Function Idat ($width, $height)


{


$data = Self::read (' img ');


$body = ';


$k = 0;





for ($h = 0; $h < $height; $h + +)


{


$body. = chr (0);





for ($w = 0; $w < $width * 3; $w + +)


{


$body. = $data [$k + +];


}


}





Return (Self::chunk self::idathead ($width, $height). $body. Self::adler ($body), "Idat");


}





/*idat header information (width, height) * *


Private Function Idathead ($width, $height)


{


$len = ($width * 3 + 1) * $height;


Return (Pack ("c*", 0x78, 0x01, 1, $len & 255, ($len >> 8) & 255, 255-($len & 255), 255-(($len >&gt ; 8) & 255));


}


/* Calculate ADLER32 Code (content data) * *


Private Function Adler ($data)


{


$dataLen = strlen ($data);


$s 1 = 1;


$s 2 = 0;





for ($i = 0; $i < $dataLen; $i + +)


{


$s 1 = ($s 1 + ord ($data [$i]))% 65521;


$s 2 = ($s 2 + $s 1)% 65521;


}





$adler = ($s 2 << 16) | $s 1;


Return (Chr ($adler >>) & 255) Chr (($adler >>) & 255) Chr (($adler >> 8) & 255). Ch R ($adler & 255));


}





/*iend*/


Private Function Iend ()


{


Return (Self::chunk (' "," iend "));


}





/* Save (field name, value) * *


Private function Save ($field, $value)


{


$this->info[$field] = $value;


return (true);


}





/* Read (field name) * *


Private function Read ($field)


{


Return ($this->info[$field]);


}





Public Function __destruct ()


{


}


}


?>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.