You are here

function chart_copy in Google Chart Tools: Image Charts 7

Same name and namespace in other branches
  1. 5 chart.module \chart_copy()
  2. 6 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/string Returns FALSE on failure, and file path on success.

File

./chart.module, line 207
Provides primary Drupal hook implementations.

Code

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

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

    // Generate temp image
    $tempname = tempnam(file_directory_temp(), 'tmp_');
    if (!file_prepare_directory($dest)) {
      _chart_error(t('Failed to prepare destination directory.'));
      return FALSE;
    }
    $filename = $dest . DIRECTORY_SEPARATOR . $name . '.png';

    // We add the protocol to the url as chart_url returns
    // a protocol-relative URI - @see http://drupal.org/node/587742
    if (isset($_SERVER['https']) && $_SERVER['https'] == 'on') {
      $protocol = 'https:';
    }
    else {
      $protocol = 'http:';
    }
    $png = imagecreatefrompng($protocol . chart_url($chart));
    $fh = fopen($tempname, 'w+');
    imagepng($png, $tempname);

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

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