function pdf_to_image_generate_page in PDF to ImageField 7.3
Same name and namespace in other branches
- 7.2 pdf_to_image.module \pdf_to_image_generate_page()
Generate a single page for the given pdf file.
Return value
array File definition, registered in the DB.
2 calls to pdf_to_image_generate_page()
- pdf_to_image_generate_process in ./
pdf_to_image.module - Processing pdf file creation.
- pdf_to_image_generate_process_page in ./
pdf_to_image.module - Generate a single page (of the given index) inside a batch process.
File
- ./
pdf_to_image.module, line 535 - Generates thumbnail image(s) from an uploaded PDF.
Code
function pdf_to_image_generate_page($params, $page_number = 0) {
$source_file = drupal_realpath($params['pdf']['file']->uri);
if (!file_exists($source_file)) {
watchdog('pdf_to_image', 'Invalid file given to convert. Could not read %file (%source_file)', array(
'%file' => $params['pdf']['file']->uri,
'%source_file' => $source_file,
), WATCHDOG_ERROR);
return NULL;
}
$density = "-density " . $params['pdf']['instance']['widget']['settings']['pdf_to_image']['density'];
$extra_args = isset($params['pdf']['instance']['widget']['settings']['pdf_to_image']['extra_args']) ? $params['pdf']['instance']['widget']['settings']['pdf_to_image']['extra_args'] : "";
$format = isset($params['pdf']['instance']['widget']['settings']['pdf_to_image']['target_format']) ? $params['pdf']['instance']['widget']['settings']['pdf_to_image']['target_format'] : "jpg";
// We need to know both the uri and realpath versions of the paths we want to
// work with.
$image_dir_uri = file_stream_wrapper_uri_normalize($params['image']['field']['settings']['uri_scheme'] . '://' . $params['image']['instance']['settings']['file_directory']);
file_prepare_directory($image_dir_uri, FILE_CREATE_DIRECTORY);
if (!empty($params['image']['instance']['settings']['file_directory'])) {
$image_dir_uri .= '/';
}
$image_uri = $image_dir_uri . $params['pdf']['file']->fid . "-" . $page_number . '.' . $format;
$image_realpath = drupal_realpath($image_uri);
if (empty($image_uri)) {
watchdog('pdf_to_image', 'Failed to calculate a destination filename for conversion', array(), WATCHDOG_ERROR);
return FALSE;
}
// Check to see if the target image file already exists, is registered in the
// database. Why?
$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));
watchdog('pdf_to_image', 'PDF preview %image already exists. Re-attaching it.', array(
'%image' => $image_uri,
), WATCHDOG_INFO);
return $file;
}
watchdog('pdf_to_image', 'Converting PDF: %file page %page_number: to image: %image', array(
'%file' => $params['pdf']['file']->uri,
'%page_number' => $page_number,
'%image' => $image_uri,
), WATCHDOG_INFO);
pdf_to_image_convert_exec($source_file . '[' . $page_number . ']', $image_realpath, array(), array(
$density,
$extra_args,
));
if (file_exists($image_realpath)) {
watchdog('pdf_to_image', 'PDF preview %image created', array(
'%image' => $image_uri,
), WATCHDOG_INFO);
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;
}
watchdog('pdf_to_image', 'Failed to generate image. Expected result at %image_realpath but it was not produced.', array(
'%image_realpath' => $image_realpath,
), WATCHDOG_ERROR);
return FALSE;
}