Crop whitespace from image in PHP

前端 未结 6 597
盖世英雄少女心
盖世英雄少女心 2020-11-28 04:43

Is it possible to remove the whitespace surrounding an image in PHP?

NOTE: to clarify I mean something like photoshops trim feature.

Thanks.

6条回答
  •  旧巷少年郎
    2020-11-28 05:28

    Gnud's script redundantly calls imagesx and imagesy. It also iterates every pixel on every side, even when the corners overlap. This improved version eliminates redundant function calls and checks every pixel only once, granting a significant increase in speed. The function returns a status ($result['#']) equal to 2 if every pixel is the trimmed.

    example();
    function example(){
        $img = imagecreatefromjpeg("http://ecx.images-amazon.com/images/I/413XvF0yukL._SL500_AA280_.jpg");
    
        // find the trimmed image border
        $box = imageTrimBox($img);
    
        // copy cropped portion
        $img2 = imagecreate($box['w'], $box['h']);
        imagecopy($img2, $img, 0, 0, $box['l'], $box['t'], $box['w'], $box['h']);
    
        // output cropped image to the browser
        header('Content-Type: image/png');
        imagepng($img2);
    
        imagedestroy($img);
        imagedestroy($img2);
    }
    
    
    
    function imageTrimBox($img, $hex=null){
    if (!ctype_xdigit($hex)) $hex = imagecolorat($img, 0,0);
    $b_top = $b_lft = 0;
    $b_rt = $w1 = $w2 = imagesx($img);
    $b_btm = $h1 = $h2 = imagesy($img);
    
    do {
        //top
        for(; $b_top < $h1; ++$b_top) {
            for($x = 0; $x < $w1; ++$x) {
                if(imagecolorat($img, $x, $b_top) != $hex) {
                    break 2;
                }
            }
        }
    
        // stop if all pixels are trimmed
        if ($b_top == $b_btm) {
            $b_top = 0;
            $code = 2;
            break 1;
        }
    
        // bottom
        for(; $b_btm >= 0; --$b_btm) {
            for($x = 0; $x < $w1; ++$x) {
                if(imagecolorat($img, $x, $b_btm-1) != $hex) {
                    break 2;
                }
            }
        }
    
        // left
        for(; $b_lft < $w1; ++$b_lft) {
            for($y = $b_top; $y <= $b_btm; ++$y) {
                if(imagecolorat($img, $b_lft, $y) != $hex) {
                    break 2;
                }
            }
        }
    
        // right
        for(; $b_rt >= 0; --$b_rt) {
            for($y = $b_top; $y <= $b_btm; ++$y) {
                if(imagecolorat($img, $b_rt-1, $y) != $hex) {
                    break 2;
                }
            }
    
        }
    
        $w2 = $b_rt - $b_lft;
        $h2 = $b_btm - $b_top;
        $code = ($w2 < $w1 || $h2 < $h1) ? 1 : 0;
    } while (0);
    
    // result codes:
    // 0 = Trim Zero Pixels
    // 1 = Trim Some Pixels
    // 2 = Trim All Pixels
    return array(
        '#'     => $code,   // result code
        'l'     => $b_lft,  // left
        't'     => $b_top,  // top
        'r'     => $b_rt,   // right
        'b'     => $b_btm,  // bottom
        'w'     => $w2,     // new width
        'h'     => $h2,     // new height
        'w1'    => $w1,     // original width
        'h1'    => $h1,     // original height
    );
    }
    

提交回复
热议问题