tags:

views:

35

answers:

2

I am trying to build a class that does many photo operations, one method will uplaod images from a user but I am also needing to build a method to grab a photo from a URL and run other methods on it just like if it were being uploaded with a POST form from user.

Below is my start of the function for getting image from URL, it works but needs work still. Below the code you can see a image that is the result of this function being ran. Also is the original image to see what it should look like. You can see that this function makes the image have a black background on this transparent image. How caqn I make it look better like it should look?

$url = 'http://a0.twimg.com/a/1262802780/images/twitter_logo_header.png';

//run our function
savePhotofromURL($url, 'no');



// photo function should grab an photo from a URL
function savePhotofromURL($photo_url, $saveimage = 'yes'){
    if(isset($photo_url) && $photo_url != '') {

        //get info about photo
        $photo_info = getimagesize($photo_url);
        $source_width = $photo_info['0'];
        $source_height = $photo_info['1'];
        $source_type = $photo_info['mime'];

        //grab the Photo from URL
        $photo = imagecreatefromstring(file_get_contents($photo_url));

        if (is_resource($photo) === true){
            if($saveimage === 'yes'){
                // TO DO: resize image and make the thumbs code would go here if we are saving image:
                // TO DO: resize source image if it is wider then 800 pixels
                // TO DO: make 1 thumbnail that is 150 pixels wide
            }else{
                // We are not saving the image show it in the user's browser
                // TO DO: we will add in correct photo type soon
                header('Content-Type: image/gif');
                imagejpeg($photo, null, 100);
                imagedestroy($photo); 
            }
        }else{
            // not a valid resource, show error
            echo 'error getting URL photo from ' .$photo_url;
        }
    }else{
        // url of image was empty
        echo 'The URL was not passed into our funtion';
    }
}

The result looks like this
alt text

Instead of like this alt text

+1  A: 

The following two calls will tell php to use the alpha blending present in the png image:

        ImageAlphaBlending($photo, false);
        ImageSaveAlpha($photo, true);

Edit: I see you're outputting the image as a JPEG also. JPEGs don't support transparency, so no matter what you do you will end up with an incorrect background color. Also see this related question: http://stackoverflow.com/questions/1025228/php-gd-imagesavealpha-and-imagealphablending

SoapBox
the jpg output was just me playing around, I will make it do different stuff based on the image type, for a png image I will output or save as a png image. I tried ImageAlphaBlending($photo, false); and ImageSaveAlpha($photo, true); but it doesn't seem to change anything even when output is PNG
jasondavis
Got it working now, had to change a few more things, thanks
jasondavis
A: 

You need to add better support for image types and by extension their transparency.

Since the image is transparent we can know that its either a GIF or a PNG yet your sending the GIF header while using imagejpeg() - jpegs dont support any kind of transparency. But if its a png you may also have to account for if its alpha trans or index transparency.

prodigitalson
yes I plan on adding that in, I just added the ability to get the image type before I posted this so I didnt add in code to use that data yet
jasondavis