PHP implementation of the processing of BMP format picture of the method detailed

Source: Internet
Author: User
This article mainly introduced the PHP processing BMP format Picture method, combined with the concrete instance form analysis PHP operation BMP format Picture correlation function and the use skill, needs the friend to refer to the next

Specific as follows:

During the day QA put forward the project upload image has a problem, specifically: Upload success, preview failed. After I went, and then uploaded a few other pictures can be uploaded, and then carefully asked the next he uploaded the picture, looked after using the getimagesize function to print the next.

Array (  [0] = 494  [1] = 260  [2] = 6  [3] = width= "494" height= "260"  [bits] = 24
  
   [mime] = image/x-ms-bmp)
  

Look carefully, the above printed array, MIME is image/x-ms-bmp, that is, BMP format, the impression of the PHP GD library does not handle this function, and then I in the configuration file, as long as the mime for this can not upload.

Back in the evening, I will be on the Internet to find PHP processing BMP-related methods, and finally found in the StackOverflow, the post said into the official website about imagecreate function comments, there is a ImageCreateFromBMP method, first the BMP format of the file to this function call, Again with imagejpeg save on line, later tried, found Imagepng also line, the following posted code.

<?phpfunction imagecreatefrombmp ($filename) {//ouverture du fichier en mode binaire if (! $f 1 = fopen ($filename, "RB"  )) return FALSE;  1:chargement des ent tes fichier $FILE = Unpack ("Vfile_type/vfile_size/vreserved/vbitmap_offset", Fread ($f 1, 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 ($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;  3:chargement des couleurs de la palette $PALETTE = array ();  if ($BMP [' Colors '] < 16777216) {$PALETTE = unpack (' V '. $BMP [' colors '], fread ($f 1, $BMP [' colors '] * 4));  }//4:cr ation de l ' Image $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 ']) {if ($BMP [' bits_per_pixel '] = =) $COLOR = Unpack ("V", substr ($IMG, $P, 3). $      vide);        ElseIf ($BMP [' bits_per_pixel '] = = +) {$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 [' decal '];  }//fermeture du fichier fclose ($f 1); return $res;} $pic = ' 2.bmp '; $res = Imagecreatefrombmp ($pic); Imagepng ($res, ' 1.png '); imagejpeg ($res, ' 1.jpeg ');

Related recommendations:

bmp Image php to convert bmp images to JPG and other images in any format

BMP Format picture PHP to convert bmp images to JPG and other images in any format

PHP uses GD library to generate BMP format images (imagebmp)

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.