PHP: Change Image Opacity

Here is a script which will change the image opacity…

Try this code and have fun..
$imageName = 'path/to/your/image/file'
$im_src = create_image_from_type($imageName);
$size = getimagesize($imageName);
$im_dst = create_image_from_type($imageName);
$white = imagecolorallocate($im_dst, 255, 255, 255);
imagecolortransparent($im_dst, $white);
imagefilledrectangle($im_dst, 0, 0, $size[0], $size[1], $white);
$opacityVal = 50;// put the opacity value here
imagecopymerge($im_dst, $im_src, 0, 0, 0, 0,$size[0], $size[1], $opacityVal);
save_image($im_dst, $imageName, 100);
imagedestroy($im_dst);
imagedestroy($im_src);

function save_image($image_dest,$image_src,$image_quality)
{
$image_type = getimagesize($image_src);
if($image_type['mime']=='image/png')
{
imagepng($image_dest,$image_src);
}
else
{
imagejpeg($image_dest,$image_src,$image_quality);
}
return true;
}

function create_image_from_type($image_path)
{
$image_type = getimagesize($image_path);
if($image_type['mime']=='image/png')
{
$im = imagecreatefrompng($image_path);
}
else
{
$im = imagecreatefromjpeg($image_path);
}
return $im;
}

Advertisements
About

Work is fun!

Tagged with: , , ,
Posted in PHP
One comment on “PHP: Change Image Opacity
  1. […] I had to find a way to put an opacity onto an image and finally found a script on how to do it on Tareq Alam’s Blog. The client wanted me to scale up a selected image to a specific size, grayscale it, and then […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Disclaimer
All content provided on this "tareqalam.com" blog is for informational purposes only. The owner of this blog makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site. The owner of [tareqalam.wordpress.com] will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of this information.
June 2008
M T W T F S S
« May   Jul »
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 23 other followers

%d bloggers like this: