简体   繁体   English

jpg文件上的php gd image png watermark会产生奇怪的结果

[英]php gd image png watermark on jpg file produces weird result

Trying to add a transparent PNG watermark to jpg images get's me different results either if i'm on localhost or production server. 试图为jpg图像添加透明的PNG水印,如果我在localhost或生产服务器上,我会得到不同的结果。

Here's the results... 这是结果......

On my localhost (PHP Version 5.6.24) 在我的localhost上(PHP版本5.6.24) 在此输入图像描述

On production server (PHP Version 5.5.9-1ubuntu4.21) 在生产服务器上(PHP版本5.5.9-1ubuntu4.21) 在此输入图像描述

I don't even know what to call this problem of the blue color turning pink and the logo doesn't get correctly rendered... 我甚至不知道该怎么称为蓝色变粉红色这个问题并且徽标没有正确呈现...

The code is this: 代码是这样的:

function getBrightness($gdHandle) {
$width = imagesx($gdHandle);
$height = imagesy($gdHandle);

$totalBrightness = 0;

for ($x = 0; $x < $width; $x++) {
    for ($y = 0; $y < $height; $y++) {
        $rgb = imagecolorat($gdHandle, $x, $y);

        $red = ($rgb >> 16) & 0xFF;
        $green = ($rgb >> 8) & 0xFF;
        $blue = $rgb & 0xFF;

        $totalBrightness += (max($red, $green, $blue) + min($red, $green, $blue)) / 2;
    }
}

  //  imagedestroy($gdHandle);

return ($totalBrightness / ($width * $height)) / 2.55;
}

function render_text_on_gd_image(&$source_gd_image, $text, $font, $size, $color, $opacity, $rotation, $bpx, $bpy, $wnewx) {

$source_width = imagesx($source_gd_image);
$source_height = imagesy($source_gd_image);
$bb = imagettfbbox($size, $rotation, $font, $text);
$x0 = min($bb[0], $bb[2], $bb[4], $bb[6]);
$x1 = max($bb[0], $bb[2], $bb[4], $bb[6]);
$y0 = min($bb[1], $bb[3], $bb[5], $bb[7]);
$y1 = max($bb[1], $bb[3], $bb[5], $bb[7]);
$bb_width = abs($x1 - $x0);
$bb_height = abs($y1 - $y0);

$alpha_color = imagecolorallocatealpha(
    $source_gd_image,
    hexdec(substr($color, 0, 2)),
    hexdec(substr($color, 2, 2)),
    hexdec(substr($color, 4, 2)),
    127 * (100 - $opacity) / 100
);
return imagettftext($source_gd_image, $size, $rotation, $bpx + $wnewx/2 - $bb_width/2 , $bpy, $alpha_color, $font, $text);
}


$val = array();

//$val["displayName"] = "Administrador";
$val["displayName"] = "Afonso Ferreira Gomes";

$val["relFile"] = "imagem.jpg";

list($val["width_original"], $val["height_original"]) = getimagesize($val["relFile"]);

if ($val["width_original"] > $val["height_original"] && $val["width_original"] >= 1200) {

    $val["height_resized"] = $val["height_original"] * (1200/$val["width_original"]);
    $val["width_resized"]  = 1200;

}  elseif ($val["height_original"] > $val["width_original"] && $val["height_original"] >= 1200) {

    $val["height_resized"] = 1200;
    $val["width_resized"]  = (1200/$val["height_original"]) * $val["width_original"];

} else {

    $val["height_resized"] = $val["height_original"];
    $val["width_resized"]  = $val["width_original"];

}

$i = imagecreatefromjpeg($val["relFile"]);

$i = imagescale($i, $val["width_resized"], $val["height_resized"]);

$val["width_new"] = imagesx($i);

$val["height_new"] = imagesy($i);


if ($val["width_new"] < $val["height_new"]) {

    $val["portait"] = true;
    $val["smallest"] = $val["width_new"];

} else {

    $val["portait"] = false;
    $val["smallest"] = $val["height_new"];

}


/* GERAR WATERMARK LOGOTIPO */
$val["racio"] = 0.20;
$val["marLeft"] = 40;
$val["marBottom"] = 40;
$val["bright"] = getBrightness($i);


if ($val["bright"] < 30) {

    $val["opacidade"] = 20;

} else {

    $val["opacidade"] = 40;

}

$w = imagecreatefrompng("jbw_260_40.png");

if ($val["portait"]) {

    $val["water_width"] = $val["width_new"] * $val["racio"];
    $val["water_height"] = $val["width_new"] * $val["racio"];

} else {

    $val["water_width"] = $val["height_new"] * $val["racio"];
    $val["water_height"] = $val["height_new"] * $val["racio"];

}

$wnew = imagescale($w, $val["water_width"], $val["water_height"]);

$val["water_width"] = imagesx($wnew);
$val["water_height"] = imagesy($wnew);

/* GERAR WATERMARK TEXTO AUTOR */
if ($val["displayName"] == "Administrador") {
    $val["autor"] = "";
    $val["marBottom"] = 20;
    $val["marLeft"] = 20;
} else {
    $val["autor"] = $val["displayName"];
}


// PRIMEIRO E ULTIMO NOME SÓ!!
$autorNomes = explode(" ", $val["autor"]);

if (count($autorNomes) >= 2) {
    $val["autor"] = $autorNomes[0] . " " . $autorNomes[count($autorNomes) - 1];
} else {
    $val["autor"] = $autorNomes[0];
}

$font = "../font/Montserrat.ttf";
$color = "FFFFFF";

$flagSize = false;

$val["fontSize"] = 12;

for ($size = 7; $size <= 50; $size += 0.5) {

    $bb = imagettfbbox($size, 0, $font, $val["autor"]);
    $x0 = min($bb[0], $bb[2], $bb[4], $bb[6]);
    $x1 = max($bb[0], $bb[2], $bb[4], $bb[6]);
    $y0 = min($bb[1], $bb[3], $bb[5], $bb[7]);
    $y1 = max($bb[1], $bb[3], $bb[5], $bb[7]);

    $val["bbox_width"] = abs($x1 - $x0);
    $val["bbox_height"] = abs($y1 - $y0);

    if (($val["bbox_width"] >= $val["water_width"] && !$flagSize) || $size == 50) {
        $val["fontSize"] = $size;
        $flagSize = true;
        break;
    }
}

$val["posY_water"] = $val["height_new"] - $val["water_height"] - $val["marBottom"];
$val["posY_text"] = $val["height_new"] - $val["marBottom"] + $val["bbox_height"] - 5;

imagecopy($i, $wnew, $val["marLeft"],  $val["posY_water"] , 0, 0, $val["water_width"], $val["water_height"]);

render_text_on_gd_image($i, $val["autor"], $font, $val["fontSize"], $color, $val["opacidade"] ,0 , $val["marLeft"]  , $val["posY_text"], $val["water_width"]);


// echo "<pre>"; print_r($val); echo "</pre>"; die;

/* OUTPUT IMAGEM E LIMPA BUFFER */
header('Content-type: image/png');

imagepng($i);
imagedestroy($i);
imagedestroy($w);

And this are the gd section of phpinfo for localhost and production server 这是localhost和生产服务器的phpinfo的gd部分 在此输入图像描述

What am I missing? 我错过了什么? this is driving me insane! 这让我疯了!

Those issues almost always object to the alpha channel of either the source or destination not beeing preserved or the output not really beeing true color. 这些问题几乎总是反对源或目标的alpha通道,而不是保留或输出不是真正的颜色。

Try using the imagesavealpha() function and search for "alpha" in the comments of the PHP documentation because there are many people having solutions to similiar issues. 尝试使用imagesavealpha()函数并在PHP文档的注释中搜索"alpha" ,因为有很多人都有类似问题的解决方案。

This comment might be helpful and is kinda what Esko said: 这条评论可能会有所帮助,有点像Esko所说:

http://php.net/manual/en/function.imagecreatefrompng.php#90364 http://php.net/manual/en/function.imagecreatefrompng.php#90364

Basically it copies an image to a buffer using imagecreatetruecolor() . 基本上它使用imagecreatetruecolor()将图像复制到缓冲区。

I'm having the same issue. 我有同样的问题。 The imageScale function in Ubuntu seems to turn greens and blues to pink. Ubuntu中的imageScale函数似乎将绿色和蓝色变为粉红色。 Solution could be to use imagecopyresampled and imagecreatetruecolor. 解决方案可以是使用imagecopyresampled和imagecreatetruecolor。 However this solution is way slower. 然而,这种解决方案更慢。

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

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