【发布时间】: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');
【问题讨论】: