You are here

function phantomjs_capture_screen in PhantomJS Capture 7

Same name and namespace in other branches
  1. 8 phantomjs_capture.module \phantomjs_capture_screen()

Captures a screen shot using PhantomJS by calling the program.

Parameters

string $url: The URL to render the screen shot from.

string $dest: The destination for the rendered file (e.g. public://phantomjs).

string $filename: The filename to store the file as in the destination.

string $element: The id of the DOM element to render in the document.

Return value

bool Returns TRUE if the screen shot was taken else FALSE on error.

4 calls to phantomjs_capture_screen()
phantomjs_capture_field_field_formatter_view in phantomjs_capture_field/phantomjs_capture_field.module
Implements hook_field_formatter_view().
phantomjs_capture_field_field_insert in phantomjs_capture_field/phantomjs_capture_field.module
Implements hook_field_insert().
phantomjs_capture_field_field_update in phantomjs_capture_field/phantomjs_capture_field.module
Implements hook_field_update().
phantomjs_capture_test_submit in ./phantomjs_capture.module
Ajax callback for the test PhantomJS form on the administration page.

File

./phantomjs_capture.module, line 238
Defines the administration interface and utility functions to use PhantomJS and test screen shot capture functions.

Code

function phantomjs_capture_screen($url, $dest, $filename, $element = NULL) {

  // Get PhantomJS binary.
  $binary = _phantomjs_capture_get_binary();
  if (!$binary) {
    drupal_set_message(t('PhantomJS binary was not found. Plase intall PhantomJS on the system.'));
    return FALSE;
  }

  // Check that destination is writable.
  if (!file_prepare_directory($dest, FILE_CREATE_DIRECTORY)) {
    drupal_set_message(t('The PhantomJS destination path (@dest) was not writeable or could not be created.', array(
      '@dest' => $dest,
    )));
    return FALSE;
  }

  // Get absolute path to PhantomJS script and the destination file.
  $script = realpath(variable_get('phantomjs_capture_script', drupal_get_path('module', 'phantomjs_capture') . '/js/phantomjs_capture.js'));
  $dest = drupal_realpath($dest . '/' . $filename);

  // Run PhantomJS to create the screen shot.
  $output = array();
  if ($element) {
    exec($binary . ' ' . $script . ' "' . $url . '" ' . $dest . ' ' . escapeshellarg($element), $output);
  }
  else {
    exec($binary . ' ' . $script . ' "' . $url . '" ' . $dest, $output);
  }

  // Check that PhantomJS was able to load the page.
  if ($output[0] == '500') {
    return FALSE;
  }
  return TRUE;
}