使用PHP中的GD库将位图文件转换为JPEG
我一直在试图找到一种使用PHP中的GD库将位图文件转换为JPEG的方法 我已经尝试了许多实现,但似乎没有任何效果。我曾试图告诉我的客户,他们不应该使用位图文件,但他坚持认为,坦率地说,他对计算机了解不够,无法自己将其转换为JPG 我不能在此服务器上使用ImageMagick,我需要一个纯GD解决方案。提前感谢您的帮助 编辑: 正在使用的位图图像是16位的,这就是问题所在 我有这个功能,我有工作。。。。有点:使用PHP中的GD库将位图文件转换为JPEG,php,bitmap,gd,jpeg,Php,Bitmap,Gd,Jpeg,我一直在试图找到一种使用PHP中的GD库将位图文件转换为JPEG的方法 我已经尝试了许多实现,但似乎没有任何效果。我曾试图告诉我的客户,他们不应该使用位图文件,但他坚持认为,坦率地说,他对计算机了解不够,无法自己将其转换为JPG 我不能在此服务器上使用ImageMagick,我需要一个纯GD解决方案。提前感谢您的帮助 编辑: 正在使用的位图图像是16位的,这就是问题所在 我有这个功能,我有工作。。。。有点: function ImageCreateFromBMP($filename) {
function ImageCreateFromBMP($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 && $BMP['colors'] != 65536) {
$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']) {
if ($BMP['bits_per_pixel'] == 24)
$COLOR = unpack("V",substr($IMG,$P,3).$VIDE);
elseif ($BMP['bits_per_pixel'] == 16) {
$COLOR = unpack("v",substr($IMG,$P,2));
$blue = ($COLOR[1] & 0x001f) << 3;
$green = ($COLOR[1] & 0x07e0) >> 3;
$red = ($COLOR[1] & 0xf800) >> 8;
$COLOR[1] = $red * 65536 + $green * 256 + $blue;
}
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'];
}
fclose($f1);
return $res;
}
函数ImageCreateFromBMP($filename){
如果(!$f1=fopen($filename,“rb”))返回FALSE;
$FILE=unpack(“vfile_类型/vfile_大小/Vreserved/Vbitmap_偏移量”,fread($f1,14));
如果($FILE['FILE_type']!=19778),则返回FALSE;
$BMP=unpack('Vheader\u size/Vwidth/Vheight/vplanes/vbits\u per\u pixel')。
“/Vcompression/Vsize_位图/Vhoriz_分辨率”。
“/Vvert_分辨率/Vcolors_使用/Vcolors_重要”,fread($f1,40));
$BMP['colors']=pow(2,$BMP['bits_per_pixel']);
如果($BMP['size\u bitmap']==0)$BMP['size\u bitmap']=$FILE['FILE\u size']-$FILE['bitmap\u offset'];
$BMP['bytes\u per\u pixel']=$BMP['bits\u per\u pixel']/8;
$BMP['bytes\u per_pixel2']=ceil($BMP['bytes\u per_pixel']);
$BMP['deal']=($BMP['width']*$BMP['bytes\u/u pixel']/4);
$BMP['deal']-=地板($BMP['width']*$BMP['bytes\u/u pixel']/4);
$BMP['seal']=4-(4*$BMP['seal']);
如果($BMP['deal']=4)$BMP['deal']=0;
$palete=array();
如果($BMP['colors']<16777216&&$BMP['colors']!=65536){
$palete=unpack('V'.$BMP['colors']),fread($f1,$BMP['colors']*4));
}
$IMG=fread($f1,$BMP['size\u bitmap']);
$VIDE=chr(0);
$res=imagecreatetruecolor($BMP['width'],$BMP['height']);
$P=0;
$Y=$BMP['height']-1;
而($Y>=0){
$X=0;
而($X<$BMP['width'])){
如果($BMP['bits\u per\u pixel']==24)
$COLOR=unpack(“V”,substr($IMG,$P,3)。$VIDE);
elseif($BMP['bits\u per\u pixel']==16){
$COLOR=unpack(“v”,substr($IMG,$P,2));
$blue=($COLOR[1]&0x001f)>3;
$red=($COLOR[1]&0xf800)>>8;
$COLOR[1]=$red*65536+$green*256+$blue;
}
elseif($BMP['bits\u per\u pixel']==8){
$COLOR=unpack(“n”,参见substr($IMG,$P,1));
$COLOR[1]=$palete[$COLOR[1]+1];
}
elseif($BMP['bits\u per\u pixel']==4){
$COLOR=unpack(“n”,参见substr($IMG,floor($P),1));
如果($P*2)%2==0)$COLOR[1]=($COLOR[1]>>4);否则$COLOR[1]=($COLOR[1]&0x0F);
$COLOR[1]=$palete[$COLOR[1]+1];
}
elseif($BMP['bits\u per\u pixel']==1){
$COLOR=unpack(“n”,参见substr($IMG,floor($P),1));
如果($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]=$palete[$COLOR[1]+1];
}
其他的
返回FALSE;
imagesetpixel($res,$X,$Y,$COLOR[1]);
$X++;
$P+=$BMP[‘每像素字节数’];
}
$Y--;
$P+=$BMP[‘贴花’];
}
fclose(f1美元);
返回$res;
}
生成的图像如下所示:
如果您查看左侧的图像,可以看到生成的图像没有正确对齐。小银条应该在右手边。代码哪里出错了?问题发生在16位else if中
再次感谢您的帮助。功能如何
bool imagejpeg(资源$image[,,
字符串$filename[,int$quality]]
)
imagejpeg()从中创建JPEG文件
给定的图像
有关在GD中支持BMP格式的帮助,请看一看,例如
编辑:这不支持16位图像,这是正确的,因为原始位图规范不支持它。在您的情况下,请找出用于编码颜色值的位模式。我假设R和B是5位,G是6位,在这个解决方案中顺序是BGR(请插入我上面链接的代码):
else如果($bits==16){
$gd_scan_line=“”;
$j=0;
而($j<$scan\u line\u size){
$byte1=$scan_line{$j++};
$byte2=$scan_line{$j++};
$b=chr($byte1>>3)*(255/31);
$g=(chr($byte1&0x07)+chr($byte2>>5))*(255/63);
$r=chr($byte2和0x1F)*(255/31);
$gd\U扫描线=“\x00$r$g$b”;
}
请注意,我没有测试这段代码(特别是,我不确定是否可以缩放到0..255),它只在使用了5-6-5位模式时才起作用(当然,它也可以与其他模式一起使用,但颜色将是错误的)。虽然GD本机不支持BMP,但稍微大一点的谷歌搜索提供了一个imagecreatefrombmp()函数的功能
我还没有试过,但我相信其中至少有一款适合你。我想:
function convert_to_jpeg( $input_path, $output_path )
{
$image = imagecreatefromstring(file_get_contents($input_path));
imagejpeg($image, $output_path);
imagedestroy($image);
}
这将采用GD可以处理的任何格式作为输入,并输出一个jpeg文件。我不知道你们使用的GD版本是什么,但我的版本可以完美地处理.bmp,我们在我工作的前一家公司使用的版本也是如此。(分别在Mac OS X 10.6和CentOS 5上)
编辑:忘记了!哎哟
function convert_to_jpeg( $input_path, $output_path )
{
$image = imagecreatefromstring(file_get_contents($input_path));
imagejpeg($image, $output_path);
imagedestroy($image);
}