You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用PHP裁剪JPEG图像的空白白色区域并将有效内容左移

PHP裁剪JPEG空白区域实现方案

核心依赖PHP内置的GD扩展,绝大多数PHP环境默认已开启该扩展,实现逻辑如下:

  • 加载原始JPG图像资源
  • 逐列扫描图像,找到第一个包含非白色像素的左边界
  • 也可同步扫描右、上、下边界,裁剪掉所有多余空白区域
  • 将有效内容区域复制到新生成的图像画布最左侧,即可实现有效内容左移

完整实现代码

<?php
// 配置项
$sourcePath = '你的原始图片路径.jpg';
$outputPath = '裁剪后的输出路径.jpg';
// 背景色阈值,纯白就是255,如果有轻微偏白可以适当调低
$whiteThreshold = 255;

// 加载原始图像
$sourceImg = imagecreatefromjpeg($sourcePath);
if (!$sourceImg) {
    die('图像加载失败');
}
$width = imagesx($sourceImg);
$height = imagesy($sourceImg);

// 找左边界:从左到右逐列扫描
$leftBound = 0;
for ($x = 0; $x < $width; $x++) {
    $hasBlack = false;
    for ($y = 0; $y < $height; $y++) {
        $pixel = imagecolorat($sourceImg, $x, $y);
        $r = ($pixel >> 16) & 0xFF;
        $g = ($pixel >> 8) & 0xFF;
        $b = $pixel & 0xFF;
        // 只要有一个像素不是白色,当前列就是左边界
        if ($r < $whiteThreshold || $g < $whiteThreshold || $b < $whiteThreshold) {
            $hasBlack = true;
            break;
        }
    }
    if ($hasBlack) {
        $leftBound = $x;
        break;
    }
}

// 可选:找右边界,裁剪右侧空白
$rightBound = $width - 1;
for ($x = $width - 1; $x >= $leftBound; $x--) {
    $hasBlack = false;
    for ($y = 0; $y < $height; $y++) {
        $pixel = imagecolorat($sourceImg, $x, $y);
        $r = ($pixel >> 16) & 0xFF;
        $g = ($pixel >> 8) & 0xFF;
        $b = $pixel & 0xFF;
        if ($r < $whiteThreshold || $g < $whiteThreshold || $b < $whiteThreshold) {
            $hasBlack = true;
            break;
        }
    }
    if ($hasBlack) {
        $rightBound = $x;
        break;
    }
}

// 可选:找上下边界,裁剪上下空白
$topBound = 0;
for ($y = 0; $y < $height; $y++) {
    $hasBlack = false;
    for ($x = $leftBound; $x <= $rightBound; $x++) {
        $pixel = imagecolorat($sourceImg, $x, $y);
        $r = ($pixel >> 16) & 0xFF;
        $g = ($pixel >> 8) & 0xFF;
        $b = $pixel & 0xFF;
        if ($r < $whiteThreshold || $g < $whiteThreshold || $b < $whiteThreshold) {
            $hasBlack = true;
            break;
        }
    }
    if ($hasBlack) {
        $topBound = $y;
        break;
    }
}

$bottomBound = $height - 1;
for ($y = $height - 1; $y >= $topBound; $y--) {
    $hasBlack = false;
    for ($x = $leftBound; $x <= $rightBound; $x++) {
        $pixel = imagecolorat($sourceImg, $x, $y);
        $r = ($pixel >> 16) & 0xFF;
        $g = ($pixel >> 8) & 0xFF;
        $b = $pixel & 0xFF;
        if ($r < $whiteThreshold || $g < $whiteThreshold || $b < $whiteThreshold) {
            $hasBlack = true;
            break;
        }
    }
    if ($hasBlack) {
        $bottomBound = $y;
        break;
    }
}

// 计算有效内容的宽高
$newWidth = $rightBound - $leftBound + 1;
$newHeight = $bottomBound - $topBound + 1;

// 创建新画布,背景设为白色
$newImg = imagecreatetruecolor($newWidth, $newHeight);
$white = imagecolorallocate($newImg, 255, 255, 255);
imagefill($newImg, 0, 0, $white);

// 把原始图像的有效区域复制到新画布左上角,实现左移
imagecopy($newImg, $sourceImg, 0, 0, $leftBound, $topBound, $newWidth, $newHeight);

// 输出图像,质量设为90可根据需求调整
imagejpeg($newImg, $outputPath, 90);

// 销毁资源释放内存
imagedestroy($sourceImg);
imagedestroy($newImg);
?>

注意事项

  • 若你的图像存在轻微的偏色(不是纯纯白背景),可适当调低$whiteThreshold的数值,比如调到250,避免把偏白的背景当成有效内容
  • 如果不需要裁剪上下、右侧的空白,直接删掉对应的边界查找代码,把$newWidth设为原始宽度减去左边界,$newHeight设为原始高度,复制的时候起始坐标对应调整即可
  • 处理大尺寸图像时可适当增加PHP的内存限制,避免执行中断

内容的提问来源于stack exchange,提问作者Vasiliy Tsiganov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 19:36:00