Jump to content

Recommended Posts

OK... I have this as my code, it seems to work, but it doesn't make my PNG images have a transparent background, it just makes the background black.

In the last 9 lines is where the PNG portion is located
[code]<?php
// The file
session_start();
$img_id = $_GET['image_id'];
include"db.php";
$img = mysql_query("SELECT * FROM files where file_id='$img_id'")or die(mysql_query());
$imgs = mysql_fetch_array($img);
//$imgname = $_GET['image_id'];
$filename = "users/".$_SESSION['user']."/$imgs[file_name]";
#$filename = "users/ryan/screen2.jpg";
//$filename = 'images/user_images/geoff.jpg';


$percent = $_GET['percent'];
switch($percent){
case ".25";
$percent = .25;
break;
case ".50";
$percent = .50;
break;
case ".75";
$percent = .75;
break;
case "1";
$percent = 1;
break;
default:
$percent = .5;
break;
}
function getext($file) {
$pos = strrpos($file,'.');
$str = substr($file, $pos);
return strtolower($str);
}

// Content type
if(getext($filename)=='.jpg'){
header('Content-type: image/jpeg');
}elseif(getext($filename)=='.gif'){
header('Content-type: image/gif');
}elseif(getext($filename)=='.png'){
header('Content-type: image/png');
}

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);

if(getext($filename)=='.jpg'){
$source = imagecreatefromjpeg($filename);
}elseif(getext($filename)=='.gif'){
$source = imagecreatefromgif($filename);
}elseif(getext($filename)=='.png'){
$source = imagecreatefrompng($filename);
imageAlphaBlending($source, true);
imageSaveAlpha($source, true);
}
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb);
?>[/code]

So how do I make it transparent? If I do this:

[b]$source = imagecreatefrompng($filename);
$source = imageAlphaBlending($source, true);
$source = imageSaveAlpha($source, true);[/b]

It doesn't even work.
Link to comment
https://forums.phpfreaks.com/topic/31209-png-transparency/
Share on other sites

If it's a PNG...why the hell are you using [b]imagejpeg[/b]:

[code]// Output
imagejpeg($thumb);
?>[/code]

:o

[b][EDIT:][/b]

Wait...you're using imagejpeg for ALL your images? It won't work that way. You'll have to use imagejpeg for your JPEGs imagegif for your GIFs and imagepng for your PNGs.
Link to comment
https://forums.phpfreaks.com/topic/31209-png-transparency/#findComment-144302
Share on other sites

OK, I changed that line to this:
[code]
// Output
if(getext($filename)=='.jpg'){
imagejpeg($thumb);
}elseif(getext($filename)=='.gif'){
imagegif($thumb);
}elseif(getext($filename)=='.png'){
imagepng($thumb);
}[/code]

It still doesn't do anything useful, it is still non-transparent

I'm using FF 2 btw.

It worked just fine even with imgjpeg on gif, and png images.
Link to comment
https://forums.phpfreaks.com/topic/31209-png-transparency/#findComment-144304
Share on other sites

Hmmm...as for that...I don't know. Are you sending the proper headers?

i.e.,

[code]header("Content-type: image/" . (getext($filename) === ".jpg" ? "jpeg" : (getext($filename) === ".gif" ? "gif" : "png"))); // send the appropriate image header since you are printing the image's contents directly to the browser[/code]

Notice I've used === instead of == I'm not sure why but you should use it instead. Also just make sure you're sending the headers (since you're printing the raw image data to the screen)...either that or write it to a file first and print out the image from the file using HTML tags.

However it appears to me you're just trying to resize your images to make thumbnails. If this is the case, why not just set the WIDTH/HEIGHT attributes of your IMG tags? Unless you really want to just want/need a script that will make thumbnails of your images...
Link to comment
https://forums.phpfreaks.com/topic/31209-png-transparency/#findComment-144308
Share on other sites

Well that makes sense of some of it. Sorry if I seemed harsh before...I just thought you had written the entire code snippet which would have made it a rather stupid mistake. ;)

I've noticed that you are indeed sending the proper headers...my mistake for not taking the time to read over your code properly.

However I'm fairly certain that using == won't work properly with strings. At least I think I read that somewhere. In any case, try using === instead. If nothing else it will make for better coding practice.

Also, have you tried using [url=http://us2.php.net/manual/en/function.imagecopyresampled.php]imagecopyresampled[/url] instead? I don't know if it would make a difference, but it could be worth a try.

P.S. you can make links by putting [ url=http://link.com ]link.com[ /url ] (without the spaces) ;)
Link to comment
https://forums.phpfreaks.com/topic/31209-png-transparency/#findComment-144327
Share on other sites

the === thing didn't do anything. Any other suggestions?

Once again here is the code:
[code=php:0]
<?php
// The file
session_start();
$img_id = $_GET['image_id'];
include"db.php";
$img = mysql_query("SELECT * FROM files where file_id='$img_id'")or die(mysql_query());
$imgs = mysql_fetch_array($img);
//$imgname = $_GET['image_id'];
$filename = "users/".$_SESSION['user']."/$imgs[file_name]";
#$filename = "users/ryan/screen2.jpg";
//$filename = 'images/user_images/geoff.jpg';


$percent = $_GET['percent'];
switch($percent){
case ".25";
$percent = .25;
break;
case ".50";
$percent = .50;
break;
case ".75";
$percent = .75;
break;
case "1";
$percent = 1;
break;
default:
$percent = .5;
break;
}
function getext($file) {
$pos = strrpos($file,'.');
$str = substr($file, $pos);
return strtolower($str);
}

// Content type
if(getext($filename)=='.jpg'){
header('Content-type: image/jpeg');
}elseif(getext($filename)=='.gif'){
header('Content-type: image/gif');
}elseif(getext($filename)=='.png'){
header('Content-type: image/png');
}

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);

if(getext($filename)=='.jpg'){
$source = imagecreatefromjpeg($filename);
}elseif(getext($filename)=='.gif'){
$source = imagecreatefromgif($filename);
}elseif(getext($filename)=='.png'){
$source = imagecreatefrompng($filename);
imageAlphaBlending($source, true);
imageSaveAlpha($source, true);
}
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
if(getext($filename)=='.jpg'){
imagejpeg($thumb);
}elseif(getext($filename)=='.gif'){
imagegif($thumb);
}elseif(getext($filename)=='.png'){
imagepng($thumb);
}
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/31209-png-transparency/#findComment-144932
Share on other sites

Just to see what's going on...why not throw a die in there somewhere...

i.e.:

[code]if (getext($filename) === ".jpg") die("JPEG");
else if (getext($filename) === ".gif") die("GIF");
else if (getext($filename) === ".png") die("PNG");[/code]

I'm telling you...you really need to use triple equal signs here. You said it didn't make a difference, but you should still change it.
Link to comment
https://forums.phpfreaks.com/topic/31209-png-transparency/#findComment-144941
Share on other sites

This is my alt text in the img tag when I run yours:

The image “http://71.82.126.113:6251/tzfiles/resize.php?image_id=43” cannot be displayed, because it contains errors.

When I run mine, It outputs the image, only it has a black background instead of a transparent one.
Link to comment
https://forums.phpfreaks.com/topic/31209-png-transparency/#findComment-144956
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.