2012-08-27 78 views
0

「res.php」下方提供的腳本旨在縮放隨後用於HTML頁面的HEADER css中的圖像。根據「CID」(俱樂部ID)調用圖像。每個俱樂部都有存儲在數據庫中的信息,其中包括一個名爲club_header的信息。 club_header是一個存儲絕對圖像位置(文件系統或網頁)的VARCHAR。 「res.php」功能是調用圖像並將其輸出到瀏覽器,以及將圖像重新調整大小以適應設置。默認情況下,這些設置是960 x 200.PHP圖像縮放不起作用

但是,由於某些原因,腳本沒有做它必須做的事情,而是輸出一個破碎的圖像。代碼有什麼問題?

某些服務器+ 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); 

預先感謝您。

+0

你打開SQL注入。 –

+0

你的照片尺寸是多少? (以像素爲單位) –

+0

@arxanas我知道:),這是在本地主機上運行的,也是它迄今爲止唯一的1天項目。所以我會在稍後處理安全問題:)但首先必須讓所有其他工作。 –

回答

0

您可能需要將圖像輸出之前發送正確的標題,如

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

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

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