php gd image png watermark on jpg file produces weird result

364

Trying to add a transparent PNG watermark to jpg images get's me different results either if i'm on localhost or production server.

Here's the results...

On my localhost (PHP Version 5.6.24) enter image description here

On production server (PHP Version 5.5.9-1ubuntu4.21) enter image description here

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 enter image description here

What am I missing? this is driving me insane!

399

Answer

Solution:

I'm having the same issue. The imageScale function in Ubuntu seems to turn greens and blues to pink. Solution could be to use imagecopyresampled and imagecreatetruecolor. However this solution is way slower.

People are also looking for solutions to the problem: php - Codeigniter - How to prevent user from accessing data by entering random ID or Code

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.