简体   繁体   English

许多图像/ GD问题:裁剪,设置颜色和透明度

[英]A number of Image/GD Problems: cropping, setting color and transparency

I wrote some PHP code that uses GD on a Ubuntu machine (PHP 5.5.9) and just moved it over to a amazon EC2 with Amazon Linux (PHP 5.5.31) and I'm getting quite different results from the code from the same input data file (I'm only using imagecreatefromstring() ; $data contains the contents of a JPEG file.) 我编写了一些在Ubuntu机器上使用GD的PHP代码(PHP 5.5.9),并将其移植到亚马逊Linux(PHP 5.5.31)的亚马逊EC2上,我从相同的代码中得到了完全不同的结果输入数据文件(我只使用imagecreatefromstring() ; $data包含JPEG文件的内容。)

  1. Before I changed machines it would change the white to transparent. 在我更换机器之前,它会将白色变为透明。 It no longer makes that change. 它不再做出这种改变。
  2. It used to crop the graphic perfectly. 它曾用于完美地裁剪图形。 Now it leaves a little white on the left and cuts off the image by a little bit on the right. 现在它在左边留下一点白色,并在右边略微切断图像。
  3. There is a thin black line on the left side. 左侧有一条细黑线。
  4. I can't even seem to make the image change a color with imagecolorset() . 我甚至无法使用imagecolorset()使图像改变颜色。

Here's the code: 这是代码:

if ($isFileString) {
    $src2 = imagecreatefromstring($data);
} else {
    $src2 = imagecreatefromjpeg($data);
}

// This was an attempt to get it to recognize transparent.
if (!unlink ("../drive/sigs/tmp.png"))
    die("Failed to delete tmp.png");

imagepng($src2, "../drive/sigs/tmp.png");
imagedestroy($src2);
$src = imagecreatefrompng("../drive/sigs/tmp.png");

imagealphablending($src, false);
imagesavealpha($src, true);

for ($i=0; $i< 1024; $i++) {
    echo $i;
    echo print_r(imagecolorsforindex($src, $i));
    imagecolorset($src, $i, 255, 255, 255,255);
    echo print_r(imagecolorsforindex($src, $i));
    echo "<BR>";
}

$src = imagecropauto($src, IMG_CROP_WHITE);
$white = imagecolorallocate($src, 255, 255, 255);
imagecolortransparent($src, $white);
$src = imagerotate($src, -90, 0);

EDIT As an example of my not being able to get imagecolorset() to make a change in the file, below is the output from the loop on $i : 编辑作为我无法让imagecolorset()在文件中进行更改的示例,下面是$i循环的输出:

1Array ( [red] => 0 [green] => 0 [blue] => 1 [alpha] => 0 ) 1Array ( [red] => 0 [green] => 0 [blue] => 1 [alpha] => 0 ) 1
2Array ( [red] => 0 [green] => 0 [blue] => 2 [alpha] => 0 ) 1Array ( [red] => 0 [green] => 0 [blue] => 2 [alpha] => 0 ) 1
3Array ( [red] => 0 [green] => 0 [blue] => 3 [alpha] => 0 ) 1Array ( [red] => 0 [green] => 0 [blue] => 3 [alpha] => 0 ) 1

EDIT2 EDIT2

A little more information: I checked the gd versions ( php -i | grep -i gd ). 更多信息:我检查了gd版本( php -i | grep -i gd )。 I am surprised to see the Ubuntu box with GD version 2.1.1-dev and the EC2 box with "bundled (2.1.0 compatible)". 我很惊讶地看到带有GD版本2.1.1-dev的Ubuntu盒子和带有“捆绑(2.1.0兼容)”的EC2盒子。 I'm new to GD so I'm more inclined to believe this is my fault, rather than Amazon providing a bad version of GD. 我是GD的新手,所以我更倾向于认为这是我的错,而不是亚马逊提供不良版本的GD。

EDIT3 EDIT3

  • It doesn't appear to be a memory issue. 它似乎不是一个内存问题。 The memory_get_peak_usage() reports about 35MB. memory_get_peak_usage()报告大约35MB。

  • The only difference that gd_info() reports is the version number. gd_info()报告的唯一区别是版本号。

That version of GD seems to have some issues. 那个版本的GD似乎有一些问题。 It's not perfect, but here's the final code we settled on: 它并不完美,但这是我们确定的最终代码:

    if ($isFileString) {
        $im1 = imagecreatefromstring($data);
    } else {
        $im1 = imagecreatefromjpeg($data);
    }
    $tmp_file = $this->tmp_dir . md5(time() . mt_rand());
    imagepng($im1, $tmp_file);
    imagedestroy($im1);

    // rotate, pre-resize and resample the source image
    $im2 = imagecreatefrompng($tmp_file);
    unlink($tmp_file);
    if (imagesy($im2) > imagesx($im2))
        $im2 = imagerotate($im2, $this->rotate, 0);
    $width = imagesx($im2);
    $height = imagesy($im2);
    $percent = ($this->standard_width * 2) / $width;
    $newWidth = $width * $percent;
    $newHeight = $height * $percent;
    $im = imagecreatetruecolor($newWidth, $newHeight);
    imagecopyresampled($im, $im2, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);

    // create a transparent image with a size like $im image
    $im3 = imagecreatetruecolor($newWidth, $newHeight);
    imagesavealpha($im3, TRUE);
    imagefill($im3, 0, 0, imagecolorallocatealpha($im3, 0, 0, 0, 127));

    // find non-white-ish pixels on $im and copy them to $dst
    $sizeX = $newWidth;
    $sizeY = $newHeight;
    $startX = $startY = 100000;
    $finishX = $finishY = 0;
    for ($x = 1; $x < $sizeX - 1; $x++) {
        for ($y = 1; $y < $sizeY - 1; $y++) {
            $rgb = imagecolorat($im, $x, $y);
            $colors = imagecolorsforindex($im, $rgb);
            $r = $colors['red'];
            $g = $colors['green'];
            $b = $colors['blue'];
            if ($r < $this->redLimit && $g < $this->greenLimit && $b < $this->blueLimit) {
                imagesetpixel($im3, $x, $y, $rgb);
                if ($startX > $x) {
                    $startX = $x;
                }
                if ($startY > $y) {
                    $startY = $y;
                }
                if ($finishX < $x)
                    $finishX = $x;
                if ($finishY < $y)
                    $finishY = $y;
            }
        }
    }

    // final resize
    $width = $finishX - $startX + 1;
    $height = $finishY - $startY + 1;
    $percent = $this->standard_width / $width;
    $newWidth = $width * $percent;
    $newHeight = $height * $percent;
    $im4 = imagecreatetruecolor($newWidth, $newHeight);
    imagesavealpha($im4, TRUE);
    imagefill($im4, 0, 0, imagecolorallocatealpha($im4, 0, 0, 0, 127));
    imagecopyresampled($im4, $im3, 0, 0, $startX, $startY, $newWidth, $newHeight, $width, $height);

    // returning image
    ob_start();
    imagepng($im4);
    $image_data = ob_get_contents();
    ob_end_clean();
    return $image_data;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM