PHP bir URL'den saydam bir görüntü kaydetme

2 Cevap php

Ben bir yöntem bir kullanıcı görüntüleri uplaod edecek, birçok fotoğraf işlemlerini yapan bir sınıf oluşturmak için çalışıyorum ama ben de bir URL'den bir fotoğraf kapmak ve üzerinde diğer yöntemleri çalıştırmak için bir yöntem kurmak gerek duyuyorum sadece tarih ediliyordu eğer gibi kullanıcıdan gelen bir POST form ile.

Aşağıdaki URL'den görüntü almak için işlevi benim başlangıç, çalışıyor ama hala çalışmak gerekir. Kod Aşağıda bu fonksiyon varlık koştu sonucu olan bir resim görebilirsiniz. Ayrıca ne gibi görünmelidir görmek için orijinal görüntü. Bu işlev görüntü bu saydam görüntü üzerinde siyah bir arka plan var yapan görebilirsiniz. Onu nasıl görünmelidir gibi daha iyi görünmesi caqn?

$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

2 Cevap

Aşağıdaki iki aramalar png görüntüde mevcut karıştırma alfa kullanmak php söyleyecektir:

        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

Onların şeffaflık görüntü türleri ve uzantısı tarafından daha iyi desteği eklemek gerekir.

Görüntü saydam olduğundan biz biliyoruz ki onun bir GIF ya da PNG ya henüz sizin imagejpeg() kullanırken GIF başlığını gönderme - jpegler şeffaflık her türlü desteği yok. Ama eğer onun bir de onun alfa trans ya da dizin eğer şeffaflık için hesap olabilir png.