ANTICHAT.XYZ    VIDEO.ANTICHAT.XYZ    НОВЫЕ СООБЩЕНИЯ    ФОРУМ  
Баннер 1   Баннер 2
Antichat снова доступен.
Форум Antichat (Античат) возвращается и снова открыт для пользователей. Здесь обсуждаются безопасность, программирование, технологии и многое другое. Сообщество снова собирается вместе.
Новый адрес: forum.antichat.xyz
Вернуться   Форум АНТИЧАТ > Программирование > PHP, PERL, MySQL, JavaScript
   
 
 
Опции темы Поиск в этой теме Опции просмотра

  #2  
Старый 17.05.2010, 07:50
Аватар для LStr1ke
LStr1ke
Постоянный
Регистрация: 29.07.2009
Сообщений: 400
Провел на форуме:
1455812

Репутация: 150
По умолчанию

Вот класс
PHP код:
<?php  //bmp.php
// Read & Save 24bit BMP files
 
// Author: de77
// Licence: MIT
// Webpage: de77.com
// Version: 07.02.2010
 
class BMP
{
 public static function 
imagebmp(&$img$filename false)
 {
 return 
imagebmp($img$filename);
 }
 
 public static function 
imagecreatefrombmp($filename)
 {
 return 
imagecreatefrombmp($filename);
 }
}
 
function 
imagebmp(&$img$filename false)
{
 
$wid imagesx($img);
 
$hei imagesy($img);
 
$wid_pad str_pad(''$wid 4"\0");
 
 
$size 54 + ($wid $wid_pad) * $hei;
 
 
//prepare & save header
 
$header['identifier']        = 'BM';
 
$header['file_size']        = dword($size);
 
$header['reserved']            = dword(0);
 
$header['bitmap_data']        = dword(54);
 
$header['header_size']        = dword(40);
 
$header['width']            = dword($wid);
 
$header['height']            = dword($hei);
 
$header['planes']            = word(1);
 
$header['bits_per_pixel']    = word(24);
 
$header['compression']        = dword(0);
 
$header['data_size']        = dword(0);
 
$header['h_resolution']        = dword(0);
 
$header['v_resolution']        = dword(0);
 
$header['colors']            = dword(0);
 
$header['important_colors']    = dword(0);
 
 if (
$filename)
 {
 
$f fopen($filename"wb");
 foreach (
$header AS $h)
 {
 
fwrite($f$h);
 }
 
 
//save pixels
 
for ($y=$hei-1$y>=0$y--)
 {
 for (
$x=0$x<$wid$x++)
 {
 
$rgb imagecolorat($img$x$y);
 
fwrite($fbyte3($rgb));
 }
 
fwrite($f$wid_pad);
 }
 
fclose($f);
 }
 else
 {
 foreach (
$header AS $h)
 {
 echo 
$h;
 }
 
 
//save pixels
 
for ($y=$hei-1$y>=0$y--)
 {
 for (
$x=0$x<$wid$x++)
 {
 
$rgb imagecolorat($img$x$y);
 echo 
byte3($rgb);
 }
 echo 
$wid_pad;
 }
 }
}
 
function 
imagecreatefrombmp($filename)
{
 
$f fopen($filename"rb");
 
 
//read header
 
$header fread($f54);
 
$header unpack('c2identifier/Vfile_size/Vreserved/Vbitmap_data/Vheader_size/' .
 
'Vwidth/Vheight/vplanes/vbits_per_pixel/Vcompression/Vdata_size/'.
 
'Vh_resolution/Vv_resolution/Vcolors/Vimportant_colors'$header);
 
 if (
$header['identifier1'] != 66 or $header['identifier2'] != 77)
 {
 die(
'Not a valid bmp file');
 }
 
 if (
$header['bits_per_pixel'] != 24)
 {
 die(
'Only 24bit BMP images are supported');
 }
 
 
$wid2 ceil((3*$header['width']) / 4) * 4;
 
 
$wid $header['width'];
 
$hei $header['height'];
 
 
$img imagecreatetruecolor($header['width'], $header['height']);
 
 
//read pixels
 
for ($y=$hei-1$y>=0$y--)
 {
 
$row fread($f$wid2);
 
$pixels str_split($row3);
 for (
$x=0$x<$wid$x++)
 {
 
imagesetpixel($img$x$ydwordize($pixels[$x]));
 }
 }
 
fclose($f);            
 
 return 
$img;
}    
 
function 
dwordize($str)
{
 
$a ord($str[0]);
 
$b ord($str[1]);
 
$c ord($str[2]);
 return 
$c*256*256 $b*256 $a;
}
 
function 
byte3($n)
{
 return 
chr($n 255) . chr(($n >> 8) & 255) . chr(($n >> 16) & 255);
}
function 
dword($n)
{
 return 
pack("V"$n);
}
function 
word($n)
{
 return 
pack("v"$n);
}
?>
Чтобы конвертировать используй так:

PHP код:
<?php
include 'bmp_class.php'//Класс который выложен выше

$im imagecreatefrombmp('test.bmp'); //Читаем картинку test.bmp
imagegif($im,"image.gif"); //Сохраняем картинку в image.gif
?>
У меня из 544 Кб BMP файла конвертировалось в 3.4Кб GIF
 
Ответить с цитированием
 



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Books PHP FRAGNATIC PHP, PERL, MySQL, JavaScript 186 21.02.2010 02:41
Books PSalm69 Избранное 248 27.10.2009 04:52
На PHP, как на "Новые ворота"... Mertvii-Listopad Чужие Статьи 7 18.09.2006 12:42
Безопасность в Php, Часть Iii k00p3r Чужие Статьи 0 11.07.2005 19:02
Защищаем Php. Шаг за шагом. k00p3r Чужие Статьи 0 13.06.2005 11:31



Здесь присутствуют: 1 (пользователей: 0 , гостей: 1)
 


Быстрый переход




ANTICHAT.XYZ