<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>代码收藏 &#187; 压缩</title> <atom:link href="http://code.9enjoy.com/tag/%e5%8e%8b%e7%bc%a9/feed/" rel="self" type="application/rss+xml" /><link>http://code.9enjoy.com</link> <description>收集网站开发时常用到的代码(asp,php,js等)，以函数为主</description> <lastBuildDate>Mon, 24 Jan 2011 14:23:49 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.9.1</generator> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>php 中图像压缩处理类（一）bmp.php</title><link>http://code.9enjoy.com/php/imagebmp/</link> <comments>http://code.9enjoy.com/php/imagebmp/#comments</comments> <pubDate>Fri, 06 Feb 2009 13:29:00 +0000</pubDate> <dc:creator>enjoy</dc:creator> <category><![CDATA[php]]></category> <category><![CDATA[bmp]]></category> <category><![CDATA[压缩]]></category> <category><![CDATA[图像]]></category><guid
isPermaLink="false">http://code.itlearner.com/?p=62</guid> <description><![CDATA[<?php<br/>/**<br/> * @(#)bmp.php<br/> * PHP version 5.2.3<br/> *<br/> *<br/> * Copyright 2007 SoftRoad. All Rights Reserved.<br/> *<br/> *====================================================================<br/> * 変更履歴<br/> * <br/> * 新規作成                            2007 年5月17日   蒋彪<br/> */<br/>function imagebmp($img, $file = "", $RLE = 0) {<br/>...
]]></description> <content:encoded><![CDATA[<p>/**<br
/> * @(#)bmp.php<br
/> * PHP version 5.2.3<br
/> *<br
/> *<br
/> * Copyright 2007 SoftRoad. All Rights Reserved.<br
/> *<br
/> *====================================================================<br
/> * 変更履歴<br
/> *<br
/> * 新規作成                            2007 年5月17日   蒋彪<br
/> */<br
/> function imagebmp($img, $file = &#8220;&#8221;, $RLE = 0) {</p><p>$ColorCount = imagecolorstotal($img);</p><p>$Transparent = imagecolortransparent($img);<br
/> $IsTransparent = $Transparent != -1;</p><p>if ($IsTransparent)<br
/> $ColorCount&#8211;;</p><p>if ($ColorCount == 0) {<br
/> $ColorCount = 0;<br
/> $BitCount = 24;<br
/> };<br
/> if (($ColorCount &gt; 0) and ($ColorCount &lt;= 2)) {<br
/> $ColorCount = 2;<br
/> $BitCount = 1;<br
/> };<br
/> if (($ColorCount &gt; 2) and ($ColorCount &lt;= 16)) {<br
/> $ColorCount = 16;<br
/> $BitCount = 4;<br
/> };<br
/> if (($ColorCount &gt; 16) and ($ColorCount &lt;= 256)) {<br
/> $ColorCount = 0;<br
/> $BitCount = 8;<br
/> };</p><p>$Width = imagesx($img);<br
/> $Height = imagesy($img);</p><p>$Zbytek = (4 &#8211; ($Width / (8 / $BitCount)) % 4) % 4;<br
/> $palsize = 0; // cid added<br
/> if ($BitCount &lt; 24)<br
/> $palsize = pow(2, $BitCount) * 4;</p><p>$size = (floor($Width / (8 / $BitCount)) + $Zbytek) * $Height +54;<br
/> $size += $palsize;<br
/> $offset = 54 + $palsize;</p><p>// Bitmap File Header<br
/> $ret = &#8216;BM&#8217;; // header (2b)<br
/> $ret .= int_to_dword($size); // size of file (4b)<br
/> $ret .= int_to_dword(0); // reserved (4b)<br
/> $ret .= int_to_dword($offset); // byte location in the file which is first byte of IMAGE (4b)<br
/> // Bitmap Info Header<br
/> $ret .= int_to_dword(40); // Size of BITMAPINFOHEADER (4b)<br
/> $ret .= int_to_dword($Width); // width of bitmap (4b)<br
/> $ret .= int_to_dword($Height); // height of bitmap (4b)<br
/> $ret .= int_to_word(1); // biPlanes = 1 (2b)<br
/> $ret .= int_to_word($BitCount); // biBitCount = {1 (mono) or 4 (16 clr ) or 8 (256 clr) or 24 (16 Mil)} (2b)<br
/> $ret .= int_to_dword($RLE); // RLE COMPRESSION (4b)<br
/> $ret .= int_to_dword(0); // width x height (4b)<br
/> $ret .= int_to_dword(0); // biXPelsPerMeter (4b)<br
/> $ret .= int_to_dword(0); // biYPelsPerMeter (4b)<br
/> $ret .= int_to_dword(0); // Number of palettes used (4b)<br
/> $ret .= int_to_dword(0); // Number of important colour (4b)<br
/> // image data</p><p>$CC = $ColorCount;<br
/> $sl1 = strlen($ret);<br
/> if ($CC == 0)<br
/> $CC = 256;<br
/> if ($BitCount &lt; 24) {<br
/> $ColorTotal = imagecolorstotal($img);<br
/> if ($IsTransparent)<br
/> $ColorTotal&#8211;;</p><p>for ($p = 0; $p &lt; $ColorTotal; $p++) {<br
/> $color = imagecolorsforindex($img, $p);<br
/> $ret .= inttobyte($color["blue"]);<br
/> $ret .= inttobyte($color["green"]);<br
/> $ret .= inttobyte($color["red"]);<br
/> $ret .= inttobyte(0); //RESERVED<br
/> };</p><p>$CT = $ColorTotal;<br
/> for ($p = $ColorTotal; $p &lt; $CC; $p++) {<br
/> $ret .= inttobyte(0);<br
/> $ret .= inttobyte(0);<br
/> $ret .= inttobyte(0);<br
/> $ret .= inttobyte(0); //RESERVED<br
/> };<br
/> };</p><p>$retd = &#8221;; // cid added<br
/> if ($BitCount &lt;= 8) {</p><p>for ($y = $Height -1; $y &gt;= 0; $y&#8211;) {<br
/> $bWrite = &#8220;&#8221;;<br
/> for ($x = 0; $x &lt; $Width; $x++) {<br
/> $color = imagecolorat($img, $x, $y);<br
/> $bWrite .= decbinx($color, $BitCount);<br
/> if (strlen($bWrite) == 8) {<br
/> $retd .= inttobyte(bindec($bWrite));<br
/> $bWrite = &#8220;&#8221;;<br
/> };<br
/> };</p><p>if ((strlen($bWrite) &lt; 8) and (strlen($bWrite) != 0)) {<br
/> $sl = strlen($bWrite);<br
/> for ($t = 0; $t &lt; 8 &#8211; $sl; $t++)<br
/> $sl .= &#8220;0&#8243;;<br
/> $retd .= inttobyte(bindec($bWrite));<br
/> };<br
/> for ($z = 0; $z &lt; $Zbytek; $z++)<br
/> $retd .= inttobyte(0);<br
/> };<br
/> };</p><p>if (($RLE == 1) and ($BitCount == 8)) {<br
/> for ($t = 0; $t &lt; strlen($retd); $t += 4) {<br
/> if ($t != 0)<br
/> if (($t) % $Width == 0)<br
/> $ret .= chr(0) .<br
/> chr(0);</p><p>if (($t +5) % $Width == 0) {<br
/> $ret .= chr(0) . chr(5) . substr($retd, $t, 5) . chr(0);<br
/> $t += 1;<br
/> }<br
/> if (($t +6) % $Width == 0) {<br
/> $ret .= chr(0) . chr(6) . substr($retd, $t, 6);<br
/> $t += 2;<br
/> } else {<br
/> $ret .= chr(0) . chr(4) . substr($retd, $t, 4);<br
/> };<br
/> };<br
/> $ret .= chr(0) . chr(1);<br
/> } else {<br
/> $ret .= $retd;<br
/> };</p><p>if ($BitCount == 24) {<br
/> $Dopl = &#8221;; // cid added<br
/> for ($z = 0; $z &lt; $Zbytek; $z++)<br
/> $Dopl .= chr(0);</p><p>for ($y = $Height -1; $y &gt;= 0; $y&#8211;) {<br
/> for ($x = 0; $x &lt; $Width; $x++) {<br
/> $color = imagecolorsforindex($img, ImageColorAt($img, $x, $y));<br
/> $ret .= chr($color["blue"]) . chr($color["green"]) . chr($color["red"]);<br
/> }<br
/> $ret .= $Dopl;<br
/> };</p><p>};</p><p>if ($file != &#8220;&#8221;) {<br
/> $r = ($f = fopen($file, &#8220;w&#8221;));<br
/> $r = $r and fwrite($f, $ret);<br
/> $r = $r and fclose($f);<br
/> return $r;<br
/> } else {<br
/> echo $ret;<br
/> };<br
/> };</p><p>/*<br
/> *&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br
/> *                    ImageCreateFromBmp<br
/> *&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br
/> *            &#8211; Reads image from a BMP file<br
/> *<br
/> *         Parameters:  $file &#8211; Target file to load<br
/> *<br
/> *            Returns: Image ID<br
/> */</p><p>function imagecreatefrombmp($file) {<br
/> global $CurrentBit, $echoMode;</p><p>$f = fopen($file, &#8220;r&#8221;);<br
/> $Header = fread($f, 2);</p><p>if ($Header == &#8220;BM&#8221;) {<br
/> $Size = freaddword($f);<br
/> $Reserved1 = freadword($f);<br
/> $Reserved2 = freadword($f);<br
/> $FirstByteOfImage = freaddword($f);</p><p>$SizeBITMAPINFOHEADER = freaddword($f);<br
/> $Width = freaddword($f);<br
/> $Height = freaddword($f);<br
/> $biPlanes = freadword($f);<br
/> $biBitCount = freadword($f);<br
/> $RLECompression = freaddword($f);<br
/> $WidthxHeight = freaddword($f);<br
/> $biXPelsPerMeter = freaddword($f);<br
/> $biYPelsPerMeter = freaddword($f);<br
/> $NumberOfPalettesUsed = freaddword($f);<br
/> $NumberOfImportantColors = freaddword($f);</p><p>if ($biBitCount &lt; 24) {<br
/> $img = imagecreate($Width, $Height);<br
/> $Colors = pow(2, $biBitCount);<br
/> for ($p = 0; $p &lt; $Colors; $p++) {<br
/> $B = freadbyte($f);<br
/> $G = freadbyte($f);<br
/> $R = freadbyte($f);<br
/> $Reserved = freadbyte($f);<br
/> $Palette[] = imagecolorallocate($img, $R, $G, $B);<br
/> };</p><p>if ($RLECompression == 0) {<br
/> $Zbytek = (4 &#8211; ceil(($Width / (8 / $biBitCount))) % 4) % 4;</p><p>for ($y = $Height -1; $y &gt;= 0; $y&#8211;) {<br
/> $CurrentBit = 0;<br
/> for ($x = 0; $x &lt; $Width; $x++) {<br
/> $C = freadbits($f, $biBitCount);<br
/> imagesetpixel($img, $x, $y, $Palette[$C]);<br
/> };<br
/> if ($CurrentBit != 0) {<br
/> freadbyte($f);<br
/> };<br
/> for ($g = 0; $g &lt; $Zbytek; $g++)<br
/> freadbyte($f);<br
/> };</p><p>};<br
/> };</p><p>if ($RLECompression == 1) //$BI_RLE8<br
/> {<br
/> $y = $Height;</p><p>$pocetb = 0;</p><p>while (true) {<br
/> $y&#8211;;<br
/> $prefix = freadbyte($f);<br
/> $suffix = freadbyte($f);<br
/> $pocetb += 2;</p><p>$echoit = false;</p><p>if ($echoit)<br
/> echo &#8220;Prefix: $prefix Suffix: $suffix<br
/> &#8220;;<br
/> if (($prefix == 0) and ($suffix == 1))<br
/> break;<br
/> if (feof($f))<br
/> break;</p><p>while (!(($prefix == 0) and ($suffix == 0))) {<br
/> if ($prefix == 0) {<br
/> $pocet = $suffix;<br
/> $Data .= fread($f, $pocet);<br
/> $pocetb += $pocet;<br
/> if ($pocetb % 2 == 1) {<br
/> freadbyte($f);<br
/> $pocetb++;<br
/> };<br
/> };<br
/> if ($prefix &gt; 0) {<br
/> $pocet = $prefix;<br
/> for ($r = 0; $r &lt; $pocet; $r++)<br
/> $Data .= chr($suffix);<br
/> };<br
/> $prefix = freadbyte($f);<br
/> $suffix = freadbyte($f);<br
/> $pocetb += 2;<br
/> if ($echoit)<br
/> echo &#8220;Prefix: $prefix Suffix: $suffix<br
/> &#8220;;<br
/> };</p><p>for ($x = 0; $x &lt; strlen($Data); $x++) {<br
/> imagesetpixel($img, $x, $y, $Palette[ord($Data[$x])]);<br
/> };<br
/> $Data = &#8220;&#8221;;</p><p>};</p><p>};</p><p>if ($RLECompression == 2) //$BI_RLE4</p><p>{<br
/> $y = $Height;<br
/> $pocetb = 0;</p><p>/*while(!feof($f))<br
/> echo freadbyte($f).&#8221;_&#8221;.freadbyte($f).&#8221;<br
/> &#8220;;*/<br
/> while (true) {<br
/> //break;<br
/> $y&#8211;;<br
/> $prefix = freadbyte($f);<br
/> $suffix = freadbyte($f);<br
/> $pocetb += 2;</p><p>$echoit = false;</p><p>if ($echoit)<br
/> echo &#8220;Prefix: $prefix Suffix: $suffix<br
/> &#8220;;<br
/> if (($prefix == 0) and ($suffix == 1))<br
/> break;<br
/> if (feof($f))<br
/> break;</p><p>while (!(($prefix == 0) and ($suffix == 0))) {<br
/> if ($prefix == 0) {<br
/> $pocet = $suffix;</p><p>$CurrentBit = 0;<br
/> for ($h = 0; $h &lt; $pocet; $h++)<br
/> $Data .= chr(freadbits($f, 4));<br
/> if ($CurrentBit != 0)<br
/> freadbits($f, 4);<br
/> $pocetb += ceil(($pocet / 2));<br
/> if ($pocetb % 2 == 1) {<br
/> freadbyte($f);<br
/> $pocetb++;<br
/> };<br
/> };<br
/> if ($prefix &gt; 0) {<br
/> $pocet = $prefix;<br
/> $i = 0;<br
/> for ($r = 0; $r &lt; $pocet; $r++) {<br
/> if ($i % 2 == 0) {<br
/> $Data .= chr($suffix % 16);<br
/> } else {<br
/> $Data .= chr(floor($suffix / 16));<br
/> };<br
/> $i++;<br
/> };<br
/> };<br
/> $prefix = freadbyte($f);<br
/> $suffix = freadbyte($f);<br
/> $pocetb += 2;<br
/> if ($echoit)<br
/> echo &#8220;Prefix: $prefix Suffix: $suffix<br
/> &#8220;;<br
/> };</p><p>for ($x = 0; $x &lt; strlen($Data); $x++) {<br
/> imagesetpixel($img, $x, $y, $Palette[ord($Data[$x])]);<br
/> };<br
/> $Data = &#8220;&#8221;;</p><p>};</p><p>};</p><p>if ($biBitCount == 24) {<br
/> $img = imagecreatetruecolor($Width, $Height);<br
/> $Zbytek = $Width % 4;</p><p>for ($y = $Height -1; $y &gt;= 0; $y&#8211;) {<br
/> for ($x = 0; $x &lt; $Width; $x++) {<br
/> $B = freadbyte($f);<br
/> $G = freadbyte($f);<br
/> $R = freadbyte($f);<br
/> $color = imagecolorexact($img, $R, $G, $B);<br
/> if ($color == -1)<br
/> $color = imagecolorallocate($img, $R, $G, $B);<br
/> imagesetpixel($img, $x, $y, $color);<br
/> }<br
/> for ($z = 0; $z &lt; $Zbytek; $z++)<br
/> freadbyte($f);<br
/> };<br
/> };<br
/> return $img;</p><p>};</p><p>fclose($f);</p><p>};</p><p>/*<br
/> * Helping functions:<br
/> *&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br
/> *<br
/> * freadbyte($file) &#8211; reads 1 byte from $file<br
/> * freadword($file) &#8211; reads 2 bytes (1 word) from $file<br
/> * freaddword($file) &#8211; reads 4 bytes (1 dword) from $file<br
/> * freadlngint($file) &#8211; same as freaddword($file)<br
/> * decbin8($d) &#8211; returns binary string of d zero filled to 8<br
/> * RetBits($byte,$start,$len) &#8211; returns bits $start-&gt;$start+$len from $byte<br
/> * freadbits($file,$count) &#8211; reads next $count bits from $file<br
/> * RGBToHex($R,$G,$B) &#8211; convert $R, $G, $B to hex<br
/> * int_to_dword($n) &#8211; returns 4 byte representation of $n<br
/> * int_to_word($n) &#8211; returns 2 byte representation of $n<br
/> */</p><p>function freadbyte($f) {<br
/> return ord(fread($f, 1));<br
/> };</p><p>function freadword($f) {<br
/> $b1 = freadbyte($f);<br
/> $b2 = freadbyte($f);<br
/> return $b2 * 256 + $b1;<br
/> };</p><p>function freadlngint($f) {<br
/> return freaddword($f);<br
/> };</p><p>function freaddword($f) {<br
/> $b1 = freadword($f);<br
/> $b2 = freadword($f);<br
/> return $b2 * 65536 + $b1;<br
/> };</p><p>function RetBits($byte, $start, $len) {<br
/> $bin = decbin8($byte);<br
/> $r = bindec(substr($bin, $start, $len));<br
/> return $r;</p><p>};</p><p>$CurrentBit = 0;<br
/> function freadbits($f, $count) {<br
/> global $CurrentBit, $SMode;<br
/> $Byte = freadbyte($f);<br
/> $LastCBit = $CurrentBit;<br
/> $CurrentBit += $count;<br
/> if ($CurrentBit == 8) {<br
/> $CurrentBit = 0;<br
/> } else {<br
/> fseek($f, ftell($f) &#8211; 1);<br
/> };<br
/> return RetBits($Byte, $LastCBit, $count);<br
/> };</p><p>function RGBToHex($Red, $Green, $Blue) {<br
/> $hRed = dechex($Red);<br
/> if (strlen($hRed) == 1)<br
/> $hRed = &#8220;0$hRed&#8221;;<br
/> $hGreen = dechex($Green);<br
/> if (strlen($hGreen) == 1)<br
/> $hGreen = &#8220;0$hGreen&#8221;;<br
/> $hBlue = dechex($Blue);<br
/> if (strlen($hBlue) == 1)<br
/> $hBlue = &#8220;0$hBlue&#8221;;<br
/> return ($hRed . $hGreen . $hBlue);<br
/> };</p><p>function int_to_dword($n) {<br
/> return chr($n &amp; 255) . chr(($n &gt;&gt; 8) &amp; 255) . chr(($n &gt;&gt; 16) &amp; 255) . chr(($n &gt;&gt; 24) &amp; 255);<br
/> }<br
/> function int_to_word($n) {<br
/> return chr($n &amp; 255) . chr(($n &gt;&gt; 8) &amp; 255);<br
/> }</p><p>function decbin8($d) {<br
/> return decbinx($d, 8);<br
/> };</p><p>function decbinx($d, $n) {<br
/> $bin = decbin($d);<br
/> $sbin = strlen($bin);<br
/> for ($j = 0; $j &lt; $n &#8211; $sbin; $j++)<br
/> $bin = &#8220;0$bin&#8221;;<br
/> return $bin;<br
/> };</p><p>function inttobyte($n) {<br
/> return chr($n);<br
/> };<br
/> ?&gt;</p> <img
src="http://code.9enjoy.com/?ak_action=api_record_view&id=62&type=feed" alt="" />]]></content:encoded> <wfw:commentRss>http://code.9enjoy.com/php/imagebmp/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk
Database Caching 10/14 queries in 0.004 seconds using disk

Served from: code.itlearner.com @ 2012-02-07 02:52:33 -->
