gpt4 book ai didi

php - 使用 PHP 中的 GD 库将位图文件转换为 JPEG

转载 作者:可可西里 更新时间:2023-11-01 13:14:08 26 4
gpt4 key购买 nike

我一直在尝试找出一种使用 PHP 中的 GD 库将位图文件转换为 JPEG 的方法。

我尝试了很多实现,但似乎没有任何效果。我曾试图告诉我的客户他们不应该使用位图文件,但他坚持并坦率地说,他对计算机的了解不够,无法自行将它们转换为 JPG。

我不能在此服务器上使用 ImageMagick,我需要一个纯 GD 解决方案。预先感谢您提供的所有帮助。

编辑:

正在使用的位图图像是 16 位的,这就是问题所在。

我有这个功能,我正在工作......有点:

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;
}

生成的图像是这样的:

Uploaded Image

如果您查看左侧的图像,您会发现生成的图像没有正确对齐。小条子属于右手边。代码哪里出错了?问题出现在 16 位 else-if 中。

再次感谢您的帮助。

最佳答案

使用这个函数:

http://www.programmierer-forum.de/function-imagecreatefrombmp-welche-variante-laeuft-t143137.htm

它支持多种比特率,例如 16 位和 32 位。此外,它还包含一些错误修复,涉及丢失的文件大小、负调色板、错误输出、额外的 16 位掩码 header (这是 16 位的主要问题)和减少的调色板 (biClrUsed)。

希望你喜欢它;)

2015 年更新:此函数现在是 DOMPDF 的一部分(在页面上搜索“imagecreatefrombmp”)并得到完善。现在它涵盖压缩的 4 位和 8 位,忽略不重要的 header 并支持特殊的 16 位 565 掩码。

关于php - 使用 PHP 中的 GD 库将位图文件转换为 JPEG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1586094/

26 4 0
文章推荐: PHP - 替换图像中的颜色
文章推荐: html -
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com