Rails + AmCharts(エクスポート画像PHPスクリプト) - PHPスクリプトがコントローラーに変換されましたか?
-
24-09-2019 - |
質問
これは少し混乱するかもしれません。レール付きの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コードが変更されていると感じています。どちらの実装も私のために機能しなかったからです。
解決 2
どうやら私は他のエラーに遭遇していたので、すでに既存のコードが機能しなかったと思いました。ただし、元の質問でリンクしたスレッド上のコードは、実際には機能します!
他のヒント
このコードが多かれ少なかれ用量は、スクリプトに送信された情報を取得します(投稿)。情報には、画像の高さと幅、およびすべてのピクセルの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
実際、私はaを見つけました 討論 ピクセル描画によるピクセルのスピードアップについて。
所属していません StackOverflow