• 智能门禁刷脸照片格式gif、bmp,png转换,转换base64


    随着刷脸闸机的普及,很多场所都使用了刷脸金闸机,很多时候对方传来的照片格式不对。

    刷脸闸机对应的格式都是jpg

    照片来源:访客手机上传,管理员上传,团队购票上传

    在转换的语言很多,在网站中php使用较为广泛

    一png转jpg

    1. $图片格式png_数据 = imagecreatefrompng($facecache_fullpath);
    2. $转换后保存路径 = str_replace('_ori.bmp', '_new_png.jpg', $facecache_fullpath);
    3. $res = imagejpeg($图片格式png_数据, $转换后保存路径);

    二 gif 转换jpg

    1. $图片格式png_数据 = imagecreatefromgif($facecache_fullpath);
    2. $dstFile = str_replace('_ori.bmp', '_new_gif.jpg', $facecache_fullpath);
    3. $res = imagejpeg($图片格式png_数据, $dstFile);
    4. imagedestroy($dstFile);

    三、bmp转换jpg

    1. $dstFile = str_replace('_ori.bmp', '_new_bmp.jpg', $facecache_fullpath);
    2. changeBMPtoJPGV2024($facecache_fullpath, $dstFile);
    3. function changeBMPtoJPGV2024($srcPathName,$dstFile ){
    4. $srcFile=$srcPathName;
    5. // $dstFile = str_replace('_ori.bmp', '_new.jpg', $srcPathName);
    6. $photoSize = GetImageSize($srcFile);
    7. $pw = $photoSize[0];
    8. $ph = $photoSize[1];
    9. $dstImage = ImageCreateTrueColor($pw, $ph);
    10. $white = imagecolorallocate($dstImage, 255, 255, 255);
    11. //用 $white 颜色填充图像
    12. imagefill( $dstImage, 0, 0, $white);
    13. //读取图片
    14. $srcImage = ImageCreateFromBMP_private($srcFile);
    15. //合拼图片
    16. imagecopyresampled($dstImage, $srcImage, 0, 0, 0, 0, $pw, $ph, $pw, $ph);
    17. $judge = imagejpeg($dstImage, $dstFile, 90);
    18. imagedestroy($dstImage);
    19. if($judge){
    20. return $dstFile;
    21. }else{
    22. return false;
    23. }
    24. }
    25. function ImageCreateFromBMP_private($filename) {
    26. if (!$f1 = fopen($filename, "rb"))
    27. return false;
    28. $FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1, 14));
    29. if ($FILE['file_type'] != 19778)
    30. return false;
    31. $BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel' .
    32. '/Vcompression/Vsize_bitmap/Vhoriz_resolution' .
    33. '/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1, 40));
    34. $BMP['colors'] = pow(2, $BMP['bits_per_pixel']);
    35. if ($BMP['size_bitmap'] == 0)
    36. $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset'];
    37. $BMP['bytes_per_pixel'] = $BMP['bits_per_pixel'] / 8;
    38. $BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']);
    39. $BMP['decal'] = ($BMP['width'] * $BMP['bytes_per_pixel'] / 4);
    40. $BMP['decal'] -= floor($BMP['width'] * $BMP['bytes_per_pixel'] / 4);
    41. $BMP['decal'] = 4 - (4 * $BMP['decal']);
    42. if ($BMP['decal'] == 4)
    43. $BMP['decal'] = 0;
    44. $PALETTE = array();
    45. if ($BMP['colors'] < 16777216) {
    46. $PALETTE = unpack('V' . $BMP['colors'], fread($f1, $BMP['colors'] * 4));
    47. }
    48. $IMG = fread($f1, $BMP['size_bitmap']);
    49. $VIDE = chr(0);
    50. $res = imagecreatetruecolor($BMP['width'], $BMP['height']);
    51. $P = 0;
    52. $Y = $BMP['height'] - 1;
    53. while ($Y >= 0) {
    54. $X = 0;
    55. while ($X < $BMP['width']) {
    56. switch ($BMP['bits_per_pixel']) {
    57. case 32:
    58. $COLOR = unpack("V", substr($IMG, $P, 3) . $VIDE);
    59. break;
    60. case 24:
    61. $COLOR = unpack("V", substr($IMG, $P, 3) . $VIDE);
    62. break;
    63. case 16:
    64. $COLOR = unpack("n", substr($IMG, $P, 2));
    65. $COLOR[1] = $PALETTE[$COLOR[1] + 1];
    66. break;
    67. case 8:
    68. $COLOR = unpack("n", $VIDE . substr($IMG, $P, 1));
    69. $COLOR[1] = $PALETTE[$COLOR[1] + 1];
    70. break;
    71. case 4:
    72. $COLOR = unpack("n", $VIDE . substr($IMG, floor($P), 1));
    73. if (($P * 2) % 2 == 0)
    74. $COLOR[1] = ($COLOR[1] >> 4);
    75. else
    76. $COLOR[1] = ($COLOR[1] & 0x0F);
    77. $COLOR[1] = $PALETTE[$COLOR[1] + 1];
    78. break;
    79. case 1:
    80. $COLOR = unpack("n", $VIDE . substr($IMG, floor($P), 1));
    81. if (($P * 8) % 8 == 0)
    82. $COLOR[1] = $COLOR[1] >> 7;
    83. elseif (($P * 8) % 8 == 1)
    84. $COLOR[1] = ($COLOR[1] & 0x40) >> 6;
    85. elseif (($P * 8) % 8 == 2)
    86. $COLOR[1] = ($COLOR[1] & 0x20) >> 5;
    87. elseif (($P * 8) % 8 == 3)
    88. $COLOR[1] = ($COLOR[1] & 0x10) >> 4;
    89. elseif (($P * 8) % 8 == 4)
    90. $COLOR[1] = ($COLOR[1] & 0x8) >> 3;
    91. elseif (($P * 8) % 8 == 5)
    92. $COLOR[1] = ($COLOR[1] & 0x4) >> 2;
    93. elseif (($P * 8) % 8 == 6)
    94. $COLOR[1] = ($COLOR[1] & 0x2) >> 1;
    95. elseif (($P * 8) % 8 == 7)
    96. $COLOR[1] = ($COLOR[1] & 0x1);
    97. $COLOR[1] = $PALETTE[$COLOR[1] + 1];
    98. break;
    99. default:
    100. return false;
    101. break;
    102. }
    103. imagesetpixel($res, $X, $Y, $COLOR[1]);
    104. $X++;
    105. $P += $BMP['bytes_per_pixel'];
    106. }
    107. $Y--;
    108. $P+=$BMP['decal'];
    109. }
    110. fclose($f1);
    111. return $res;
    112. }

    自动判断图片格式

    在我们不确定客户上传格式时候仅仅靠文件后缀名判断是不够的,这时候我们需要自动判断

    1. function cyberwin_getImgFile_Type($facecache_fullpath){
    2. //image/png image/bmp image/jpeg
    3. $imginfo= getimagesize($facecache_fullpath);
    4. $图片类型 = end($imginfo);
    5. if($图片类型 == "image/png"){
    6. return "png";
    7. }
    8. if($图片类型 == "image/bmp"){
    9. return "bmp";
    10. }
    11. if($图片类型 == "image/jpeg"){
    12. return "jpg";
    13. }
    14. if($图片类型 == "image/gif"){
    15. return "gif";
    16. }
    17. return $图片类型;
    18. // return end($imginfo);
    19. }

    实现自动识别自动转换

    1. $图片类型 = cyberwin_getImgFile_Type($facecache_fullpath);
    2. $目标最终地址= $facecache_fullpath;
    3. if($图片类型 == "jpg"){
    4. }
    5. if($图片类型 == "png"){
    6. $图片格式png_数据 = imagecreatefrompng($facecache_fullpath);
    7. $dstFile = str_replace('_ori.bmp', '_new_png.jpg', $facecache_fullpath);
    8. $res = imagejpeg($图片格式png_数据, $dstFile);
    9. $目标最终地址= $dstFile;
    10. }
    11. if($图片类型 == "bmp"){
    12. echo "bmp格式";
    13. $dstFile = str_replace('_ori.bmp', '_new_bmp.jpg', $facecache_fullpath);
    14. changeBMPtoJPGV2024($facecache_fullpath, $dstFile);
    15. $目标最终地址= $dstFile;
    16. }
    17. if($图片类型 == "gif"){
    18. $图片格式png_数据 = imagecreatefromgif($facecache_fullpath);
    19. $dstFile = str_replace('_ori.bmp', '_new_gif.jpg', $facecache_fullpath);
    20. $res = imagejpeg($图片格式png_数据, $dstFile);
    21. imagedestroy($dstFile);
    22. $目标最终地址= $dstFile;
    23. }

    将图片转换为base64,用于推送到刷脸设备

    1. $content互联网 = file_get_contents($目标最终地址);
    2. $file_content = chunk_split(base64_encode($content互联网)); // base64编码
    3. $img_base64 = 'data:image/' . $img_type . ';base64,' . $file_content;//合成图片的base64编码
    4. echo $img_base64;

  • 相关阅读:
    MobileViT v3论文超详细解读(翻译+精读)
    安卓性能优化手册
    【无标题】
    MS | 使用小技巧不完全总结
    图片懒加载实现方案
    聚观早报|苹果明年iPhone基带继续由高通提供
    【MyBatis框架】关联映射
    Linux---shell语法(一)
    网页里面的3D交互展示是怎么做的呢?
    搭建企业级代码托管仓库-gitlab,k8s+jenkins部署cicd必备组件
  • 原文地址:https://blog.csdn.net/cybersnow/article/details/134521519