Jump to content

php gd alpha transparency problems,. anyhelp,.?


somerfeld

Recommended Posts

The purpose of the script is to re size a image and watermark it with another overlayed image which has also been resized to the same parimiters.

I seem to be able to resize the images and overlay them,.
however the watermark is not preserving the alpha layer.,..
Ive tried a few different items and have continously have had no resolve
what is wrong with the following script.? thank you!

Links to watermark 24bit png with transparency perserved file
http://gateway-productions.com/v6/media/gallery/watermark.png

Link to the current output file
http://gateway-productions.com/v6/media/gallery/error.jpg

Link to what this function should be outputting
http://gateway-productions.com/v6/media/gallery/correct.jpg

[code]
<?
//declare varibles
$base = '/home/djgatewa/public_html/v6/media/gallery/';
If (empty($type)){ $type='ws';  $root=$base.$page.'/'; $size=150; } else{ $root= $base.$cat.'/'; $size=600; }

function makeimagefile($root,$image,$page,$size,$type,$base){

//load images
    $wm = imagecreatefrompng($base.'watermark.png');
    $im = imagecreatefromjpeg($root.$image);

//get sizes
    $sz=$size;
    $im_width=imageSX($im);
    $im_height=imageSY($im);
    $wm_width=imageSX($wm);
    $wm_height=imageSY($wm);

//perserve alpha of watermark
  imageAlphaBlending($wm, false);
  imageSaveAlpha($wm, true);

// work out new sizes
if($im_width >= $im_height)
{
    $factor = $sz/$im_width;
    $im_new_width = $sz;
    $im_new_height = $im_height * $factor;
    $wfactor = $sz/$wm_width;
    $wm_new_width = $sz;
    $wm_new_height = $wm_height * $wfactor;
}
else
{
    $factor = $sz/$im_height;
    $im_new_height = $sz;
    $im_new_width = $im_width * $factor;
    $wfactor = $sz/$wm_height;
    $wm_new_height = $sz;
    $wm_new_width = $wm_width * $wfactor;
}

//find bottom middle placement for watermark
      $dest_x = ( ( $im_new_width - $wm_new_width ) / 2 );
      $dest_y = $im_new_height- $wm_new_height ;

//now make new image size is memory
$new_wm=ImageCreateTrueColor($wm_new_width,$wm_new_height);
$new_im=ImageCreateTrueColor($im_new_width,$im_new_height);

//now copy image into new image in memory with images new sizes
ImageCopyResized($new_im,$im,0,0,0,0,$im_new_width,$im_new_height,$im_width,$im_height);
ImageCopyResized($new_wm,$wm,0,0,0,0,$wm_new_width,$wm_new_height,$wm_width,$wm_height);

//merge new image with new watermark at middle bottom with 80% transparency
ImageCopyMerge ($new_im,$new_wm,$dest_x,$dest_y,0,0,$wm_new_width,$wm_new_height,80);

//output and save final image
Imagejpeg($new_im,$root.$type.'/'.$type.'_'.$image,75); // quality 75;

//cleanup memory
ImageDestroy($im);
ImageDestroy($wm);
ImageDestroy($new_im);
ImageDestroy($new_wm);
}

?>[/code]
I haven't read your script, however, I have seen this problem before. The best thing I can suggest is to resize your original image to match (or as close as you can get) your watermark image. Apply the watermark and then resize again (if necessary).

Why? Who knows. All I know is GD doesn't seem to like resizing (or rotating) an image with alpha.

hope that helps.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.