Angelo
Angelo

Reputation: 111

Convert uploaded pdf file to jpg in codeigniter

I am currently beginning to learn CodeIgniter. There is this site that I've been working on.

In this site, users can upload PDF files but they are only allowed to view their uploaded files in JPG format. The question is, how can I convert the PDF file into JPG on the time of upload and store JPG format instead of PDF.

here is the code of my 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');
    }
}

I've already read about Imagick but I don't know how to use it in CodeIgniter. Can you give me some tutorials and examples or a much easier way to convert PDF to JPG in CodeIgniter?

Thank you in advance guys.

Upvotes: 2

Views: 7047

Answers (1)

Deepak Panwar
Deepak Panwar

Reputation: 179

$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.

Upvotes: 3

Related Questions