铁轨 + AMcharts(带有导出图像 PHP 脚本)- PHP 脚本转换为控制器?

发布于 2024-08-27 04:34:54 字数 1936 浏览 2 评论 0原文

这可能有点令人困惑。我正在将 AMCharts 与 Rails 一起使用。 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 代码从那时起就发生了变化 - 因为这两种实现都不适合我。

This one might be a little confusing. I'm using AMCharts with rails. Amcharts comes with a PHP script to export images called "export.php"

I'm trying to figure out how to take the code in export.php and put it into a controller.

Here is the code:

   <?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);
?>

There are some versions in existence in a thread I found here: http://www.amcharts.com/forum/viewtopic.php?id=341

But I have a feeling the PHP code above has changed since then - because neither implementation worked for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从﹋此江山别 2024-09-03 04:34:54

该代码或多或少是抓取发送到脚本(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

实际上,我发现了 关于加速逐像素绘制的讨论

What this code more or less dose is grabs the informations, that were sent to the script (POST).
The informations include the height and width of the picture and the RGB values of every pixel. The script draws every pixel and sends the images at the end to the client.

You can use Rmagick's method to draw a pixel. This will give you the same result.

The incomming post data looks like this:

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

Actually, I found a discussion about speeding up pixel by pixel drawing.

ゝ杯具 2024-09-03 04:34:54

显然我遇到了其他错误,这让我认为现有的代码不起作用。然而,我在原始问题中链接到的线程上的代码实际上确实有效!

So apparently I was running into other errors which made me think the already existing code didnt work. However, the code on the thread I linked to in the original question does in fact work!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文