【问题标题】:Create jpgs from pdf with WordPress plugin使用 WordPress 插件从 pdf 创建 jpgs
【发布时间】:2015-10-10 15:40:12
【问题描述】:

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

我使用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');

【问题讨论】:

    标签: php pdf wordpress jpeg


    【解决方案1】:

    这是实现此目的的代码。 pdf文件必须上传到post,否则没有$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' );
    

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-02
        • 2011-09-07
        • 2017-02-14
        • 2013-09-15
        • 1970-01-01
        • 2012-09-03
        • 2015-01-23
        • 1970-01-01
        相关资源
        最近更新 更多