ConFoo Montreal 2026: Call for Papers

Voting

: max(two, two)?
(Example: nine)

The Note You're Voting On

chandrachur at elegantsystems dot net
14 years ago
Here is a fun code for making images with white bg transparent. Well it can work with any bg but you need to adjust the color values accordingly. Works best with plain dual colors. I searched for this kind of code a lot but unable to find one ...see if this helps someone.

<?php

function transparentImage($src){ //making images with white bg transparent
$r1=80;
$g1=80;
$b1=80;
for(
$x = 0; $x < imagesx($src); ++$x)
{
for(
$y = 0; $y < imagesy($src); ++$y)
{
$color=imagecolorat($src, $x, $y);
$r = ($color >> 16) & 0xFF;
$g = ($color >> 8) & 0xFF;
$b = $color & 0xFF;
for(
$i=0;$i<270;$i++){
if(
$r.$g.$b==($r1+$i).($g1+$i).($b1+$i)){
$trans_colour = imagecolorallocatealpha($src, 0, 0, 0, 127);
imagefill($src, $x, $y, $trans_colour);
}
}
}
}

return
$src;
}

$image='abc/abc.jpg';
$src = imagecreatefromjpeg($image);
$src=transparentImage($src); //Lets make the jpegs transparent

?>

<< Back to user notes page

To Top