I have the code below, if i don't use watermark it's fine but when i use watermark, both the original pics and thumbnail become very small and , watermark happens on thumbnail, I want watermark on the original image, pls help
//UPLOAD IMAGE
//some $config vars for image
$config['upload_path'] = './images/blog';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '0';
$config['remove_spaces'] = true;
$config['overwrite'] = false;
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
//upload main image
if(!$this->upload->do_upload('photo')){
$e = $this->upload->display_errors();
print_r($e);
}
$image = $this->upload->data();
//thumbnail creation start
$config1['image_library'] = 'gd2';
$config1['source_image'] = $image['full_path'];
$config1['create_thumb'] = TRUE;
$config1['maintain_ratio'] = TRUE;
$config1['width'] = 75;
$config1['height'] = 50;
$this->load->library('image_lib', $config1);
$this->image_lib->resize();
//thumbnail creation ends
$this->image_lib->clear();
//$image = $this->upload->data();
//start watermarking
$config2['source_image'] = $image['full_path'];
$config2['wm_text'] = 'Copyright 2011 myself';
$config2['wm_type'] = 'text';
$config2['wm_font_path'] = './system/fonts/FromWhereYouAre.ttf';
$config2['wm_font_size'] = '16';
$config2['wm_font_color'] = 'ffffff';
$config2['wm_vrt_alignment'] = 'middle';
$config2['wm_hor_alignment'] = 'center';
$config2['wm_padding'] = '20';
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->image_lib->watermark();
//end watermarking
please try adding
$config2['new_image'] = '';
in the watermarking code
Related
I'm setting up the upload file before add resize code. And its normally process, but after adding resize code my controller isn't working. Can you help me with this? I'm trying but it didn't work.
And my another problem, I can't upload image file size more than 2MB.
This for my work on online shop.
public function submit_image()
{
$input_name = $_POST['input_name'];
$input_email = $_POST['input_email'];
$input_code_transaction = $_POST['input_code_transaction'];
$config['file_name'] = $input_code_transaction;
$config['overwrite'] = TRUE;
$config['upload_path'] = './img/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 50000000;
$config['max_width'] = 6000;
$config['max_height'] = 4000;
$this->upload->initialize($config);
if(!empty($_FILES['filefoto']['name'])) {
if ($this->upload->do_upload('doc')) {
$gbr = $this->upload->data();
//Compress Image
$config['image_library']='gd2';
$config['source_image']='./img/'.$gbr['file_name'];
$config['create_thumb']= FALSE;
$config['maintain_ratio']= FALSE;
$config['quality']= '50%';
$config['width']= 1280;
$config['height']= 720;
$config['new_image']= './img/'.$gbr['file_name'];
$this->load->library('upload', $config);
$this->upload->resize();
$data = $upload_data = $this->upload->data();
$input_picture = $this->upload->do_upload('doc');
}
}
}
The upload library has no inbuilt features for image resizing.
This whole section:
$config['image_library']='gd2';
$config['source_image']='./img/'.$gbr['file_name'];
$config['create_thumb']= FALSE;
$config['maintain_ratio']= FALSE;
$config['quality']= '50%';
$config['width']= 1280;
$config['height']= 720;
$config['new_image']= './img/'.$gbr['file_name'];
$this->load->library('upload', $config);
$this->upload->resize();
$data = $upload_data = $this->upload->data();
$input_gambar = $this->upload->do_upload('doc');
needs to reference image_lib the image resizer codeigniter uses: https://www.codeigniter.com/user_guide/libraries/image_lib.html
So:
if ($this->upload->do_upload('doc')) {
$gbr = $this->upload->data();
//Compress Image
$imlib['image_library'] = 'gd2';
$imlib['source_image'] = './img/' . $gbr['file_name'];
$imlib['create_thumb'] = FALSE;
$imlib['maintain_ratio'] = FALSE;
$imlib['quality'] = '50%';
$imlib['width'] = 1280;
$imlib['height'] = 720;
$imlib['new_image'] = './img/resized_' . $gbr['file_name'];
$this->load->library('image_lib', $imlib);
if (!$this->image_lib->resize()) {
show_error($this->image_lib->display_errors());
}
$yourouputimage = './img/resized_' . $gbr['file_name'];
}
currently I'm working with codeigniter framework but I'm confused how to resize the image. I have followed the documentation but I'm still confused. This is my code
public function create(){
//Check login
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$data['title'] = 'Create Post';
$data['categories'] = $this->BlogModel->get_categories();
$this->form_validation->set_rules('title','title','required');
$this->form_validation->set_rules('content','content','required');
if($this->form_validation->run() === FALSE){
$this->load->view('frontend/header');
$this->load->view('frontend/navbar');
$this->load->view('frontend/blog/create',$data);
$this->load->view('frontend/footer');
} else{
//Upload Image
$config['upload_path'] = './assets/images/posts/blog';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if(!$this->upload->do_upload()){
$errors = array('error' => $this->upload->display_errors());
$post_image = 'noimage.jpg';
}else{
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->BlogModel->create_post($post_image);
//set message
$this->session->set_flashdata('post_created','Your post has been created');
redirect('blog');
}
}
Please advice how to resize the image.
Thanks!
If you want to do different things like resize the original image directly, rather than creating a thumb set create_thumb to false, and do not use new_image. The only difference between create_thumb and new_image is that new_image allows you to specify your own path and name. Whereas the most you can do with create_thumb is change the thumb_marker. Which is all in the docs...
//Upload Image
$config['upload_path'] = './assets/images/posts/blog';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
//$errors = array('error' => $this->upload->display_errors());
exit($this->upload->display_errors());
$post_image = 'noimage.jpg';
} else {
$up_data = $this->upload->data();
$post_image = $up_data['name'];
$config = array(); // clear prev config
$config['image_library'] = 'gd2';
$config['source_image'] = $up_data['full_path'];
$config['create_thumb'] = TRUE;
$config['thumb_marker'] = '_thumbnail';
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
if(!$this->image_lib->resize()) {
exit($this->image_lib->display_errors());
}
$thumb_path = $up_data['file_path'] . $up_data['raw_name'] . $config['thumb_marker'] . $up_data['file_ext'];
}
The exits you can remove later, but leave them for debugging until you figure out a way to properly message your errors (I suggest session vars). These functions should also be moved to a model.
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['maintain_ratio'] = TRUE;
$config['width'] = 100;
$config['height'] = 150;
$config['file_name'] = $_FILES['image']['name'];
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->initialize($config);
$this->load->library('upload',$config);
$this->upload->initialize($config);
I am unable to resize image ... please give me my mistake
As per the #jigar Shah comment you have to first initalize upload library before resize
$this->load->library('upload');
$config['upload_path'] = './uploads';
$config['allowed_types'] ='csv|xls|xlsx';
$config['max_size'] = 1024; //in KB
$config['overwrite'] = TRUE;
$config['encrypt_name'] = TRUE;
$config['max_filename'] = 25;
$this->upload->initialize($config);
if (!$this->upload->do_upload('image_name')) {
//if file upload failed then catch the errors
$this->handle_error($this->upload->display_errors());
$is_file_error = TRUE;
} else {
//store the file info
$image_data = $this->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = $image_data['full_path']; //get original image
$config['maintain_ratio'] = TRUE;
$config['width'] = 150;
$config['height'] = 100;
$this->load->library('image_lib', $config);
if (!$this->image_lib->resize()) {
$this->handle_error($this->image_lib->display_errors());
}
}
For more reference please visit
I want to upload large images on my website. I want to reduce the size of those using codeigniter. So I am doing this code
function upload_image($data) {
$config['upload_path'] = './temp/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 10000;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('image')) {
$error = array('error' => $this->upload->display_errors());
pre($error);
} else {
$config = array();
$data = array('upload_data' => $this->upload->data());
$config['image_library'] = 'gd';
$config['source_image'] = './temp/' . $data['upload_data']['file_name'];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['quality'] = 50;
$config['new_image'] = './temp/' . $data['upload_data']['file_name'];
$this->load->library('image_lib', $config);
$this->image_lib->resize();
pre($data);
}
}
But images are not being compressed. Original and uploaded size are the same. Where am I wrong?
Maybe exists an error. Check for errors:
if (!$this->image_lib->resize()){
echo $this->image_lib->display_errors();
}
Note: Use gd2 as library (default value is gd2):
$config['image_library'] = 'gd2';
I'm trying to upload an image and everything is working fine, except the width and heigh of the resize. the strange part is, if I add px on the $config_img['width'] and $config_img['height'] I got an error saing:
"A non well formed numeric value encountered
Filename: libraries/Image_lib.php"
What is obvious because the px on the value, but this way the image do resize.
Here is my upload image function:
function upload_foto_acompanhe_sua_obra($field) {
$dir = realpath('assets/uploads/acompanhe_sua_obra');
$config['upload_path'] = $dir;
$config['allowed_types'] = 'gif|jpg|png';
$config['encrypt_name'] = TRUE;
$config['max_size'] = '500000';
$config['max_width'] = '10024';
$config['max_height'] = '7068';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$field_name = $field;
if ($this->upload->do_upload($field_name)) {
$dados = $this->upload->data();
$this->load->library('image_lib');
$this->image_lib->clear();
$size = getimagesize($dados['full_path']);
$config_img['image_library'] = 'GD2';
$config_img['source_image'] = $dados['full_path'];
$config_img['create_thumb'] = FALSE;
$config_img['maintain_ratio'] = TRUE;
$config_img['encrypt_name'] = TRUE;
$config_img['width'] = '1092px';
$config_img['height'] = '295px';
$this->image_lib->initialize($config_img);
$this->image_lib->resize();
// Returns the photo name
return $dados['file_name'];
} else {
$error = array('error' => $this->upload->display_errors());
return $error;
}
}
That's it, any help will be appreciated! Thanks!