PHP - getimagesize() redirect not working

点点圈 提交于 2019-12-06 13:25:30

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

Dillen Meijboom

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
?>

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!