You are here

function chart_copy in Google Chart Tools: Image Charts 6

Same name and namespace in other branches
  1. 5 chart.module \chart_copy()
  2. 7 chart.module \chart_copy()

Copies rendered chart image.

Parameters

array $chart: Chart API structure

string $name: (optional) Filename WITHOUT extension. when NULL #chart_id is used.

string $dest: (optional) A string containing the path to verify. If this value is omitted, Drupal's 'files/charts' directory will be used.

string $replace: (optional) Replace behavior when the destination file already exists.

Return value

bool

File

./chart.module, line 188
Provides Google chart API integration.

Code

function chart_copy($chart, $name = NULL, $dest = 'charts', $replace = FILE_EXISTS_REPLACE) {
  if (!($chart_query_string = chart_build($chart))) {
    return FALSE;
  }
  if ($dest = file_create_path($dest)) {
    if (file_check_directory($dest, FILE_CREATE_DIRECTORY)) {

      // Defaults
      $name = is_null($name) ? $chart['#chart_id'] : $name;

      // Generate temp image
      $tempname = tempnam(file_directory_temp(), 'tmp_');
      $filename = file_create_path($dest) . '/' . $name . '.png';
      $png = imagecreatefrompng(CHART_URI . '?' . $chart_query_string);
      $fh = fopen($tempname, 'w+');
      imagepng($png, $tempname);

      // Copy temp image to new location
      if (!file_copy($tempname, $filename, $replace)) {
        _chart_error(t('Failed to copy chart image.'));
        return FALSE;
      }

      // Remove temp image
      fclose($fh);
      return $filename;
    }
  }
  return FALSE;
}