您好,欢迎访问一九零五行业门户网

php bmp转jpg的实现方法

php bmp转jpg的实现方法:首先开启gd扩展库;然后用“function_exist()”函数“imagecreatefrombmp”检查是否存在;最后通过“imagecreatefrombmp_private”方法实现转换即可。
推荐:《php视频教程》
进行图片格式转换前,先输出phpinfo()查看php库信息,查看gd扩展库是否开启,若未开启,打开php.ini文件,用查找工具找到extension=php_gd2.dll,把extension=php_gd2.dll 前面的【;】,就可以了
这里转换成为jpg格式需要jpeg support支持,png需要 png support支持,对应的先检查自己转的格式是否支持
imagejpeg()函数失败也许是你的文件夹权限问题,得多注意
可以先用function_exist()函数imagecreatefrombmp检查是否存在,下例中的imagecreatefrombmp_private用来代替imagecreatefrombmp方法
示例代码如下:
<?php $srcfile ='test.bmp';//需要转换的图片$info = getimagesize($srcfile); $srcfileext=$info['mime'];//判断是否是bmp格式if($srcfileext=='image/x-ms-bmp'){ $result = changebmptojpg($srcfile); if($result){ echo $result; }else{ echo "转换失败!"; }}else{ echo "该图片不是bmp格式";} exit;function imagecreatefrombmp_private($filename) { if (!$f1 = fopen($filename, "rb")) return false; $file = unpack("vfile_type/vfile_size/vreserved/vbitmap_offset", fread($f1, 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($f1, 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($f1, $bmp['colors'] * 4)); } $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']) { switch ($bmp['bits_per_pixel']) { case 32: $color = unpack("v", substr($img, $p, 3) . $vide); break; case 24: $color = unpack("v", substr($img, $p, 3) . $vide); break; case 16: $color = unpack("n", substr($img, $p, 2)); $color[1] = $palette[$color[1] + 1]; break; case 8: $color = unpack("n", $vide . substr($img, $p, 1)); $color[1] = $palette[$color[1] + 1]; break; case 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]; break; case 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]; break; default: return false; break; } imagesetpixel($res, $x, $y, $color[1]); $x++; $p += $bmp['bytes_per_pixel']; } $y--; $p+=$bmp['decal']; } fclose($f1); return $res;}function changebmptojpg($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); //用 $white 颜色填充图像 imagefill( $dstimage, 0, 0, $white); //读取图片 $srcimage = imagecreatefrombmp_private($srcfile); //合拼图片 imagecopyresampled($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 bmp转jpg的实现方法的详细内容。
其它类似信息

推荐信息