source: trunk/expressoMail1_2/inc/gd_functions.php @ 1005

Revision 1005, 5.6 KB checked in by amuller, 15 years ago (diff)

Ticket #520 - Implementação de redução automática de imagens

Line 
1<?/*
2*Author:
3*    JPEXS  from http://www.hotscripts.com* ImageCreateFromBmp
4*/
5
6function imagecreatefrombmp($file)
7{
8global  $CurrentBit, $echoMode;
9
10$f=fopen($file,"r");
11$Header=fread($f,2);
12
13if($Header=="BM")
14{
15 $Size=freaddword($f);
16 $Reserved1=freadword($f);
17 $Reserved2=freadword($f);
18 $FirstByteOfImage=freaddword($f);
19
20 $SizeBITMAPINFOHEADER=freaddword($f);
21 $Width=freaddword($f);
22 $Height=freaddword($f);
23 $biPlanes=freadword($f);
24 $biBitCount=freadword($f);
25 $RLECompression=freaddword($f);
26 $WidthxHeight=freaddword($f);
27 $biXPelsPerMeter=freaddword($f);
28 $biYPelsPerMeter=freaddword($f);
29 $NumberOfPalettesUsed=freaddword($f);
30 $NumberOfImportantColors=freaddword($f);
31
32if($biBitCount<24)
33 {
34  $img=imagecreate($Width,$Height);
35  $Colors=pow(2,$biBitCount);
36  for($p=0;$p<$Colors;$p++)
37   {
38    $B=freadbyte($f);
39    $G=freadbyte($f);
40    $R=freadbyte($f);
41    $Reserved=freadbyte($f);
42    $Palette[]=imagecolorallocate($img,$R,$G,$B);
43   };
44
45
46
47
48if($RLECompression==0)
49{
50   $Zbytek=(4-ceil(($Width/(8/$biBitCount)))%4)%4;
51
52for($y=$Height-1;$y>=0;$y--)
53    {
54     $CurrentBit=0;
55     for($x=0;$x<$Width;$x++)
56      {
57         $C=freadbits($f,$biBitCount);
58       imagesetpixel($img,$x,$y,$Palette[$C]);
59      };
60    if($CurrentBit!=0) {freadbyte($f);};
61    for($g=0;$g<$Zbytek;$g++)
62     freadbyte($f);
63     };
64
65 };
66};
67
68
69if($RLECompression==1) //$BI_RLE8
70{
71$y=$Height;
72
73$pocetb=0;
74
75while(true)
76{
77$y--;
78$prefix=freadbyte($f);
79$suffix=freadbyte($f);
80$pocetb+=2;
81
82$echoit=false;
83
84if($echoit)echo "Prefix: $prefix Suffix: $suffix<BR>";
85if(($prefix==0)and($suffix==1)) break;
86if(feof($f)) break;
87
88while(!(($prefix==0)and($suffix==0)))
89{
90 if($prefix==0)
91  {
92   $pocet=$suffix;
93   $Data.=fread($f,$pocet);
94   $pocetb+=$pocet;
95   if($pocetb%2==1) {freadbyte($f); $pocetb++;};
96  };
97 if($prefix>0)
98  {
99   $pocet=$prefix;
100   for($r=0;$r<$pocet;$r++)
101    $Data.=chr($suffix);
102  };
103 $prefix=freadbyte($f);
104 $suffix=freadbyte($f);
105 $pocetb+=2;
106 if($echoit) echo "Prefix: $prefix Suffix: $suffix<BR>";
107};
108
109for($x=0;$x<strlen($Data);$x++)
110 {
111  imagesetpixel($img,$x,$y,$Palette[ord($Data[$x])]);
112 };
113$Data="";
114
115};
116
117};
118
119
120if($RLECompression==2) //$BI_RLE4
121{
122$y=$Height;
123$pocetb=0;
124
125/*while(!feof($f))
126 echo freadbyte($f)."_".freadbyte($f)."<BR>";*/
127while(true)
128{
129//break;
130$y--;
131$prefix=freadbyte($f);
132$suffix=freadbyte($f);
133$pocetb+=2;
134
135$echoit=false;
136
137if($echoit)echo "Prefix: $prefix Suffix: $suffix<BR>";
138if(($prefix==0)and($suffix==1)) break;
139if(feof($f)) break;
140
141while(!(($prefix==0)and($suffix==0)))
142{
143 if($prefix==0)
144  {
145   $pocet=$suffix;
146
147   $CurrentBit=0;
148   for($h=0;$h<$pocet;$h++)
149    $Data.=chr(freadbits($f,4));
150   if($CurrentBit!=0) freadbits($f,4);
151   $pocetb+=ceil(($pocet/2));
152   if($pocetb%2==1) {freadbyte($f); $pocetb++;};
153  };
154 if($prefix>0)
155  {
156   $pocet=$prefix;
157   $i=0;
158   for($r=0;$r<$pocet;$r++)
159    {
160    if($i%2==0)
161     {
162      $Data.=chr($suffix%16);
163     }
164     else
165     {
166      $Data.=chr(floor($suffix/16));
167     };
168    $i++;
169    };
170  };
171 $prefix=freadbyte($f);
172 $suffix=freadbyte($f);
173 $pocetb+=2;
174 if($echoit) echo "Prefix: $prefix Suffix: $suffix<BR>";
175};
176
177for($x=0;$x<strlen($Data);$x++)
178 {
179  imagesetpixel($img,$x,$y,$Palette[ord($Data[$x])]);
180 };
181$Data="";
182
183};
184
185};
186
187
188 if($biBitCount==24)
189{
190 $img=imagecreatetruecolor($Width,$Height);
191 $Zbytek=$Width%4;
192
193   for($y=$Height-1;$y>=0;$y--)
194    {
195     for($x=0;$x<$Width;$x++)
196      {
197       $B=freadbyte($f);
198       $G=freadbyte($f);
199       $R=freadbyte($f);
200       $color=imagecolorexact($img,$R,$G,$B);
201       if($color==-1) $color=imagecolorallocate($img,$R,$G,$B);
202       imagesetpixel($img,$x,$y,$color);
203      }
204    for($z=0;$z<$Zbytek;$z++)
205     freadbyte($f);
206   };
207};
208return $img;
209
210};
211
212
213fclose($f);
214
215
216};
217
218
219/*
220* Helping functions:
221*-------------------------
222*
223* freadbyte($file) - reads 1 byte from $file
224* freadword($file) - reads 2 bytes (1 word) from $file
225* freaddword($file) - reads 4 bytes (1 dword) from $file
226* freadlngint($file) - same as freaddword($file)
227* decbin8($d) - returns binary string of d zero filled to 8
228* RetBits($byte,$start,$len) - returns bits $start->$start+$len from $byte
229* freadbits($file,$count) - reads next $count bits from $file
230* RGBToHex($R,$G,$B) - convert $R, $G, $B to hex
231* int_to_dword($n) - returns 4 byte representation of $n
232* int_to_word($n) - returns 2 byte representation of $n
233*/
234
235function freadbyte($f)
236{
237 return ord(fread($f,1));
238};
239
240function freadword($f)
241{
242 $b1=freadbyte($f);
243 $b2=freadbyte($f);
244 return $b2*256+$b1;
245};
246
247
248function freadlngint($f)
249{
250return freaddword($f);
251};
252
253function freaddword($f)
254{
255 $b1=freadword($f);
256 $b2=freadword($f);
257 return $b2*65536+$b1;
258};
259
260
261
262function RetBits($byte,$start,$len)
263{
264$bin=decbin8($byte);
265$r=bindec(substr($bin,$start,$len));
266return $r;
267
268};
269
270
271
272$CurrentBit=0;
273function freadbits($f,$count)
274{
275 global $CurrentBit,$SMode;
276 $Byte=freadbyte($f);
277 $LastCBit=$CurrentBit;
278 $CurrentBit+=$count;
279 if($CurrentBit==8)
280  {
281   $CurrentBit=0;
282  }
283 else
284  {
285   fseek($f,ftell($f)-1);
286  };
287 return RetBits($Byte,$LastCBit,$count);
288};
289
290
291
292function RGBToHex($Red,$Green,$Blue)
293  {
294   $hRed=dechex($Red);if(strlen($hRed)==1) $hRed="0$hRed";
295   $hGreen=dechex($Green);if(strlen($hGreen)==1) $hGreen="0$hGreen";
296   $hBlue=dechex($Blue);if(strlen($hBlue)==1) $hBlue="0$hBlue";
297   return($hRed.$hGreen.$hBlue);
298  };
299
300        function int_to_dword($n)
301        {
302                return chr($n & 255).chr(($n >> 8) & 255).chr(($n >> 16) & 255).chr(($n >> 24) & 255);
303        }
304        function int_to_word($n)
305        {
306                return chr($n & 255).chr(($n >> 8) & 255);
307        }
308
309
310function decbin8($d)
311{
312return decbinx($d,8);
313};
314
315function decbinx($d,$n)
316{
317$bin=decbin($d);
318$sbin=strlen($bin);
319for($j=0;$j<$n-$sbin;$j++)
320 $bin="0$bin";
321return $bin;
322};
323
324function inttobyte($n)
325{
326return chr($n);
327};
328
329?>
Note: See TracBrowser for help on using the repository browser.