You are here

function pdf_to_image_generate_page in PDF to ImageField 7.2

Same name and namespace in other branches
  1. 7.3 pdf_to_image.module \pdf_to_image_generate_page()

Generate a single page for the given pdf file.

1 call to pdf_to_image_generate_page()
pdf_to_image_generate_process_page in ./pdf_to_image.module
Generate a single page in batch process.

File

./pdf_to_image.module, line 530
Implement a pdf field, based on the file module's file field.

Code

function pdf_to_image_generate_page($params, $page_number = 0) {
  $source_file = file_stream_wrapper_get_instance_by_uri($params['pdf']['file']->uri)
    ->realpath();
  if (!file_exists($source_file)) {
    return NULL;
  }

  // Add density arguments, preg_replace using for security.
  $density_x = preg_replace('/^([0-9]+)x[0-9]+$/', '\\1', $params['pdf']['instance']['widget']['settings']['density']);
  $density_y = preg_replace('/^[0-9]+x([0-9]+)$/', '\\1', $params['pdf']['instance']['widget']['settings']['density']);
  $density = "-density {$density_x}x{$density_y}";
  $image_dir = file_stream_wrapper_get_instance_by_scheme($params['image']['field']['settings']['uri_scheme'])
    ->realpath();
  $image_filename = $params['image']['instance']['settings']['file_directory'] . '/' . $params['pdf']['file']->fid . "-{$density_x}x{$density_y}-" . $page_number . '.jpg';
  $image_realpath = $image_dir . '/' . $image_filename;
  $image_uri = file_stream_wrapper_uri_normalize($params['image']['field']['settings']['uri_scheme'] . '://' . $image_filename);
  if (empty($image_uri)) {
    return FALSE;
  }
  $query = db_select('file_managed', 'f')
    ->fields('f', array(
    'fid',
  ))
    ->condition('uri', $image_uri)
    ->execute()
    ->fetchCol();
  if (!empty($query)) {
    $file = file_load(array_shift($query));
    return $file;
  }
  pdf_to_image_exec($source_file . '[' . $page_number . ']', $image_realpath, array(), array(
    $density,
  ));
  if (file_exists($image_realpath)) {
    global $user;
    $file = (object) array(
      'uid' => $user->uid,
      'filename' => basename($image_uri),
      'uri' => $image_uri,
      'filemime' => file_get_mimetype($image_uri),
      'filesize' => @filesize($image_uri),
      'timestamp' => REQUEST_TIME,
      'status' => FALSE,
      'is_new' => TRUE,
    );
    file_save($file);
    return $file;
  }
  return FALSE;
}