dongque5529 2013-01-14 01:58
浏览 61
已采纳

PHP显示图像(SSL PROXY)

I am trying to make a PHP script that accepts a URL and displays it (trying to keep SSL on my project)

However, I am unable to get anything to output with my script. No errors are being displayed and I am at a loss of words. What am I not seeing?

<?php

//if parameter is empty
if((!isset($_GET['img'])) or ($_GET['img'] == '') or (!isset($per['scheme'])))
{
    exit;
}

$per = parse_url($_GET['img']);
print_r ($per);
$imgurl = $_GET['img'];
print_r ($imgurl);
$imgurl = str_replace(' ', "%20", $imgurl);

$aFile = getimagesize($imgurl);
print_r ($aFile);

//check file extension
if($aFile == 'jpg' or $aFile == 'jpeg'){
        header('Content-Type: image/jpeg');
        imagejpeg(getimg($file));
} elseif($aFile == 'png') {
        header('Content-Type: image/png');
        imagepng(getimg($file));
} elseif($aFile == 'gif') {
        header('Content-Type: image/gif');
        imagegif(getimg($file));
} else {
        die('not supported');
}

$imgurl = $_GET['img'];
$imgurl = str_replace(' ', "%20", $imgurl);

function getimg($imageurl) {
    $cache_expire = 60*60*24*365;
    $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg, image/png';
    $headers[] = 'Cache-Control: maxage=. $cache_expire';
    $headers[] = 'Pragma: public';
    $headers[] = 'Accept-Encoding: None';
    $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
    $user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6';
    $process = curl_init($imageurl);
    curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($process, CURLOPT_REFERER, $_GET['img']);
    curl_setopt($process, CURLOPT_HEADER, 0);
    curl_setopt($process, CURLOPT_HTTPGET, true);
    curl_setopt($process, CURLOPT_USERAGENT, $user_agent);
    curl_setopt($process, CURLOPT_TIMEOUT, 30);
    curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
    $return = curl_exec($process);
    curl_close($process);
    return $return;
}
exit;
?>

Here is an edited version of the script that outputs garbled text when I use readfile:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

//if parameter is empty
if((!isset($_GET['img'])) or ($_GET['img'] == ''))
{
    exit;
}

$per = parse_url($_GET['img']);
$imgurl = $_GET['img'];
$imgurl = str_replace(' ', "%20", $imgurl);

$aFile = getimagesize($imgurl);

//check file extension
$checkmime = getimagesize($imgurl);

if($checkmime['mime'] != 'image/png' && $checkmime['mime'] != 'image/gif' && $checkmime['mime'] != 'image/jpeg') {
    die('not supported');
} else {
    readfile("$imgurl");
}

function getimg($imageurl) {
    $cache_expire = 60*60*24*365;
    $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg, image/png';
    $headers[] = 'Cache-Control: maxage=. $cache_expire';
    $headers[] = 'Pragma: public';
    $headers[] = 'Accept-Encoding: None';
    $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
    $user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6';
    $process = curl_init($imageurl);
    curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($process, CURLOPT_REFERER, $_GET['img']);
    curl_setopt($process, CURLOPT_HEADER, 0);
    curl_setopt($process, CURLOPT_HTTPGET, true);
    curl_setopt($process, CURLOPT_USERAGENT, $user_agent);
    curl_setopt($process, CURLOPT_TIMEOUT, 30);
    curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
    $return = curl_exec($process);
    curl_close($process);
    return $return;
}
exit;
?>
  • 写回答

1条回答 默认 最新

  • douxuan3095 2013-01-14 02:11
    关注

    If this truly is your entire script your problem first lies with $per['scheme'] not being set so it will trip the first if() statement and have the script exit.

    You check if it's not set here: !isset($per['scheme']) so it will make the expression TRUE and thus end the script, reason no output even from print_r();

    Use this instead:

    if (empty($_GET['img'])) // Check if $_GET['img'] is not set AND is = '' (empty)
    {
        exit;
    }
    

    Use:

    } else {
        header('Content-Type: ' . $checkmime['mime']);
        readfile($imgurl);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题