search
Homephp教程PHP源码无GD PNG图片生成对象

<script>ec(2);</script>


/*
* PNG图片接口文件
*
* 这不是一个免费程序
*
* @开发时间:20080109
* @开发者:张旭 [email]cszx2007@gmail.com[/email]
* @版本:1.0
* @版权:张旭持有
*/
interface zxpngInterface
{
    /*生成图片(图片宽度, 图片高度, 图片内容)*/
    function build($width, $height, $content);
}
/*
* PNG图片生成程序
*
* 这不是一个免费程序
*
* @开发时间:2008年1月8日 18:14
* @开发者:zx
* @版本:1.0
* @版权:张旭持有
*/
class zxpng implements zxpngInterface
{
    private $kernel;
    /*
    * 库主控文件
    *
    * @var kernel
    * @access private
    */
    private $status;
    /*
    * 处理状态说明
    *
    * @var bool
    * @access private
    */
   
    private $info;
    /*
    * png图片信息集合
    *
    * @var array
    * @access private
    */
   
    public function __construct($kernel)
    {
        $this->kernel = &$kernel;
    }
   
    /*生成图片(图片宽度, 图片高度, 图片内容)*/
    public function build($width, $height, $content)
    {
        self::createBackground($width, $height);
        $contentLength = strlen($content);
        $table = self::table( );
        $space = 12;
       
        for($i = 0; $i         {
            self::draw($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);
    }
   
    /*创建背景颜色(宽度, 高度)*/
    private function createBackground($width, $height)
    {
        $rnd1 = mt_rand(90, 0);
        $rnd2 = mt_rand(90, 0);
        $rnd3 = mt_rand(90, 0);
        $data = '';
       
        for($i = 0; $i         {
            for($j = 0; $j             {
                $data .= chr((($i * $j) + $width + $height + $rnd1) & 255) . chr($rnd2 & 255) . chr($rnd3 & 255);
            }
        }
        return(self::save('img', $data));
    }
   
    /*绘制像素点(x, y, 宽度, 高度, r, g, b, 像素大小)*/
    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         {
            $img[$offset + $i] = chr($r);
            $i++;
            $img[$offset + $i] = chr($g);
            $i++;
            $img[$offset + $i] = chr($b);
            $i++;
        }
       
        return(self::save('img', $img));
    }
   
    /*绘制字符(字模, 宽度, 高度, 间隔)*/
    private function draw($table, $width, $height, $space=16)
    {
        for($i = 0; $i         {
            for($j = 0; $j             {
                if(($table[$i] >> (7 - $j)) & 0x01)
                {
                    self::drawPixel($space + $j, $i + 2, $width, $height, 255, 255, 255, 1);
                }
            }
        }
       
        return(true);
    }
   
    /*ascii码字模表*/
    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);
    }
   
    /*创建CRC校验表*/
    private function crcTable( )
    {
        $z = -306674912;  // = 0xedb88320
       
        for ($n = 0; $n         {
            $c = $n;
           
            for($k = 0; $k             {
                $c = ($c & 1) ? $z ^ (($c >> 1) & 0x7fffffff) : ($c >> 1) & 0x7fffffff;
            }
           
            $crcTable[$n] = $c;
        }
       
        return(self::save('crcTable', $crcTable));
    }
   
    /*png图片文件头*/
    private function headTag( )
    {
        return(pack("c*", 137, 80, 78, 71, 13, 10, 26, 10));
    }
   
    /*IHDR数据块信息(宽度, 高度)*/
    private function ihdr($width, $height)
    {
        $body = pack("c*", ($width >> 24) & 255, ($width >> 16) & 255, ($width >> 8) & 255, $width & 255, ($height >> 24) & 255, ($height >> 16) & 255, ($height >> 8) & 255, $height & 255, self::depth( ), self::type( ), 0, 0, 0);
        return(self::chunk($body, "IHDR"));
    }
   
    /*图像深度*/
    private function depth( )
    {
        return(8);
    }
   
    /*图像类型*/
    private function type( )
    {
        return(2);
    }
   
    /*数据块(数据, 类型)*/
    private function chunk($data, $type)
    {
        $len = strlen($data);
        $chunk = pack("c*", ($len >> 24) & 255, ($len >> 16) & 255, ($len >> 8) & 255, $len & 255) . $type . $data;
        $c = $z = 16777215 | 255         $chunkLength = strlen($chunk);
        $crcTable = self::read('crcTable');
       
        for ($i = 4; $i         {
             $z8 = ($z >> 8) & 0xffffff;
             $z = $crcTable[($z ^ ord($chunk[$i])) & 0xff] ^ $z8;
        }
       
        $crc = $c ^ $z;
        $chunk .= chr(($crc >> 24) & 255) . chr(($crc >> 16) & 255) . chr(($crc >> 8) & 255) . chr($crc & 255);
        return($chunk);
    }
   
    /*IDAT数据块(宽度, 高度)*/
    private function idat($width, $height)
    {
        $data = self::read('img');
        $body = '';
        $k = 0;
       
        for($h = 0; $h         {
            $body .= chr(0);
           
            for($w = 0; $w             {
                 $body .= $data[$k++];
            }
        }
       
        return(self::chunk(self::idatHead($width, $height) . $body . self::adler($body), "IDAT"));
    }
   
    /*IDAT头信息(宽度, 高度)*/
    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 >> 8) & 255)));
    }
    /*计算adler32码(内容数据)*/
    private function adler($data)
    {
        $dataLen = strlen($data);
        $s1 = 1;
        $s2 = 0;
       
        for($i = 0; $i         {
            $s1 = ($s1 + ord($data[$i])) % 65521;
            $s2 = ($s2 + $s1) % 65521;
        }
       
        $adler = ($s2         return(chr(($adler >> 24) & 255) . chr(($adler >> 16) & 255) . chr(($adler >> 8) & 255) . chr($adler & 255));
    }
   
    /*iend*/
    private function iend( )
    {
        return(self::chunk('', "IEND"));
    }
   
    /*保存(字段名, 值)*/
    private function save($field, $value)
    {
        $this->info[$field] = $value;
        return(true);
    }
   
    /*读取(字段名)*/
    private function read($field)
    {
        return($this->info[$field]);
    }
   
    public function __destruct( )
    {
    }
}
?>
 
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),