Rails + AMcharts (with export image php script) - PHP script converted to controller?

StackOverflow https://stackoverflow.com/questions/2569346

  •  24-09-2019
  •  | 
  •  

문제

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.

도움이 되었습니까?

해결책 2

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!

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top