简体   繁体   中英

PHP - getimagesize() redirect not working

Problem: getimagesize() does not work for some URLs, especially ones that are redirecting.

I googled around and checked stackoverflow but to no avail.

Here's what I see on my local machine:

var_dump(getimagesize('http://gan.doubleclick.net/gan_impression?lid=41000000015155731&pubid=21000000000506299&lsrc=17'));

> Array
(
    [0] => 120
    [1] => 90
    [2] => 2
    [3] => width="120" height="90"
    [bits] => 8
    [channels] => 3
    [mime] => image/jpeg
)

and on my server:

var_dump(getimagesize('http://gan.doubleclick.net/gan_impression?lid=41000000015155731&pubid=21000000000506299&lsrc=17'));

> bool(false)

I tried other images and URLs, and they work fine. It's this URL that is giving me a problem. I also tried the following (on my server), and this does work:

echo strlen(file_get_contents('http://gan.doubleclick.net/gan_impression?lid=41000000015155731&pubid=21000000000506299&lsrc=17'));

> 4829 // This number means it works

The error log has nothing, and there is no other hints I can tell. I'm guessing it's something that needs to change in the php.ini

Maybe allow_url_fopen is disabled in your PHP.ini file? You could edit your PHP.ini file or use cUrl instead.

Example:

<?php
//Download content
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, 'http://gan.doubleclick.net/gan_impression?lid=41000000015155731&pubid=21000000000506299&lsrc=17');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$file_contents = curl_exec($ch);
curl_close($ch);

file_put_contents('file', $file_contents); //Put content in ./file

var_dump(getimagesize('file')); //Get image size

unlink('file'); //Remove the file
?>

If file_get_contents works then definitely fopen would work

Curl would have been the best option since you are having permission issue but you can also use FastImage .. to read the image headers and get the information instead of having to save the whole file locally

Example

$img = new FastImage("http://gan.doubleclick.net/gan_impression?lid=41000000015155731&pubid=21000000000506299&lsrc=17");
var_dump($img->getSize(),$img->getType());

Output

array (size=2)
  0 => int 120
  1 => int 90
string 'jpeg' (length=4)

Simple Demo

hope it helps you. since there is 302 header on that link, unfortunately getimagesize dont recognize that header. you can fetch to you server to ensure getimagesize works!

    // fetch content to local
    $content = file_get_contents($file_path);
    $fetch_from_remote = file_put_contents($to_local_path, $content);

    if($fetch_from_flag === FALSE){
        // error
    }

    // defining vars to resize photo
    $file_info = getimagesize($to_local_path);

You have 2 options I can think of :

1) Get the file to your computer (use cURL or file_get_contents) and apply there any processing you want

2) Do a call for headers (cURL library), check if status = 200, if yes .. apply that function directly there, if other http status code then follow the redirect or ignore the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM