蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

在 PHP 8.4 CLI 实测 GD 能力检测、PNG 绘图字节魔数、像素读取与仿射变换

PHPWindows x64、PHP 8.4 本地 CLI 环境。explore-image-gd.php
explore-image-gd.php121 行
<?php
declare(strict_types=1);

$cases = [];

// Case 1: gd_info() capability report
$info = gd_info();
$infoActual = [
    'gd_loaded' => extension_loaded('gd'),
    'gd_version' => $info['GD Version'],
    'png_support' => $info['PNG Support'],
    'jpeg_support' => $info['JPEG Support'],
    'gif_create_support' => $info['GIF Create Support'],
    'freetype_support' => $info['FreeType Support'],
    'webp_support' => $info['WebP Support'],
];
$infoExpected = [
    'gd_loaded' => true,
    'gd_version' => 'bundled (2.1.0 compatible)',
    'png_support' => true,
    'jpeg_support' => true,
    'gif_create_support' => true,
    'freetype_support' => true,
    'webp_support' => true,
];
$cases[] = [
    'name' => 'gd_info_capabilities',
    'passed' => $infoActual === $infoExpected,
    'expected' => $infoExpected,
    'actual' => $infoActual,
];

// Case 2: imagecreatetruecolor + imagecolorallocate + imagefilledrectangle + imagearc + imagepng magic bytes
$img = imagecreatetruecolor(160, 90);
imagealphablending($img, true);
$bg = imagecolorallocate($img, 240, 248, 255);
$rectColor = imagecolorallocate($img, 30, 120, 210);
$arcColor = imagecolorallocatealpha($img, 220, 60, 40, 32);
imagefilledrectangle($img, 0, 0, 159, 89, $bg);
imagefilledrectangle($img, 10, 10, 80, 60, $rectColor);
imagearc($img, 120, 45, 40, 40, 0, 360, $arcColor);
imagechar($img, 3, 15, 68, 'G', $rectColor);

ob_start();
imagepng($img);
$pngBytes = ob_get_clean();

$drawActual = [
    'width' => imagesx($img),
    'height' => imagesy($img),
    'png_signature_hex' => bin2hex(substr($pngBytes, 0, 8)),
    'png_non_empty' => strlen($pngBytes) > 100,
];
$drawExpected = [
    'width' => 160,
    'height' => 90,
    'png_signature_hex' => '89504e470d0a1a0a',
    'png_non_empty' => true,
];
$cases[] = [
    'name' => 'gd_create_draw_and_png_bytes',
    'passed' => $drawActual === $drawExpected,
    'expected' => $drawExpected,
    'actual' => $drawActual,
];

// Case 3: imagecolorat, getimagesizefromstring, image_type_to_extension, image_type_to_mime_type
$pixel = imagecolorat($img, 25, 25);
$r = ($pixel >> 16) & 0xFF;
$g = ($pixel >> 8) & 0xFF;
$b = $pixel & 0xFF;
$sizeInfo = getimagesizefromstring($pngBytes);

$metaActual = [
    'pixel_rgb' => [$r, $g, $b],
    'parsed_width' => $sizeInfo[0],
    'parsed_height' => $sizeInfo[1],
    'parsed_mime' => $sizeInfo['mime'],
    'type_extension' => image_type_to_extension(IMAGETYPE_PNG),
    'type_mime' => image_type_to_mime_type(IMAGETYPE_PNG),
];
$metaExpected = [
    'pixel_rgb' => [30, 120, 210],
    'parsed_width' => 160,
    'parsed_height' => 90,
    'parsed_mime' => 'image/png',
    'type_extension' => '.png',
    'type_mime' => 'image/png',
];
$cases[] = [
    'name' => 'gd_inspect_pixel_and_metadata',
    'passed' => $metaActual === $metaExpected,
    'expected' => $metaExpected,
    'actual' => $metaActual,
];

// Case 4: imageaffinematrixget / imageaffinematrixconcat / imageaffine
$scaleA = imageaffinematrixget(IMG_AFFINE_SCALE, ['x' => 0.5, 'y' => 0.5]);
$identity = imageaffinematrixget(IMG_AFFINE_TRANSLATE, ['x' => 0, 'y' => 0]);
$combined = imageaffinematrixconcat($scaleA, $identity);
$scaled = imageaffine($img, $combined);

$affineActual = [
    'matrix_len' => count($combined),
    'scaled_width' => imagesx($scaled),
    'scaled_height' => imagesy($scaled),
];
$affineExpected = [
    'matrix_len' => 6,
    'scaled_width' => 79,
    'scaled_height' => 45,
];
$cases[] = [
    'name' => 'gd_affine_transform',
    'passed' => $affineActual === $affineExpected,
    'expected' => $affineExpected,
    'actual' => $affineActual,
];

echo json_encode(['version' => PHP_VERSION, 'cases' => $cases], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
本例需在 Windows x64、PHP 8.4 本地 CLI 环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、PHP 8.4 本地 CLI 环境。

入口文件:projects/php-image-gd-lab/explore-image-gd.php

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-image-gd.php,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:PHP

课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip

下载课程关联工程包:PHP

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1255.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习