2010-04-02 32 views
0

這個可能有點混亂。我正在使用AMCharts和rails。 Amcharts附帶一個PHP腳本來導出名爲「export.php」的圖像Rails + AMcharts(帶出口圖片php腳本) - PHP腳本轉換爲控制器?

我想弄清楚如何將代碼放在export.php中並將其放入控制器中。

下面是代碼:

<?php 
// amcharts.com export to image utility 
// set image type (gif/png/jpeg) 
$imgtype = 'jpeg'; 

// set image quality (from 0 to 100, not applicable to gif) 
$imgquality = 100; 

// get data from $_POST or $_GET ? 
$data = &$_POST; 

// get image dimensions 
$width = (int) $data['width']; 
$height = (int) $data['height']; 

// create image object 
$img = imagecreatetruecolor($width, $height); 

// populate image with pixels 
for ($y = 0; $y < $height; $y++) { 
    // innitialize 
    $x = 0; 

    // get row data 
    $row = explode(',', $data['r'.$y]); 

    // place row pixels 
    $cnt = sizeof($row); 
    for ($r = 0; $r < $cnt; $r++) { 
    // get pixel(s) data 
    $pixel = explode(':', $row[$r]); 

    // get color 
    $pixel[0] = str_pad($pixel[0], 6, '0', STR_PAD_LEFT); 
    $cr = hexdec(substr($pixel[0], 0, 2)); 
    $cg = hexdec(substr($pixel[0], 2, 2)); 
    $cb = hexdec(substr($pixel[0], 4, 2)); 

    // allocate color 
    $color = imagecolorallocate($img, $cr, $cg, $cb); 

    // place repeating pixels 
    $repeat = isset($pixel[1]) ? (int) $pixel[1] : 1; 
    for ($c = 0; $c < $repeat; $c++) { 
     // place pixel 
     imagesetpixel($img, $x, $y, $color); 

     // iterate column 
     $x++; 
    } 
    } 
} 

// set proper content type 
header('Content-type: image/'.$imgtype); 
header('Content-Disposition: attachment; filename="chart.'.$imgtype.'"'); 

// stream image 
$function = 'image'.$imgtype; 
if ($imgtype == 'gif') { 
    $function($img); 
} 
else { 
    $function($img, null, $imgquality); 
} 

// destroy 
imagedestroy($img); 
?> 

有一些版本在一個線程中存在,我發現在這裏:http://www.amcharts.com/forum/viewtopic.php?id=341

但是我有一種感覺,PHP代碼,因爲再上面已經改變了 - 因爲無論是實施爲我工作。

回答

0

所以顯然我遇到了其他錯誤,這讓我認爲已經存在的代碼不起作用。但是,我在原始問題中鏈接到的線程上的代碼實際上工作正常!

0

這段代碼或多或少的劑量是抓取發送到腳本(POST)的信息。 這些信息包括圖片的高度和寬度以及每個像素的RGB值。該腳本繪製每個像素並將圖像最後發送給客戶端。您可以使用Rmagick's method to draw a pixel。這會給你同樣的結果。

該進來的post數據是這樣的:

height = number -> cast to int 
width = number -> cast to int 
// first row with a repeating part of R:G:B,R:G:B,... (n = width) 
r0 = 255:0:0,150:120:0,77:88:99,... 
r1 = ... 
. 
. 
r100 = ... -> the row count is the height - 1 

其實,我發現了一個discussion有關像素繪圖加速像素。

+0

嘿DrDol,感謝您的評論!儘管如此,這仍然有點複雜。 – Elliot 2010-04-03 11:52:49