Php 使用WordPress插件从pdf创建JPG

Php 使用WordPress插件从pdf创建JPG,php,pdf,wordpress,jpeg,Php,Pdf,Wordpress,Jpeg,我正在开发一个插件,它可以从上传的pdf文件的每个页面创建JPG 我使用wp_handle_upload操作并检查mime类型是否为pdf。 之后,我使用Imagick获取页面计数,并从每个页面创建一个新的jpg。然后上传文件 我认为Wordpress不支持ImageMagick,所以我安装了ImageMagick引擎插件 当我现在在Wordpress中上传一个文件时,我得到了一个错误。我不知道到底是什么不起作用 你知道哪里出了问题吗? 谢谢,奥利弗 function process_pdf($

我正在开发一个插件,它可以从上传的pdf文件的每个页面创建JPG

我使用wp_handle_upload操作并检查mime类型是否为pdf。 之后,我使用Imagick获取页面计数,并从每个页面创建一个新的jpg。然后上传文件

我认为Wordpress不支持ImageMagick,所以我安装了ImageMagick引擎插件

当我现在在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');

下面是实现这一点的代码。 pdf文件必须上传到帖子,否则没有$post\u 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' );

我知道这是一个旧的线程,但无论如何,我想说的是,有一个非常好的WordPress插件处理PDF文件,将第一页转换成图像,使用ImageMagick或IMagik。它让你选择你在网站上安装的内容

作为免费提供的源代码,我想它可能会对研究这一问题的人有所帮助:

PDF图像生成器