Solved

Maintain transparency of PNG image during upload

Posted on 2009-04-01
5
406 Views
Last Modified: 2012-06-22
I have a php scrip to upload PNG files but I am losing the transparency during the upload and it is giving the images by default a black BG. I am attaching a snippet of code I am using. I need to know how to modify the code to maintain the transparency of the images being uploaded. Thanks and any help will be appreciated.
if ($mimetype=="image/png") {
$i = imagecreatefrompng($temporary_name);
imagealphablending($i, true); // setting alpha blending on
imagesavealpha($i, true); // save alphablending setting (important)
$dest_x = $tx;
$dest_y = $ty;
if (imagesx($i) > $dest_x or imagesy($i) > $dest_y) {
if (imagesx($i) >= imagesy($i)) {
$thumb_x = $dest_x;
$thumb_y = imagesy($i)*($dest_x/imagesx($i));
} else {
$thumb_x = imagesx($i)*($dest_y/imagesy($i));
$thumb_y = $dest_y;
}
} else {
$thumb_x = imagesx($i);
$thumb_y = imagesy($i);
}
$thumb = imagecreatetruecolor($thumb_x,$thumb_y);
imagecopyresampled($thumb, $i ,0, 0, 0, 0, $thumb_x, $thumb_y, imagesx($i), imagesy($i));
imagejpeg($thumb, $upload."/".$name."_".$un.".png", 9);
chmod($upload."/".$name."_".$un.".png", 0777);
$image_name=$name."_".$un.".png";
}

Open in new window

0
Comment
Question by:sulentho
  • 2
5 Comments
 
LVL 5

Expert Comment

by:-null-
ID: 24036969
Hi

What browser are you using as <IE6 doesn't support transparent pngs and that might be throwing you off

-null-
0
 
LVL 5

Expert Comment

by:-null-
ID: 24036982
um and you're jpeging a png:

imagejpeg($thumb, $upload."/".$name."_".$un.".png", 9);


Jpegs don't do transparency
0
 
LVL 14

Accepted Solution

by:
shobinsun earned 400 total points
ID: 24037090
Hello,

Here is an example code to create .png image with transparent effect.

Hope this code will help you.

Regards.
<?php
header("Content-type: image/png");
$image="powered_by.png";//this image is uloaded from the form
$im = imagecreatefrompng($image);
$bg_color = imagecolorat($im,1,1);
imagecolortransparent($im, $bg_color);
imagepng($im,"pics.png");
//ImagePNG($pic,"pics45.png");
imagedestroy($im);
 
?>

Open in new window

0
 

Author Comment

by:sulentho
ID: 24037573
Hi Shobinsun,
I have attached the code that I am currently using but  what is happening is there is now a very slight whitish layer behind the image so it is actually not a 100% transparent. Can anyone let me know if there is anything I need to tweak in the code. I am attaching a file created from this script as an example.
function setTransparency($new_image,$image_source)
{
$transparencyIndex = imagecolortransparent($image_source);
$transparencyColor = array('red' => 255, 'green' => 255, 'blue' => 255);
if ($transparencyIndex >= 0) {
$transparencyColor    = imagecolorsforindex($image_source, $transparencyIndex);   
}
$transparencyIndex    = imagecolorallocate($new_image, $transparencyColor['red'], $transparencyColor['green'], $transparencyColor['blue']);
imagefill($new_image, 0, 0, $transparencyIndex);
imagecolortransparent($new_image, $transparencyIndex);
} 
 
function createUniqueImage($imagefile,$imagename,$tx,$ty,$upload) {
$token = md5(uniqid(rand(),1));
$un = substr($token, 0, 5);
$filename = $imagefile['name'];
$temporary_name = $imagefile['tmp_name'];
$mimetype = strtolower($imagefile['type']);
$split = explode(".", $filename);
$name=$imagename;
 
// .png file format
 
if ($mimetype=="image/png") {
$i = imagecreatefrompng($temporary_name);
$dest_x = $tx;
$dest_y = $ty;
if (imagesx($i) > $dest_x or imagesy($i) > $dest_y) {
if (imagesx($i) >= imagesy($i)) {
$thumb_x = $dest_x;
$thumb_y = imagesy($i)*($dest_x/imagesx($i));
} else {
$thumb_x = imagesx($i)*($dest_y/imagesy($i));
$thumb_y = $dest_y;
}
} else {
$thumb_x = imagesx($i);
$thumb_y = imagesy($i);
}
$thumb = imagecreatetruecolor($thumb_x,$thumb_y);
setTransparency($thumb,$i);
imagecopyresampled($thumb, $i ,0, 0, 0, 0, $thumb_x, $thumb_y, imagesx($i), imagesy($i));
imagepng($thumb, $upload."/".$name."_".$un.".png", 9);
chmod($upload."/".$name."_".$un.".png", 0777);
$image_name=$name."_".$un.".png";
}
}

Open in new window

fcvbweather-860b5.png
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Checking if varaible is empty 6 38
Blocking submission of a web-based form 6 20
php string detection problem 7 32
Decrypt string by php 7 27
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now