0

これは少し混乱するかもしれません。レールで AMCharts を使用しています。Amcharts には、「export.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 コードが変更されているように感じます。どちらの実装も機能しなかったからです。

4

2 に答える 2

0

このコードは多かれ少なかれ、スクリプト (POST) に送信された情報を取得します。この情報には、画像の高さと幅、および各ピクセルの RGB 値が含まれます。スクリプトはすべてのピクセルを描画し、最後に画像をクライアントに送信します。

Rmagick のメソッドを使用してピクセルを描画できます。これにより、同じ結果が得られます。

受信投稿データは次のようになります。

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

実際、ピクセルごとの描画を高速化することについての議論を見つけました。

于 2010-04-02T22:13:51.100 に答える
0

そのため、既存のコードが機能しないと思われる他のエラーが発生していたようです。ただし、元の質問でリンクしたスレッドのコードは実際に機能します!

于 2010-04-06T10:54:35.987 に答える