PHP imagecreatefromstring ve file_get_contents ile yardım

2 Cevap php

Bana herhangi bir URL ikonların girin ve ardından bir kullanıcı benim sunucuda yüklüyordu sadece sanki üzerinde bazı fonksiyonları çalışır sağlayacak PHP bir işlev yapmaya çalışıyorum. SO Ben yeniden boyutlandırmak ve bazı küçük yapmak ama ben sadece bunu benim diğer kodları çalıştırabilirsiniz bir durumda görüntü elde yardıma ihtiyacım olacak. Bu sitede başka bir kullanıcı beni imagecreatefromstring (başlamanıza yardımcı) ve file_get_contents ()

Bu kod Ben sadece temel işlevi çalışma almak için çalışıyorum ve sonra tüm güvenlik önlemleri katacak, farkında olduğum bir sürü şey eksik unutmayın

I tried this code below using a URL like this with the photo URL added to my script url http://localhost/friendproject2/testing/photos/fromurl/?url=http://a0.twimg.com/a/1262802780/images/twitter_logo_header.png

But it shows nothing and not even an error

function getphotoURL($url){
    if(isset($url) && $url != 'bad') {
        $image = ImageCreateFromString(file_get_contents($url));
        if (is_resource($image) === true){
            echo 'The URL of the image we fetch is :' .$url. '<BR><BR>';
            //show image
            header('Content-Type: image/jpeg');
            imagejpeg($image, null, 100);
            imagedestroy($image); 
            imagedestroy($image); 
         	// image is valid, do your magic here
        }else{
            // not a valid image, show error
            echo 'error getting URL photo from ' .$url;
        }
    }else{
        //url was empty
        echo 'The URL was not passed into our funtion';
    }
}
?>






###### UPDATE #####

Bu, bir POST isteği yerine bir GET isteği için denetleme gibi basit bir şey benim parçası üzerinde basit bir hata burada oldu benim yeni kod aşağıda görünüyor.

Ben sorunların bir çift var,

1) Ben imagejpeg kullanıyorum ($ image, null, 100); ve ben, merak ediyorum ben başka bir şey kullanıyor olmalıdır? Bir jpg olması için kaynak görüntüyü gerektirmez ya da herhangi görüntü formatlı çalışacak? Ben izin gereken başlıca türleri (jpg, jpeg, gif, png)

Başlık: Söz yukarıda ama ben başlığı bu ayarladığınız ekrandaki görüntüyü gösteren zaman olduğu gibi 2) aynı ('Content-Type: image / jpeg'); bu görüntülerin diğer türü için jpg edilmemelidir?

3) bir görüntü değilse ben geçirilen kaynak URL gerçek bir görüntü olduğundan emin olun ve ne istersem yapabilirim bir yolu var mı, gibi kendi hatasını göstermek veya kendi kod yapmak o zamanlar URL geçerli bir resim url olmadığını tespit

<?PHP
// run our function
if(isset($_GET['url']) && $_GET['url'] != "") {
    getphotoURL($_GET['url'],'no');
}


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

2 Cevap

Yeni sorulara yanıt olarak:

1) I am using imagejpeg($image, null, 100); and I am wondering, should I be using something else? DOes it require the source image to be a jpg or will it work wiht any image? I need to allow the main types (jpg, jpeg, gif, png)

Well, php.net says, "imagejpeg() creates a JPEG file from the given image". But the important part is this, "An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().". And your using "imagecreatefromstring() returns an image identifier representing the image obtained from the given data . These types will be automatically detected if your build of PHP supports them: JPEG, PNG, GIF, WBMP, and GD2." So, that should be ok.

2) same as above question but for when showing the image on screen I have header set to this: header('Content-Type: image/jpeg'); should it not be jpg for other type of images?

Başlık türü jpg olmalıdır - bu dosya türü varsa, o zaman haklısınız.

3) Is there a way that I can make sure that the source URL passed in is an actual image and do whatever I want if it is not a image, like show my own error or do my own code once it detect that the URL is not a valid image url

Evet - Yerine yapıyor:

$image = ImageCreateFromString(file_get_contents($url));

Sen yapabilirsin:

$image = imagecreatefromjpeg($url);
if (!$image) echo "error"; 

imagecreatefromjpeg() returns an image identifier representing the image obtained from the given filename.

Ama gerçekten, ne var gayet iyi.


Bu hata iletisi görüntüler mu

    echo 'The URL was not passed into our funtion';

Ya da hiç bir şey?

Hata mesajlaşma görüntülenir ediliyor, olası onay === başarısız oluyor:

An image resource will be returned on success. FALSE is returned if the image type is unsupported, the data is not in a recognised format, or the image is corrupt and cannot be loaded.

Ayrıca, hata günlüğü, geliştirme sunucuda maxed var? Gördüğünüz Bu şekilde herhangi olası uyarıları atılıyor?

(Şeffaflık için), bir PNG veya GIF için imagecreatefromstring () çağrıldıktan sonra

Görüntü üzerinde aşağıdaki manipülasyonlar yapın:

imagealphablending($image, true); // setting alpha blending on
imagesavealpha($image, true);

Alfa kanalı için düz siyah bir arka plan dönecek.