【问题标题】:PHP image scaling does not workPHP图像缩放不起作用
【发布时间】:2012-08-27 22:14:32
【问题描述】:

“res.php”下面提供的脚本旨在缩放图像,然后在 html 页面的 HEADER css 中使用。根据“CID”(俱乐部 ID)调用图像。每个俱乐部都有存储在数据库中的信息,包括名为 club_header 的信息。 club_header 是一个 VARCHAR,用于存储图像的绝对位置(文件系统或 Web)。 “res.php”功能是调用图像并将其输出到浏览器以及将图像重新调整为任何设置的大小。默认设置为 960 x 200。

但是由于某种原因,脚本没有做它必须做的事情,而是输出了一个损坏的图像。代码可能有什么问题?

如果需要一些服务器 + php 信息

服务器: Apache v2.2.21、PHP v5.3.8、SQL v5.5.16、GD v2.0.34

PHP.ini: max_execution_time = 300,max_input_time = 60,memory_limit = 208M,post_max_size = 24M

“res.php”源代码

require("php/db.class.php");

function scaleImageFileToBlob($file) {

    $file = $_GET['cid'];

    $query = mysql_query("SELECT * FROM clubs WHERE club_id = '".$_GET['cid']."'");
    $obj = mysql_fetch_array($query);
    $photoObj = $obj['club_header'];

    $source_pic = $photoObj;
    $max_width = 960;
    $max_height = 200;

    list($width, $height, $image_type) = getimagesize($file);

    switch ($image_type)
    {
        case 1: $src = imagecreatefromgif($file); break;
        case 2: $src = imagecreatefromjpeg($file);  break;
        case 3: $src = imagecreatefrompng($file); break;
        default: return '';  break;
    }

    $x_ratio = $max_width / $width;
    $y_ratio = $max_height / $height;

    if( ($width <= $max_width) && ($height <= $max_height) ){
        $tn_width = $width;
        $tn_height = $height;
        }elseif (($x_ratio * $height) < $max_height){
            $tn_height = ceil($x_ratio * $height);
            $tn_width = $max_width;
        }else{
            $tn_width = ceil($y_ratio * $width);
            $tn_height = $max_height;
    }

    $tmp = imagecreatetruecolor($tn_width,$tn_height);

    /* Check if this image is PNG or GIF, then set if Transparent*/
    if(($image_type == 1) OR ($image_type==3))
    {
        imagealphablending($tmp, false);
        imagesavealpha($tmp,true);
        $transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
        imagefilledrectangle($tmp, 0, 0, $tn_width, $tn_height, $transparent);
    }
    imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);

    /*
     * imageXXX() only has two options, save as a file, or send to the browser.
     * It does not provide you the oppurtunity to manipulate the final GIF/JPG/PNG file stream
     * So I start the output buffering, use imageXXX() to output the data stream to the browser, 
     * get the contents of the stream, and use clean to silently discard the buffered contents.
     */
    ob_start();

    switch ($image_type)
    {
        case 1: imagegif($tmp); break;
        case 2: imagejpeg($tmp, NULL, 100);  break; // best quality
        case 3: imagepng($tmp, NULL, 0); break; // no compression
        default: echo ''; break;
    }

    $final_image = ob_get_contents();

    ob_end_clean();

    return $final_image;

}

echo scaleImageFileToBlob($file);

提前谢谢你。

【问题讨论】:

  • 您对 SQL 注入持开放态度。
  • 你的图片尺寸是多少? (以像素为单位)
  • @arxanas 我知道 :),这是在 localhost 上运行的,到目前为止它只有 1 天的项目。所以我稍后会处理安全问题 :) 但首先必须让其他一切正常工作。
  • @AlexanderLarikov 你所说的维度是什么意思? $max_width = 960; $max_height = 200;示例中提供。但用户可以上传或提交最大为 5000x5000 @ 18mb 的图片
  • 这可能无助于回答您的问题,但您应该停止使用mysql_* 函数。它们正在被弃用。请改用PDO(PHP 5.1 起支持)或mysqli(PHP 4.1 起支持)。如果您不确定使用哪一个,read this article

标签: php image-processing gd


【解决方案1】:

您可能需要在图像输出之前发送正确的标头,例如

header('Content-Type: image/png');

header('Content-Type: image/jpeg');

header('Content-Type: image/gif');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-06
    • 2013-08-12
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 2020-08-26
    相关资源
    最近更新 更多