PHP converts BMP images to JPG images and other images in any format

Source: Internet
Author: User
Tags unpack

CopyCode The Code is as follows: <? PHP
// Example:
$ Path = root. 'upload/2009/06/03/124401282315771 .';
$ Pathall = $ path. 'bmp ';
$ MI = '';
$ MI = imagecreatefrombmp ($ pathall );
Imagejpeg ($ Mi, $ path. 'jpg ');
// The function is as follows:
Function imagecreatefrombmp ($ filename)
{
// OUVERTURE du fichier en mode binaire
If (! $ F1 = fopen ($ filename, "rb") return false;
// 1: chargement des ent �tes fichier
$ File = unpack ("vfile_type/vfile_size/vreserved/vbitmap_offset", fread ($ F1, 14 ));
If ($ file ['file_type ']! = 19778) return false;
// 2: chargement des ent �tes BMP
$ BMP = unpack ('vheader_size/vwidth/vheight/vplanes/vbits_per_pixel '.
'/Vcompression/vsize_bitmap/vhoriz_resolution '.
'/Vvert_resolution/vcolors_used/vcolors_important', fread ($ F1, 40 ));
$ BMP ['color'] = POW (2, $ BMP ['bits_per_pixel ']);
If ($ BMP ['size_bitmap'] = 0) $ BMP ['size_bitmap'] = $ file ['file_size ']-$ file ['bitmap_offset'];
$ BMP ['bytes_per_pixel '] = $ BMP ['bits_per_pixel']/8;
$ BMP ['bytes_per_pixel2 '] = Ceil ($ BMP ['bytes_per_pixel']);
$ BMP ['desc'] = ($ BMP ['width'] * $ BMP ['bytes_per_pixel ']/4 );
$ BMP ['desc']-= floor ($ BMP ['width'] * $ BMP ['bytes_per_pixel ']/4 );
$ BMP ['Demo'] = 4-(4 * $ BMP ['Demo']);
If ($ BMP ['Demo'] = 4) $ BMP ['Demo'] = 0;
// 3: chargement des couleurs de la palette
$ Palette = array ();
If ($ BMP ['colors '] <16777216)
{
$ Palette = unpack ('V'. $ BMP ['colors '], fread ($ F1, $ BMP ['colors'] * 4 ));
}
// 4: CR �ation de l' Image
$ IMG = fread ($ F1, $ BMP ['size_bitmap']);
$ Vide = CHR (0 );
$ Res = imagecreatetruecolor ($ BMP ['width'], $ BMP ['height']);
$ P = 0;
$ Y = $ BMP ['height']-1;
While ($ Y> = 0)
{
$ X = 0;
While ($ x <$ BMP ['width'])
{
If ($ BMP ['bits_per_pixel '] = 24)
$ Color = unpack ("V", substr ($ IMG, $ P, 3). $ vide );
Elseif ($ BMP ['bits_per_pixel '] = 16)
{
$ Color = unpack ("N", substr ($ IMG, $ P, 2 ));
$ Color [1] = $ palette [$ color [1] + 1];
}
Elseif ($ BMP ['bits_per_pixel '] = 8)
{
$ Color = unpack ("N", $ vide. substr ($ IMG, $ P, 1 ));
$ Color [1] = $ palette [$ color [1] + 1];
}
Elseif ($ BMP ['bits_per_pixel '] = 4)
{
$ Color = unpack ("N", $ vide. substr ($ IMG, floor ($ P), 1 ));
If ($ p * 2) % 2 = 0) $ color [1] = ($ color [1]> 4 ); else $ color [1] = ($ color [1] & 0x0f );
$ Color [1] = $ palette [$ color [1] + 1];
}
Elseif ($ BMP ['bits_per_pixel '] = 1)
{
$ Color = unpack ("N", $ vide. substr ($ IMG, floor ($ P), 1 ));
If ($ p * 8) % 8 = 0) $ color [1] = $ color [1]> 7;
Elseif ($ p * 8) % 8 = 1) $ color [1] = ($ color [1] & 0x40)> 6;
Elseif ($ p * 8) % 8 = 2) $ color [1] = ($ color [1] & 0x20)> 5;
Elseif ($ p * 8) % 8 = 3) $ color [1] = ($ color [1] & 0x10)> 4;
Elseif ($ p * 8) % 8 = 4) $ color [1] = ($ color [1] & 0x8)> 3;
Elseif ($ p * 8) % 8 = 5) $ color [1] = ($ color [1] & 0x4)> 2;
Elseif ($ p * 8) % 8 = 6) $ color [1] = ($ color [1] & 0x2)> 1;
Elseif ($ p * 8) % 8 = 7) $ color [1] = ($ color [1] & 0x1 );
$ Color [1] = $ palette [$ color [1] + 1];
}
Else
Return false;
Imagesetpixel ($ res, $ X, $ y, $ color [1]);
$ X ++;
$ P + = $ BMP ['bytes_per_pixel '];
}
$ Y --;
$ P + = $ BMP ['Demo'];
}
// Fermeture du fichier
Fclose ($ F1 );
Return $ res;
}
?>

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.