PHP convert BMP image format to JPG format

Source: Internet
Author: User
Tags bmp image fread imagejpeg unpack

Before the image format conversion, the output phpinfo () to view the PHP library information, to see if the GD extension Library is open, if not opened, opened the php.ini file, Use the Find tool to find Extension=php_gd2.dll, Extension=php_ Gd2.dll the ";" in front of you.

Here the conversion to JPG format is required for JPEG support, PNG supports PNG support, corresponding to check the format of the first to support

Imagejpeg () function failure may be your folder permissions problem, pay more attention to

You can use the Function_exist () function imagecreatefrombmp to check for presence, imagecreatefrombmp_private in the following example is used instead of the Imagecreatefrombmp method

The sample code is as follows:

<?PHP$srcFile= ' test.bmp ';//images that need to be converted$info=getimagesize($srcFile); $srcFileExt=$info[' MIME '];//determine if it is a BMP formatif($srcFileExt= = ' Image/x-ms-bmp '){    $result= Changebmptojpg ($srcFile); if($result){        Echo $result; }Else{        Echo"Conversion failed!"; }}Else{    Echo"The picture is not in BMP format";} Exit;functionImagecreatefrombmp_private ($filename) {    if(!$f 1=fopen($filename, "RB"))        return false; $FILE=Unpack("Vfile_type/vfile_size/vreserved/vbitmap_offset",fread($f 1, 14)); if($FILE[' File_type ']! = 19778)        return false; $BMP=Unpack(' Vheader_size/vwidth/vheight/vplanes/vbits_per_pixel '.        '/vcompression/vsize_bitmap/vhoriz_resolution '. '/vvert_resolution/vcolors_used/vcolors_important ',fread($f 1, 40)); $BMP[' colors '] =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[' decal '] = ($BMP[' Width '] *$BMP[' Bytes_per_pixel ']/4); $BMP[' decal ']-= Floor($BMP[' Width '] *$BMP[' Bytes_per_pixel ']/4); $BMP[' decal '] = 4-(4 *$BMP[' Decal ']); if($BMP[' decal '] = = 4)        $BMP[' decal '] = 0; $PALETTE=Array(); if($BMP[' Colors '] < 16777216) {        $PALETTE=Unpack(' V '.$BMP[' Colors '],fread($f 1,$BMP[' Colors '] * 4)); }    $IMG=fread($f 1,$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 ']) {            Switch($BMP[' Bits_per_pixel ']) {                 Case32:$COLOR=Unpack("V",substr($IMG,$P, 3).$VIDE);  Break;  Case24:$COLOR=Unpack("V",substr($IMG,$P, 3).$VIDE);  Break;  Case16:$COLOR=Unpack("N",substr($IMG,$P, 2)); $COLOR[1] =$PALETTE[$COLOR[1] + 1];  Break;  Case8:$COLOR=Unpack("N",$VIDE.substr($IMG,$P, 1)); $COLOR[1] =$PALETTE[$COLOR[1] + 1];  Break;  Case4:$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];  Break;  Case1:$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];  Break; default:return false;  Break; } imagesetpixel ($res,$X,$Y,$COLOR[1]); $X++; $P+=$BMP[' Bytes_per_pixel ']; }        $Y--; $P+=$BMP[' Decal ']; }    fclose($f 1); return $res;}functionChangebmptojpg ($srcPathName){    $srcFile=$srcPathName; $dstFile=Str_replace('. bmp ', '. jpg ',$srcPathName); $photoSize=getimagesize($srcFile); $PW=$photoSize[0]; $ph=$photoSize[1]; $dstImage= Imagecreatetruecolor ($PW,$ph); $white= Imagecolorallocate ($dstImage, 255, 255, 255); //fill an image with $white colorImagefill ($dstImage, 0, 0,$white); //Reading Pictures    $srcImage= Imagecreatefrombmp_private ($srcFile); //co-collage pictureimagecopyresampled ($dstImage,$srcImage, 0, 0, 0, 0,$PW,$ph,$PW,$ph); $judge= Imagejpeg ($dstImage,$dstFile, 90); Imagedestroy ($dstImage); if($judge){        return $dstFile; }Else{        return false; }}?>

PHP convert BMP image format to JPG format

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.