codeigniter - 在codeigniter中将上传的pdf文件转换为jpg

标签 codeigniter pdf imagemagick-convert

我目前正在开始学习CodeIgniter。我一直在开发这个网站。

在此站点中,用户可以上传 PDF 文件,但仅允许查看上传的 JPG 格式文件。问题是,如何在上传时将PDF文件转换为JPG并存储JPG格式而不是PDF。

这是我的CONTROLLER的代码

public function upload()
{
    if($this->session->userdata('logged_in'))
       {

        $session_data = $this->session->userdata('logged_in');
        $data['username'] = $session_data['username'];
        $data['permission'] = $session_data['permission'];

            if($data['permission']=='Super Admin' || $data['permission']=='Admin'){
                $this->load->view('header');
                $this->load->view('upload_form', array('error' => ' ' ));
            }

       }
       else
       {
         redirect('login', 'refresh');
       }
}

function do_upload()
{
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'pdf';
    $config['max_size'] = '10000';

    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());
        $this->load->view('header');
        $this->load->view('upload_form', array('error' => ' ' ));
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());
        $upload_data = $this->upload->data();
        $session_data = $this->session->userdata('logged_in');
        $first = $session_data['firstname'];
        $last = $session_data['lastname'];
        $dept = $session_data['department'];
        $uploader = $first." ".$last;
        $name = $upload_data['file_name'];
        $path = $upload_data['file_path'];

        $this->db->query("INSERT INTO tbl_uploaded
                (`uploaded_id`, `name`, `path`,`department`,`uploader`)
                VALUES ('','".$name."',
                '". $path."','".$dept."','".$uploader."')");

        redirect('csfi','refresh');
    }
}

我已经阅读过有关 Imagick 的内容,但我不知道如何在 CodeIgniter 中使用它。您能给我一些教程和示例,或者在 CodeIgniter 中将 PDF 转换为 JPG 的更简单的方法吗?

提前谢谢你们。

最佳答案

$config                   = array();
$config['allowed_types']  = 'pdf';
$config['overwrite']      = TRUE;
$config['remove_spaces']  = TRUE;

$this->load->library('upload', $config);

// Image manipulation library
$this->load->library('image_lib');

foreach ($notes['name'] as $key => $note) 
{
    $_FILES['notes']['name']      = $notes['name'][$key];
    $_FILES['notes']['type']      = $notes['type'][$key];
    $_FILES['notes']['tmp_name']  = $notes['tmp_name'][$key];
    $_FILES['notes']['error']     = $notes['error'][$key];
    $_FILES['notes']['size']      = $notes['size'][$key];

    $extension                    = pathinfo($_FILES['notes']['name'], PATHINFO_EXTENSION);
    $unique_no                    = uniqid(rand(), true);
    $filename[$key]               = $unique_no.'.'.$extension; // with ex
    $filename2[$key]              = $unique_no; // without ex

    $target_path                  = "notes_files/";

    if (!is_dir($target_path))
    {
        mkdir('./'.$target_path, 0777, true);
    }

    $config['file_name']          = $filename[$key];
    $config['upload_path']        = './'.$target_path;

    $this->upload->initialize($config);

    if (! $this->upload->do_upload('notes')) 
    {
        return array('error' => $this->upload->display_errors());
    }

    // converting pdf to images with imagick
    $im             = new Imagick();
    $im->setResolution(160,220);

    $ig = 0;

    while(true)
    {
        try {
            $im->readimage($config['upload_path'].$config['file_name']."[$ig]");
        } catch (Exception $e) {
            $ig = -1;
        }

        if($ig === -1) break;

        $im->setImageBackgroundColor('white');
        $im->setImageAlphaChannel(imagick::ALPHACHANNEL_REMOVE);
        $im->mergeImageLayers(imagick::LAYERMETHOD_FLATTEN);
        $im->setImageFormat('jpg'); 

        $image_name     = $filename2[$key]."_$ig".'.jpg';
        $imageprops     = $im->getImageGeometry();

        $im->writeImage($config['upload_path'] .$image_name); 
        $im->clear(); 
        $im->destroy();

        // change file permission for file manipulation
        chmod($config['upload_path'].$image_name, 0777); // CHMOD file

        // add watermark to image
        $img_manip              = array();
        $img_manip              = array(
            'image_library'     => 'gd2', 
            'wm_type'           => 'overlay',
            'wm_overlay_path'   => FCPATH . '/uploads/institutes/'.$institute_logo, // path to watermark image
            'wm_x_transp'       => '10',
            'wm_y_transp'       => '10',
            'wm_opacity'        => '10',
            'wm_vrt_alignment'  => 'middle',
            'wm_hor_alignment'  => 'center',
            'source_image'      => $config['upload_path'].$image_name
        );

        $this->image_lib->initialize($img_manip);
        $this->image_lib->watermark();

        ImageJPEG(ImageCreateFromString(file_get_contents($config['upload_path'].$image_name)), $config['upload_path'].$image_name, );

        $ig++;
    }

    // unlink the original pdf file
    chmod($config['upload_path'].$config['file_name'], 0777); // CHMOD file
    unlink($config['upload_path'].$config['file_name']);    // remove file
}
// echo '<p>Success</p>';exit;
die(json_encode(array(
    'data' => 'Success',
    'status' => 'success'
)));

Try this, you can upload and convert multiple files using this.

关于codeigniter - 在codeigniter中将上传的pdf文件转换为jpg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35568601/

相关文章:

codeigniter - CodeIgniter 中的查询结果

php - 搜索结果分页效果不佳...搜索正在工作,但分页导致 codeigniter 中出现错误

linux - 在 Linux : Output Issues 上以批处理模式运行 R

php - 如何将购物车表记录复制到另一个表中?

java - 使用java从模板生成pdf文档的最佳方法

android - 应用程序内附加的 PDF 文件但不使用 android Intent 发送

java - ImageMagick Convert for EPS to JPG 未正确调用

python - 从 .ps 到 .png 的 ImageMagick 转换,从 python 运行 - 无效参数

imagemagick - 如何将 PNG 图像(特别是灰度图像)转换为索引颜色

php - 代码点火器 : insert data in database