Created
March 1, 2013 04:11
-
-
Save cesarmiquel/5062416 to your computer and use it in GitHub Desktop.
Go through all document content-types (should be PDFs), generate a thumbnail of the first page of the PDF and save the thumbnail in a field of the document.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Select all nodes of type Document | |
$result = db_select('node', 'n') | |
->fields('n', array('nid')) | |
->condition('type', 'document') | |
->execute(); | |
// create thumbnail and attach it | |
foreach($result as $row) { | |
$node = node_load($row->nid); | |
create_thumbnail($node); | |
} | |
exit(0); | |
function create_thumbnail($node) { | |
$document = entity_metadata_wrapper('node', $node); | |
$attachments = $document->field_attachment->value(); | |
$first_attachment = $attachments[0]; | |
$title = $document->title->value(); | |
$pdf = drupal_realpath($first_attachment['uri']); | |
$image_name = sprintf("doc-thumbnail-%05d.png", $node->nid); | |
$dest_uri = 'public://document/thumbnails/tmp/' . $image_name; | |
$image_path = drupal_realpath($dest_uri); | |
// run convert | |
$cmd = sprintf("/usr/bin/convert '%s'[0] -density 150x150 %s", | |
$pdf, | |
$image_path | |
); | |
system($cmd); | |
echo "Executing: [$cmd]\n"; | |
$file = (object) array( | |
'uid' => 1, | |
'uri' => $dest_uri, | |
'filemime' => file_get_mimetype($image_path), | |
'status' => 1, | |
'title' => $title, | |
'alt' => $title, | |
); | |
$file = file_copy($file, 'public://document/thumbnails/' . $image_name, FILE_EXISTS_REPLACE); | |
if ($file) { | |
// save node | |
$document->field_image = array('fid' => $file->fid); | |
$document->save(); | |
} | |
else { | |
echo "Problems generating thumbnail for [$node->title] (nid: $node->nid)\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment