I'm currently creating a program using codeigniter where I want to have image upload and save it as blob to database. The problem is when I tried to upload the image, I received this error
The path to the image is not correct.
Your server does not support the GD function required to process this type of image.
Here's my code in controller:
public function do_upload() {
if($this->session->userdata('logged_in')) {
//print_r($_FILES);
$config = array(
'upload_path' => './public/img/uploads',
'upload_url' => base_url().'public/img/uploads',
'allowed_types' => 'gif|jpg|png|jpeg',
//'overwrite' => TRUE,
'max_size' => '1000KB',
'max_width' => '1024',
'max_height' => '768',
//'encrypt_name' => true,
);
$this->load->library('upload', $config);
//for($i=0; $i<count($_FILES['userfile']['name']); $i++) {
if (!$this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('v_page/header_view');
$this->load->view('v_document/upload_error', $error);
$data['folderName'] = $this->dropdown->get_folder_details();
$data['departmentName'] = $this->dropdown->get_department_details();
$this->load->view('v_document/upload_view', $data);
$this->load->view('v_page/footer_view');
}
else {
$upload_data = $this->upload->data();
$this->_createThumbnail($upload_data['file_name']);
$data['thumbnail_name'] = $upload_data['raw_name']. '_thumb' .$upload_data['file_ext'];
$file_array = array(
'image' => "",
'image_name' => $upload_data['file_name'],
//'description' => "",
'date_created' => date('Y-m-d H:i:s', now()),
'date_modified' => date('Y-m-d H:i:s', now()),
'size' => $upload_data['file_size'],
'type' => $upload_data['image_type'],
'width' => $upload_data['image_width'],
'height' => $upload_data['image_height'],
);
$this->load->database();
$this->db->insert('tbl_image', $file_array);
$data = array('upload_data' => $this->upload->data());
$this->load->view('v_document/upload_success', $data);
}
//}
}
else {
redirect('login', 'refresh');
}
}
public function _createThumbnail($filename) {
$config['image_library'] = "gd2";
$config['source_image'] = "uploads/" .$filename;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = "80";
$config['height'] = "80";
$this->load->library('image_lib',$config);
if(!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
}
The image is uploaded successfully but the blob didn't work.
Try below suggestion:
Change from:
$config['source_image'] = "uploads/" .$filename;
To:
$config['source_image'] = "./uploads/" .$filename;
Related
my model
public function saveProduct(
$name,
$cat_id,
$user_id,
$description,
$status
) {
$post_image = '';
if (array_key_exists('post_image', $_FILES)) {
$config = [];
$config['upload_path'] = './assets/uploads/products';
$config['allowed_types'] = 'gif|svg|jpg|png|jpeg';
$config['max_size'] = '2048';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('post_image')) {
echo $this->upload->display_errors();
} else {
$post_image = $_FILES['post_image']['name'];
}
}
$data = [
'name' => $name,
'user_id' => $user_id,
'cat_id' => $cat_id,
'description' => $description,
'status' => 'active'
];
if ($post_image) {
$data['post_image'] = $post_image;
}
$this->db->insert('p0_products', $data);
return true;
}
it is changing file name to encryption in folder but in mysql i'am getting file name same as old name with unchanged on non-encrypted format
It's because you're taking from $_FILES veriable. You should take from
$this->upload->data()
So how?
$fileData = $this->upload->data();
This will return
file_name
file_type
file_path
etc.
$fileName = $fileData['file_name']
if I try to enter data, it can enter the database, but it doesn't display
// GAMBAR
public function gambar($id_program)
{
// panggil model
$program = $this->program_model->detail($id_program);
$gambar = $this->program_model->gambar($id_program);
// validasi input
$valid = $this->form_validation;
$valid->set_rules('judul_gambar', 'Judul/Nama Gambar', 'required',
array( 'required' => '%s harus diisi'));
if($valid->run()) {
$config['upload_path'] = './assets/upload/image/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2400'; // KiloByte
$config['max_width'] = '3000';
$config['max_height'] = '3000';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('gambar')) {
$data = array( 'title' => 'Tambah Gambar Program: '.$program->nama_program,
'program' => $program,
'gambar' => $gambar,
'error' => $this->upload->display_errors(),
'isi' => 'admin/program/gambar'
);
$this->load->view('admin/layout/wrapper', $data, FALSE);
}else {
$upload_data = array('uploads' =>$this->upload->data());
// Image Editor
$config['image_library'] = 'gd2';
$config['source_image'] = './assets/upload/image/'.$upload_data['uploads']['file_name'];
$config['new_image'] = './assets/upload/image/thumbs/';
$config['create_thumb'] = TRUE;
$config['quality'] = "100%";
$config['maintain_ratio'] = TRUE;
$config['width'] = 360; // Pixel
$config['height'] = 360; // Pixel
$config['thumb_marker'] = '';
$this->load->library('image_lib', $config);
$this->image_lib->resize();
// end create gambar
$inp = $this->input;
$data = array( 'id_gambar' => $id_gambar,
'judul_gambar' => $inp->post('judul_gambar'),
'gambar' => $upload_data['uploads']['file_name'],
);
$this->program_model->tambah_gambar($data);
$this->session->set_flashdata('sukses', 'Data gambar telah ditambah');
redirect(base_url('admin/program/gambar/'.$id_program),'refresh');
}}
$data = array( 'title' => 'Tambah Gambar Program: '.$program->nama_program,
'program' => $program,
'gambar' => $gambar,
'isi' => 'admin/program/gambar'
);
$this->load->view('admin/layout/wrapper', $data, FALSE);
}
Is this working for you ?
'id_gambar' => $gambar["id_gambar"];
$id_gambar is not defined.
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.
$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.
How can i delete image files from uploaded folder in codeigniter from delete button??
Can any one guide me ???
Here is my controller that upload image files
private function upload() {
$config['upload_path'] = 'assets/uploads/orginal/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|x-png';
$config['max_size'] = '500';
$config['max_width'] = '1600';
$config['max_height'] = '1200';
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ($this->upload->do_upload()) {
$img = $this->upload->data();
// create thumbnail
$new_image = 'assets/uploads/thumbs/' . $img['file_name'];
$c_img_lib = array(
'image_library' => 'gd2',
'source_image' => $img['full_path'],
'maintain_ratio' => TRUE,
'width' => 100,
'height' => 100,
'new_image' => $new_image
);
$this->load->library('image_lib', $c_img_lib);
$this->image_lib->resize();
} else {
$this->data['error'] = $this->upload->display_errors();
}
}
Use the unlink() function:
unlink($new_image);
http://pt1.php.net/manual/en/function.unlink.php
Hello I want to save the image to the database.. I written some code but it is not working fine. my code for controller is
function UploadImageView()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$imgdata['file']=$_FILES;
$this->load->model('Users');
/*$response=$this->Users->Image_upload();
header('Content-type: application/json');
echo json_encode($response);
*/
foreach($_FILES as $key => $value)
{
$this->upload->initialize($config);
if ( !$this->upload->do_upload($key))
{
//PARSE ERRORS
$error = array('error' => $this->upload->display_errors());
//var_dump($error);
$msg = $this->upload->display_errors('<p>', '</p>');
echo 'errore: '.$msg;
}
else
{
$this->load->model('Users');
$this->Users->Image_upload($key);
}
}
}
and my code for the model is as follows.
function Image_upload($value)
{
$this->filedata=$value;
$handle = fopen($this->filedata,"rb");
$img =fread($handle, filesize('$filedata'));
fclose($handle);
$img = base64_encode($img);
$data=array(
'image'=>$img
);
$flag=$this->db->insert('testimage',$data);
if($flag)
{
$this->db->where('image', $this->filedata);
$query = $this->db->get('testimage');
if ($query->num_rows() == 0)
{
$response['status'] = false;
$response['userId'] = -1;
$response['message'] = "Upload Failed!";
}
else
{
$result = $query->result();
$response['status'] = true;
$response['file'] = $this->filedata;
$response['message'] = "Success!";
}
}
return $response;
}
It returns error that
"fopen(Imgupload): failed to open stream: No such file or directory"
and
Message: filesize(): stat failed for $filedata
and
fread() expects parameter 1 to be resource, boolean given etc etc
so anybody help me to save image in my database.
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
Straight from the docs.
If you notice, they are sending the upload data to the view. So send it to the model instead:
$this->Users->Image_upload($this->upload->data());
Then your data is available in the array as is also explained in the docs:
$this->upload->data()
This is a helper function that returns an array containing all of the
data related to the file you uploaded. Here is the array prototype:
Array
(
[file_name] => mypic.jpg
[file_type] => image/jpeg
[file_path] => /path/to/your/upload/
[full_path] => /path/to/your/upload/jpg.jpg
[raw_name] => mypic
[orig_name] => mypic.jpg
[client_name] => mypic.jpg
[file_ext] => .jpg
[file_size] => 22.2
[is_image] => 1
[image_width] => 800
[image_height] => 600
[image_type] => jpeg
[image_size_str] => width="800" height="200"
)
So you can access the data in the model like so:
function Image_upload($data)
{
$handle = fopen($data['full_path'],'rb');
...