function pdf_to_image_generate_process in PDF to ImageField 7.3
Same name and namespace in other branches
- 7.2 pdf_to_image.module \pdf_to_image_generate_process()
Processing pdf file creation.
This sets up the batch job with all the neccessary parameters.
1 call to pdf_to_image_generate_process()
- pdf_to_image_entity_update in ./
pdf_to_image.module - When a fieldable entity is being updated, regenerate the files.
File
- ./
pdf_to_image.module, line 390 - Generates thumbnail image(s) from an uploaded PDF.
Code
function pdf_to_image_generate_process($entity_type, $entity, $field_id, $field_instance) {
$field_lang = field_language($entity_type, $entity, $field_id);
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
if (!empty($field_instance['widget']['settings']['pdf_to_image']['target_field'])) {
$target_field = $field_instance['widget']['settings']['pdf_to_image']['target_field'];
}
if (empty($target_field)) {
// No target field configured? do nothing.
return;
}
$new_files = field_get_items($entity_type, $entity, $field_id);
$new_images = field_get_items($entity_type, $entity, $target_field);
if (!empty($entity->original)) {
$previous_files = field_get_items($entity_type, $entity->original, $field_id);
$previous_images = field_get_items($entity_type, $entity->original, $target_field);
}
else {
$previous_files = $previous_images = array();
}
// Decide if it's really neccessary to regen the image.
if (empty($new_files)) {
// No file attachment found in the source field.
// Do nothing.
return;
}
if (empty($previous_images) && !empty($new_images)) {
// This must be a manual image addition, so do NOT auto process.
return;
}
if (!empty($new_images[0]['fid']) && $previous_images[0]['fid'] != $new_images[0]['fid']) {
// This must be a manual image replace, so do NOT auto process.
return;
}
if (!empty($previous_files) && $previous_files[0]['fid'] == $new_files[0]['fid'] && !empty($new_images)) {
// Attached file has not changed, so does not need updating.
return;
}
// By now, we probably DO want to update.
// Either the image is empty, or the file has changed.
$pdf_file = file_load($new_files[0]['fid']);
$pdf_realpath = file_stream_wrapper_get_instance_by_uri($pdf_file->uri)
->realpath();
$count = pdf_to_image_count_pages($pdf_realpath);
if (!$count) {
watchdog('pdf_to_image', 'Invalid page count on PDF %file', array(
'%file' => $pdf_file->uri,
), WATCHDOG_ERROR);
return;
}
// So far so good, we have decided to proceed.
$field = field_info_field($target_field);
// Prepare count parameter.
if ($field['cardinality'] != -1 && $count > $field['cardinality']) {
$count = $field['cardinality'];
}
// Arguments to give to the batch job.
$params = array(
'entity' => $entity,
// Don't actually need the whole thing, just the id really.
'entity_type' => $entity_type,
'entity_id' => $id,
'image' => array(
'field' => $field,
'instance' => field_info_instance($entity_type, $target_field, $field_instance['bundle']),
),
'pdf' => array(
'instance' => $field_instance,
'file' => $pdf_file,
),
);
// TODO expose this as a setting.
$always_process_in_batch = FALSE;
if ($count > 1 || $always_process_in_batch) {
watchdog('pdf_to_image', 'Starting a process to convert attached PDF %file to image previews', array(
'%file' => $params['pdf']['file']->uri,
), WATCHDOG_INFO);
for ($page = 0; $page < $count; $page++) {
$operations[] = array(
'pdf_to_image_generate_process_page',
array(
$params,
$page,
),
);
}
batch_set(array(
'title' => t('Converting PDF, %count pages', array(
'%count' => $count,
)),
'operations' => $operations,
'finished' => 'pdf_to_image_generate_process_attach',
'progress_message' => t('Processed @current out of @total.'),
));
}
else {
watchdog('pdf_to_image', 'Just process one page of attached PDF %file to image previews', array(
'%file' => $params['pdf']['file']->uri,
), WATCHDOG_INFO);
$image_file = pdf_to_image_generate_page($params, 0);
if ($image_file) {
$entity->{$target_field}[$field_lang] = array();
$entity->{$target_field}[$field_lang][] = (array) $image_file;
$image_file_uri = $image_file->uri;
}
else {
$image_file_uri = "NO IMAGE";
}
// Let filefield_paths apply the renaming rules for our files.
if (module_exists('filefield_paths')) {
filefield_paths_field_storage_pre_update($entity_type, $entity);
}
// Saving should be taken care of by the caller?
// No, we have run after hook_field_save, so must do it ourself.
field_attach_presave($entity_type, $entity);
field_attach_update($entity_type, $entity);
// Essential cleanup of use of filefield_paths_field_storage_pre_update().
// @see https://www.drupal.org/node/2503839
if ($image_file && empty($previous_images) && module_exists('filefield_paths') && !empty($entity->original->{$target_field}[$field_lang])) {
list($entity_id, , ) = entity_extract_ids($entity_type, $entity);
file_usage_add($image_file, 'file', $entity_type, $entity_id, 1);
}
watchdog('pdf_to_image', '%file created and attached to %entity_type', array(
'%file' => $image_file_uri,
'%entity_type' => $entity_type,
), WATCHDOG_INFO);
}
}