I am trying to get a image from a URL, and then save it to the server. It is a simple task, yeah, but this image has transparency... The thing is, when i save that file to my PC or when I try to get and save it via PHP to my server, then I get the same result: the image gets all messed up with its transparency set to black. The URL of the image is:

http://cellufun.com/p/avatarimage/agb.aspx?i=Body|M04001|Shirt|M10020|

Another weird thing is my browser says the image is MIME Type image/png but I can't use imagecreatefrompng(); because I get the "image not a valid PNG" error...

Here is my code (I did try other solutions, this is what I tryed the last time):

<?php
$result=file_get_contents("http://cellufun.com/p/avatarimage/agb.aspx?i=Body|F04001|Shirt|F10020|Pants|MO55|");
$img = imageCreateFromstring($result);
imageAlphaBlending($img, true);
imageSaveAlpha($img, true);
imagecolortransparent($img);


header("Content-type: image/png");
imagepng($img);
?>

Oh, and I just tryed this image against the copy() function, and still get the same result... It looks like the image is smaller in size then the original image... Just tryed this:

file_put_contents("files/test.png",file_get_contents($result));

and still not working... so it has something to do with the image itself, because whatever i try to get the data, it does not work.

有帮助吗?

解决方案

Try this code (changes at lines 2-4):

<?php
$img = imageCreateFromgif("http://cellufun.com/p/avatarimage/agb.aspx?i=Body|M04001|Shirt|M10020|");
$index = imagecolorexact($img, 0, 0, 0); 
imagecolortransparent($img, $index); 

header("Content-type: image/png");
imagepng($img);
?>

It works for me although it's a little trick ;) ..and I don't know exactly why the quality is worse..

PS: The main problem I see is that the image at the url you provided is not really a png.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top