Jump to content

PHP GD Image transparency


lilgezuz

Recommended Posts

I have the below code that outputs my image correctly expect it puts a black background in it, instead of keeping it transparent.  I'm tried using imagealphablending and couldn't get it to work.  What should I use to get it to work correctly and where should I put it.

 

$src = imagecreatefrompng($target);
echo $scr;
list($width,$height)=getimagesize($target);
$newwidth=54; // new width of image
$newheight=54;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$image1 = imagecreatefrompng('Surround.png');
imagecopymerge($image1, $tmp, 5, 5, 0, 0, 54, 54, 100);

 

This is what my gd script output looks like, the black needs to be transparent

 

012b.png

Link to comment
Share on other sites

//wrote this script almost 2 years ago, hope it helps.
            $newImg = imagecreatetruecolor($newWidth, $newHeight);
            imagealphablending($newImg, false);
            imagesavealpha($newImg,true);
            $transparent = imagecolorallocatealpha($newImg, 0, 0, 0, 0);
            imagefilledrectangle($newImg, 0, 0, $newWidth, $newHeight, $transparent);
            imagecolortransparent($newImg, $transparent);

Link to comment
Share on other sites

Try something like this:

$src = imagecreatefrompng($target);
$width=imagesx($src);
$height=imagesy($src);

$newwidth=54; // new width of image
$newheight=54;

$tmp=imagecreatetruecolor($newwidth,$newheight);
imagealphablending($tmp, false);
imagesavealpha($tmp, true);
imagefill($tmp, 0, 0, imagecolorallocatealpha($tmp, 0, 0, 0, 127));
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

$image1 = imagecreatefrompng('Surround.png');
imagealphablending($image1, false);
imagesavealpha($image1, true);

imagecopymerge($image1, $tmp, 5, 5, 0, 0, 54, 54, 100);

 

Link to comment
Share on other sites

try this.

//edit forgot to add get image info & size.
$imgInfo = getimagesize($img);
$nWidth = 50;
$nHeight = 50;


$im = imagecreatefrompng($target);
$newImg=imagecreatetruecolor($newwidth,$newheight);
imagealphablending($newImg, false);
imagesavealpha($newImg,true);
$transparent = imagecolorallocatealpha($newImg, 0, 0, 0, 0);
$black = imagecolorallocate($newImg, 0, 0, 0);

imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent);
// Make the background transparent
imagecolortransparent($newImg, $transparent);

imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]);

Link to comment
Share on other sites

Here is some more of my code that I'm working with.

 

//$url = 'http://example.com/image.jpg';

$savedir = '/home/content/mypath/html/images/tmp_images'; //or anything else you want

$overwrite = true; //or false if image has to be renamed on duplicate

 

$urlinfo = parse_url($url);

$filename = basename($urlinfo['path']);

$target = $savedir.'/'.$filename;

if(file_exists($target) && !$overwrite){

//break up file in parts:

$pathinfo = pathinfo($target);

//max 50 tries

$max = 50;

//loop

for($i = 1;$i<=$max;$i++){

$target = $pathinfo['dirname']. '/' . $pathinfo['filename'] . '[' . $i

. '].' . $pathinfo['extention'];

//break on success, do not use file_exists to avoid race

$fh = @fopen($target,'x');

if($fh) break;

}

//alternatively, if you don't care about the name, you can just:

//$target = tempnam($savedir);

if(!$fh) die('Too many retries, no unique filename found.');

} else {

$fh = fopen($target,'w');

}

$check = fwrite($fh,file_get_contents($url));

fclose($fh);

// echo (($check) ? 'Successfully saved '.$target : 'Failure');

 

 

$src = imagecreatefrompng($target);

$width=imagesx($src);

$height=imagesy($src);

$newwidth=54; // new width of image

$newheight=54;

$tmp=imagecreatetruecolor($newwidth,$newheight);

imagealphablending($tmp, false);

imagesavealpha($tmp, true);

imagefill($tmp, 0, 0, imagecolorallocatealpha($tmp, 0, 0, 0, 127));

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

$image1 = imagecreatefrompng('Surround.png');

imagealphablending($image1, false);

imagesavealpha($image1, true);

imagecopymerge($image1, $tmp, 5, 5, 0, 0, 54, 54, 100);

 

if($type == 'PLATINUM'){ $image_name= 'images/games/'.$folder.'/0'.$id.'.png';}

if($type == 'GOLD'){ $image_name= 'images/games/'.$folder.'/0'.$id.'g.png';}

if($type == 'SILVER'){ $image_name= 'images/games/'.$folder.'/0'.$id.'s.png';}

if($type == 'BRONZE'){ $image_name= 'images/games/'.$folder.'/0'.$id.'b.png';}

imagepng($image1,constant('UPLOAD_DIR').$image_name, 9);

[code=php:0]

 

Attached images.  The trophy is the image I'm resizing to 54 x 54 then placing it in the center of the surrond.png file.  I think the problem is with the surrond.png or both images.  I dunno

post-130559-13482403272685_thumb.png

post-130559-13482403272742_thumb.png

Link to comment
Share on other sites

This code appears to work for me (viewable at http://linode.aoeex.com/badge.html)

<?php

function resizeImage($im, $newW, $newH){
$imnew = ImageCreateTrueColor($newW, $newH);

$transparent = imagecolorallocatealpha($imnew, 0, 0, 0, 127);
imagefill($imnew, 0, 0, $transparent);

ImageAlphaBlending($imnew, false);
ImageSaveAlpha($imnew, true);

imagecopyresampled($imnew, $im, 0, 0, 0, 0, $newW, $newH, imagesx($im), imagesy($im));
ImageDestroy($im);
return $imnew;
}

$borderImage = ImageCreateFromPNG('surround.png');
ImageAlphaBlending($borderImage, false);
ImageSaveAlpha($borderImage, true);

$trophyImage = ImageCreateFromPNG('trophy.png');

//Resize the trophy down
$trophyImage = resizeImage($trophyImage, 54, 54);


//Copy the trophy to the border
ImageCopy($borderImage, $trophyImage, 5, 5, 0, 0, 54, 54);
header('Content-type: image/png');
ImagePNG($borderImage);
exit;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.