简体   繁体   中英

Adding PNG watermark in PNG image PHP

result: http://i.stack.imgur.com/p1kVz.png

I'm trying to copy PNG to another PNG but I have no idea why they return like this

    <?php // File and new size
$filename = 'watermark.png';

// Get new sizes
list($width, $height) = getimagesize($filename);

// Load

$resim = 'http://www.petrominpng.com.pg/images/logo_big.png';

$ext = end(explode('.', $resim));


if($ext == "jpeg" || $ext == "jpg")
{
$thumb = imagecreatefromjpeg($resim); 
}
else if($ext == "png")
{
$thumb = imagecreatefrompng($resim); 
}

$sx = imagesx($thumb);
$sy = imagesy($thumb);

if($sx >= $sy)
{
    $sxd = $sx/2;
    $degisim = $sxd/$width;
    /*
    echo $sxd." ".$width."  ";
    echo $sxd-$width." |";
    */
    $sxy = $height * $degisim;
    /*
    echo " $sxy $height | $degisim";
    exit();
    */
}
else
{
    $sxy = $sy/2;
    $degisim = $sxy/$height;
    /*
    echo $sxd." ".$width."  ";
    echo $sxd-$width." |";
    */
    $sxd = $width * $degisim;
    /*
    echo " $sxy $height | $degisim";
    exit();
    */
}

$source = imagecreatefrompng($filename);

// Resize
imagecopyresized($thumb, $source, $sx/5, $sy/4, 0, 0, $sxd, $sxy, $width, $height);

// Output
header('Content-type: image/png');
imagepng($thumb);
imagedestroy($thumb);


?>

You can see that, i have problem with images how can i make it right ?

my watermark

http://i.stack.imgur.com/TZFCa.png

Your code works fine, it appears that something is wrong with the base PNG image (not the watermark) because if you try the code with another png, it works fine, with a jpg, it also works fine.

It seems to be because the original PNG is a PNG8, because when converted to a PNG32 it works fine.

You may try this. It works fine in my project.

$stamp = imagecreatefrompng('watermark.png');
$im = imagecreatefrompng('source.png');

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 1;
$marge_bottom = 1;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

// OUTPUT IMAGE:
header("Content-Type: image/png");
imagesavealpha($im, true);
imagepng($im, NULL); 

The watermark image should be in one of the following recommended formats:

  • PNG-8 (recommended)
  • Colors: 256 or less
  • Transparency: On/Off

  • GIF

  • Colors: 256 or less
  • Transparency: On/Off

  • JPEG

  • Colors: True color
  • Transparency: n/a

The imagecopymerge function does not properly handle the PNG-24 images; it is therefore not recommended.

If you are using Adobe Photoshop to create watermark images, it is recommended that you use "Save for Web" command with the following settings:

File Format: PNG-8, non-interlaced

Color Reduction: Selective, 256 colors

Dithering: Diffusion, 88%

Transparency: On, Matte: None

Transparency Dither: Diffusion Transparency Dither, 100%

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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