【问题标题】:Rails + AMcharts (with export image php script) - PHP script converted to controller?Rails + AMcharts(带导出图像 php 脚本)- PHP 脚本转换为控制器?
【发布时间】:2010-04-02 21:39:03
【问题描述】:

这可能有点令人困惑。我正在使用带有导轨的 AMCharts。 Amcharts 附带一个 PHP 脚本来导出名为“export.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 代码从那以后发生了变化——因为这两种实现都不适合我。

【问题讨论】:

    标签: php ruby-on-rails amcharts


    【解决方案1】:

    此代码或多或少的作用是获取发送到脚本 (POST) 的信息。 这些信息包括图片的高度和宽度以及每个像素的RGB值。该脚本绘制每个像素并在最后将图像发送给客户端。

    您可以使用Rmagick's method to draw a pixel。这会给你同样的结果。

    传入的帖子数据如下所示:

    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,关于加快逐像素绘图的速度。

    【讨论】:

    • 您好 DrDol,感谢您的评论!不过这对我来说还是有点太复杂了?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-23
      • 2017-09-23
      • 2021-10-24
      • 2017-07-16
      • 1970-01-01
      • 1970-01-01
      • 2012-11-29
      • 2015-02-05
      相关资源
      最近更新 更多