我目前正在学习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的更简单方法吗?
先谢谢你们。
答案 0 :(得分:0)
$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'
)));
试试这个,你可以使用它来上传和转换多个文件。