温馨提示×

PHP中位图文件的读取与保存

PHP
小樊
82
2024-08-28 05:55:12
栏目: 编程语言

在 PHP 中,处理位图文件通常是通过 GD 库或 ImageMagick 库来实现的。这里我将向您展示如何使用 GD 库读取和保存 BMP 文件。

首先,确保您的 PHP 安装包含了 GD 库。大多数环境中默认都包含了这个库。接下来,我们需要创建一个函数来读取 BMP 文件并将其转换为 GD 图像资源。然后,我们可以选择将该图像资源保存为新的 BMP 文件。

<?php
function imagecreatefrombmp($filename) {
    $file = fopen($filename, "rb");
    $read = fread($file, 10);
    while (!feof($file) && ($read<>"")) {
        $read .= fread($file, 1024);
    }
    $temp = unpack("H*", $read);
    $hex = $temp[1];
    $header = substr($hex, 0, 108);
    if (substr($header, 0, 4) == "424d") {
        $header_parts = str_split($header, 2);
        $width = hexdec($header_parts[19] . $header_parts[18]);
        $height = hexdec($header_parts[23] . $header_parts[22]);
        unset($header_parts);
    }
    $x = 0;
    $y = 1;
    $image = imagecreatetruecolor($width, $height);
    $body = substr($hex, 108);
    $body_size = strlen($body);
    $header_size = ($width * $height);
    $usePadding = false;
    if ($body_size < $header_size * 3) {
        $usePadding = true;
    }
    for ($i = 0; $i < $body_size; $i += 3) {
        if ($x >= $width) {
            if ($usePadding) {
                $i += $width % 4;
            }
            $x = 0;
            $y++;
            if ($y > $height) {
                break;
            }
        }
        $color = imagecolorallocate($image, hexdec($body[$i + 2]), hexdec($body[$i + 1]), hexdec($body[$i]));
        imagesetpixel($image, $x, $height - $y, $color);
        $x++;
    }
    unset($body);
    return $image;
}

function imagebmp(&$image, $filename = false) {
    $width = imagesx($image);
    $height = imagesy($image);
    $colors = imagecolorstotal($image);

    if ($filename === false) {
        $filename = "php://output";
    }

    $result = fopen($filename, "wb");

    // BMP header
    $header = "BM";
    $header .= pack("V", 54 + $width * $height * 3); // File size
    $header .= pack("V", 0); // Reserved
    $header .= pack("V", 54); // Offset to image data

    // DIB header
    $header .= pack("V", 40); // Header size
    $header .= pack("V", $width); // Width
    $header .= pack("V", $height); // Height
    $header .= pack("v", 1); // Planes
    $header .= pack("v", 24); // Bits per pixel
    $header .= pack("V", 0); // Compression
    $header .= pack("V", $width * $height * 3); // Image size
    $header .= pack("V", 0); // Horizontal resolution
    $header .= pack("V", 0); // Vertical resolution
    $header .= pack("V", 0); // Colors used
    $header .= pack("V", 0); // Important colors

    fwrite($result, $header);

    for ($y = $height - 1; $y >= 0; $y--) {
        for ($x = 0; $x < $width; $x++) {
            $color = imagecolorat($image, $x, $y);
            $rgb = sprintf("%06x", $color);
            fwrite($result, pack("C*", hexdec(substr($rgb, 4, 2)), hexdec(substr($rgb, 2, 2)), hexdec(substr($rgb, 0, 2))));
        }
        fwrite($result, pack("C*", 0, 0, 0, 0));
    }

    fclose($result);
}

// 读取 BMP 文件
$image = imagecreatefrombmp("input.bmp");

// 保存为新的 BMP 文件
imagebmp($image, "output.bmp");

// 销毁图像资源
imagedestroy($image);
?>

这段代码定义了两个函数:imagecreatefrombmp() 用于从 BMP 文件创建 GD 图像资源,imagebmp() 用于将 GD 图像资源保存为 BMP 文件。请注意,这些函数仅适用于 24 位 BMP 文件。

0