PHP gd库裁剪白色背景到透明png

时间:2017-07-26 15:49:06

标签: php gd

我正在尝试使用图像和php进行一些工作。请查看以下脚本。

<?php

//testcall prepare.php?img=https://images-eu.ssl-images-amazon.com/images/I/515SENGgRnL.jpg&h=200&n=test.png

$file=$_GET["img"];
$height=$_GET["h"];
$name=$_GET["n"];



function transparent($picture) 
{
    $img_w = imagesx($picture);
    $img_h = imagesy($picture);

    $newPicture = imagecreatetruecolor( $img_w, $img_h );
    imagesavealpha( $newPicture, true );
    $rgb = imagecolorallocatealpha( $newPicture, 0, 0, 0, 127 );
    imagefill( $newPicture, 0, 0, $rgb );

    $color = imagecolorat( $picture, $img_w-1, 1);

    for( $x = 0; $x < $img_w; $x++ ) {
        for( $y = 0; $y < $img_h; $y++ ) {
            $c = imagecolorat( $picture, $x, $y );
            if($color!=$c){         
                imagesetpixel( $newPicture, $x, $y,    $c);             
            }           
        }
    }
    echo "habs transparent gemacht! <br> ";
    return $newPicture; 
}

function resize($img,$w,$name){
    $ratio = imagesx($img)/imagesy($img); 
    if( $ratio > 1) {
        $width = $w;
        $height = $w/$ratio;
    }
    else {
        $width = $w*$ratio;
        $height = $w;
    }
    $dst = imagecreatetruecolor($width,$height);
    imagesavealpha($dst, true);
    $color = imagecolorallocatealpha($dst, 0, 0, 0, 127);
    imagefill($dst, 0, 0, $color);
    imagecopyresampled($dst,$img,0,0,0,0,$width,$height,imagesx($img),imagesy($img));
    imagepng($dst, "test.png"); 

};

function crop($img){
    $img = imagecreatefromjpeg($img);
    $b_top = 0;
    $b_btm = 0;
    $b_lft = 0;
    $b_rt = 0;
    for(; $b_top < imagesy($img); ++$b_top) {
      for($x = 0; $x < imagesx($img); ++$x) {
        if(imagecolorat($img, $x, $b_top) != 0xFFFFFF) {
           break 2;
        }
      }
    }
    for(; $b_btm < imagesy($img); ++$b_btm) {
      for($x = 0; $x < imagesx($img); ++$x) {
        if(imagecolorat($img, $x, imagesy($img) - $b_btm-1) != 0xFFFFFF) {
           break 2;
        }
      }
    }
    for(; $b_lft < imagesx($img); ++$b_lft) {
      for($y = 0; $y < imagesy($img); ++$y) {
        if(imagecolorat($img, $b_lft, $y) != 0xFFFFFF) {
           break 2; 
        }
      }
    }
    for(; $b_rt < imagesx($img); ++$b_rt) {
      for($y = 0; $y < imagesy($img); ++$y) {
        if(imagecolorat($img, imagesx($img) - $b_rt-1, $y) != 0xFFFFFF) {
           break 2; 
        }
      }
    }
    $newimg = imagecreatetruecolor(imagesx($img)-($b_lft+$b_rt), imagesy($img)-($b_top+$b_btm));
    imagecopy($newimg, $img, 0, 0, $b_lft, $b_top, imagesx($newimg), imagesy($newimg));
    return $newimg;
};

$pic_cropped=crop($file);
$pic_transparent=transparent($pic_cropped);
$pic_resized=resize($pic_transparent,$height,$name);
?>

输入是jpg,高度和输出名称。第一个功能作物是四周的白色空间。然后将白色背景颜色设置为透明,并将整个图片的大小调整为高度。到目前为止,脚本在我的Mac上使用MAMP正常运行。我尝试在Rasberry pi和托管区域的Ubuntu Linux上运行。在我的Mac上是完美的:https://ibb.co/kdbCek

Raspberry和托管Linux上的输出很糟糕:{{3}}

希望你能看到差异。相同的PHP版本,相同的Apache版本。任何想法如何解决?

0 个答案:

没有答案