lilgezuz Posted February 26, 2012 Share Posted February 26, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/257805-php-gd-image-transparency/ Share on other sites More sharing options...
joel24 Posted February 26, 2012 Share Posted February 26, 2012 http://php.net/manual/en/function.imagecolortransparent.php Quote Link to comment https://forums.phpfreaks.com/topic/257805-php-gd-image-transparency/#findComment-1321360 Share on other sites More sharing options...
lilgezuz Posted February 27, 2012 Author Share Posted February 27, 2012 http://php.net/manual/en/function.imagecolortransparent.php Thats to define a color as a transparent I want to preserve the transparency of the orginal image Quote Link to comment https://forums.phpfreaks.com/topic/257805-php-gd-image-transparency/#findComment-1321547 Share on other sites More sharing options...
joel24 Posted February 27, 2012 Share Posted February 27, 2012 //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); Quote Link to comment https://forums.phpfreaks.com/topic/257805-php-gd-image-transparency/#findComment-1321551 Share on other sites More sharing options...
lilgezuz Posted February 27, 2012 Author Share Posted February 27, 2012 Thats not working for me either, I'm not sure how to make it work with my exisiting code. I tried using it on the $tmp and $image1 and still came out with a black background Quote Link to comment https://forums.phpfreaks.com/topic/257805-php-gd-image-transparency/#findComment-1321597 Share on other sites More sharing options...
lilgezuz Posted February 27, 2012 Author Share Posted February 27, 2012 So could anyone help me with this? Quote Link to comment https://forums.phpfreaks.com/topic/257805-php-gd-image-transparency/#findComment-1321784 Share on other sites More sharing options...
kicken Posted February 27, 2012 Share Posted February 27, 2012 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); Quote Link to comment https://forums.phpfreaks.com/topic/257805-php-gd-image-transparency/#findComment-1321817 Share on other sites More sharing options...
lilgezuz Posted February 29, 2012 Author Share Posted February 29, 2012 still coming out black, this stuff is confusing Quote Link to comment https://forums.phpfreaks.com/topic/257805-php-gd-image-transparency/#findComment-1322170 Share on other sites More sharing options...
kicken Posted February 29, 2012 Share Posted February 29, 2012 If you post some sample images your working with we could help you better by having something to test with. Quote Link to comment https://forums.phpfreaks.com/topic/257805-php-gd-image-transparency/#findComment-1322187 Share on other sites More sharing options...
joel24 Posted February 29, 2012 Share Posted February 29, 2012 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]); Quote Link to comment https://forums.phpfreaks.com/topic/257805-php-gd-image-transparency/#findComment-1322214 Share on other sites More sharing options...
lilgezuz Posted March 1, 2012 Author Share Posted March 1, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/257805-php-gd-image-transparency/#findComment-1322584 Share on other sites More sharing options...
lilgezuz Posted March 2, 2012 Author Share Posted March 2, 2012 Anybody have any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/257805-php-gd-image-transparency/#findComment-1322953 Share on other sites More sharing options...
kicken Posted March 2, 2012 Share Posted March 2, 2012 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; Quote Link to comment https://forums.phpfreaks.com/topic/257805-php-gd-image-transparency/#findComment-1322968 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.