2015-07-21 87 views
2

我正在研究一個插件,它可以從上傳的pdf文件的每個頁面創建jpgs。使用WordPress插件從PDF創建jpgs

我使用wp_handle_upload動作和檢查MIME類型的PDF。 之後,我使用Imagick獲取頁面數量並從每個頁面創建一個新的jpg。然後文件被上傳。

我認爲WordPress從頭開始不支持ImageMagick,所以我安裝了ImageMagick Engine Plugin。

當我現在在Wordpress中上傳文件時,我只是得到一個錯誤。我不知道什麼是行不通的。

任何有關錯誤的想法?
感謝,奧利弗

function process_pdf($results) { 

if($results['type'] === 'application/pdf') { 

    $filename = $results[ 'file' ]; 
    $filename_wo_extension = basename($filename); 

    $url = $results[ 'url' ]; 

    $im = new Imagick(); 
    $im->setResolution(300, 300); 
    $pages = $im->getNumberImages(); 

    for($p = 0; $p < $pages; $p++){ 
     // http://stackoverflow.com/questions/467793/how-do-i-convert-a-pdf-document-to-a-preview-image-in-php 
     // http://stackoverflow.com/questions/1143841/count-the-number-of-pages-in-a-pdf-in-only-php 
     $im->readImage($url.'['.p.']'); 
     $im->setImageFormat('jpg'); 

     $filename_neu = $filename_wo_extension .'_'. $p .'.jpg'; 

     // https://codex.wordpress.org/Function_Reference/wp_insert_attachment 
     $upload_file = wp_upload_bits($filename_neu, null, $im); 
     if (!$upload_file['error']) { 

      $attachment = array(
       'post_mime_type' => 'image/jpeg', 
       'post_title' => preg_replace('/\.[^.]+$/', '', $filename_neu), 
       'post_content' => '', 
       'post_status' => 'inherit' 
      ); 

      $attachment_id = wp_insert_attachment($attachment, $upload_file['file']); 

      if (!is_wp_error($attachment_id)) { 
       require_once(ABSPATH . "wp-admin" . '/includes/image.php'); 
       $attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']); 
       wp_update_attachment_metadata($attachment_id, $attachment_data); 
      } 
     } 
    } 
} 
} 

add_action('wp_handle_upload', 'process_pdf'); 

回答

2

這裏是實現這一目標的代碼。 pdf文件必須上傳到帖子,否則沒有$post_id。 現在唯一的事情是,點擊保存時,自定義字段(庫)被覆蓋。上傳PDF後未保存帖子時,圖片位於圖庫中。

function process_pdf($file) { 

    if($file['type'] === 'application/pdf') { 

     // Get the parent post ID, if there is one 
     if(isset($_REQUEST['post_id'])) { 
      $post_id = $_REQUEST['post_id']; 

      $filename = $file[ 'name' ]; 
      $filename_wo_extension = basename($filename, ".pdf"); 

      $im = new Imagick(); 
      $im->setResolution(300, 300); 
      $im->readimage($file[ 'tmp_name' ]); 
      $pages = $im->getNumberImages(); 

      $attachments_array = array(); 

      // iterate over pages of the pdf file 
      for($p = 1; $p <= $pages; $p++){ 
       $im->setIteratorIndex($p - 1); 
       $im->setImageFormat('jpeg'); 

       $filename_neu = $filename_wo_extension .'_'. $p .'.jpg'; 

       // upload new image to wordpress 
       // https://codex.wordpress.org/Function_Reference/wp_insert_attachment 
       $upload_file = wp_upload_bits($filename_neu, null, $im); 
       if (!$upload_file['error']) { 

        $attachment = array(
         'post_mime_type' => 'image/jpeg', 
         'post_title' => preg_replace('/\.[^.]+$/', '', $filename_neu), 
         'post_content' => '', 
         'post_status' => 'inherit' 
        ); 

        $attachment_id = wp_insert_attachment($attachment, $upload_file['file']); 

        if (!is_wp_error($attachment_id)) { 
         require_once(ABSPATH . "wp-admin" . '/includes/image.php'); 
         $attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']); 
         wp_update_attachment_metadata($attachment_id, $attachment_data); 
         $attachments_array[] = $attachment_id; 
        } 
       } 
      } 

      // add new images to a gallery (advanced custom fields plugin) 
      // http://www.advancedcustomfields.com/resources/update_field/ 
      update_field('field_55b0a473da995', $attachments_array, $post_id); 

      $im->destroy(); 
     } 
    } 

    return $file; 

} 

add_filter('wp_handle_upload_prefilter', 'process_pdf'); 
1

我知道這是一個古老的線程,但無論如何,我想說,有一條很細的WordPress插件,PDF文件的交易,第一個頁面轉換成圖像,無論是使用ImageMagick的或IMagik (它讓你選擇你已經安裝在你的網站上)。

彷彿免費提供的,我想這可能是一些幫助,誰就可能在這個問題上進行研究的源代碼:

PDF圖像生成 https://wordpress.org/plugins/pdf-image-generator/