I am trying to do flip operation in CodeIgniter
I have found a code for image flip in php using codeigniter libraries
First I uploaded image to a path, here is the coding of controller for this purpose:
$config['upload_path'] = 'D:/xampp/htdocs/ImageTools/assets/images/pages/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->image_lib->clear();
$this->load->library('upload',$config);
if(!$this->upload->do_upload()){
$errors = array('error' => $this->upload->display_errors());
$post_image='noimage.jpg';
}else{
$file_data = $this->upload->data();
$data['img'] = base_url().'assets/images/pages/'.$file_data['file_name'];
$this->load->view('pages/inverted',$data);
I tried to do the invert operation here but couldn't succeeded then tried to invert the uploaded image, done the coding in view file by passing this uploaded image to view:
<?php $img;?>
<?php
$this->image_lib->clear();
$config=array();
$config['image_library'] = 'gd2';
$config['source_image'] = $img;
$config['create_thumb'] = TRUE;
$config['rotation_angle'] = 'hor';
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->load->library('upload',$config);
$this->image_lib->rotate();
if ( ! $this->image_lib->rotate())
{
echo $this->image_lib->display_errors();
}
if(!$this->upload->do_upload()){
$errors = array('error' => $this->upload->display_errors());
$post_image='noimage.jpg';
}
else{
$file_data = $this->upload->data();
$data = base_url().'/assets/images/pages/'.$file_data['file_name'];
}
?>
First off D:/xampp/htdocs/ImageTools/assets/images/pages/ will never be valid in a server environment. There is a way to make it valid for all environments like so:
$config['upload_path'] = './ImageTools/assets/images/pages/';
(there are other ways but this is the one in the CI docs)
If the folder ImageTools is actually imagetools then that needs to be used in the above example; Linux is case sensitive.
Therefore, following the docs, you can do:
$upload['upload_path'] = './ImageTools/assets/images/pages/';
$upload['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $upload);
if (!$this->upload->do_upload('userfile')) {
show_error($this->upload->display_errors());
}
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['source_image'] = $this->upload->data('full_path');
$config['rotation_angle'] = 'hor'; // or whatever
$this->image_lib->initialize($config);
if (!$this->image_lib->rotate()) {
show_error($this->image_lib->display_errors());
}
$img_path = base_url() . '/assets/images/pages/' . $this->upload->data('file_name');
echo "<img src='{$img_path}' width='auto' height='200'>";
Related
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.
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 tried to upload two images using a form. To do that I wrote a image function.
protected function imageUploadResize($filename, $field_name, $width, $height, $maker){
// thumb image upload settings
$config['upload_path'] = './uploads/packages/';
$config['allowed_types'] = 'gif|jpg|png';
// thumb upload settings
$image_new_name = time().'_'.$filename;
$config['file_name'] = $image_new_name;
$this->load->library('upload', $config);
// upload thumb image
if ( $this->upload->do_upload($field_name)) {
// resize uploaded file
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/packages/'.$image_new_name;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = $width;
$config['height'] = $height;
$config['thumb_marker'] = $maker;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$error = array('error' => $this->upload->display_errors());
} else {
$imageData = array('upload_data' => $this->upload->data());
}
return $image_new_name;
}
And I access that function this way,
// allocate image name to data set
if(!empty($_FILES["thumb_image"]['name'])){
// thumb upload
$thumb_image_new_name = $this->imageUploadResize($_FILES["thumb_image"]['name'],'thumb_image',THUMB_IMAGE_WIDTH, THUMB_IMAGE_HEIGHT, '_thumb');
$data['thumb_image'] = $thumb_image_new_name;
}else{
$data['thumb_image'] = "";
}
// allocate image name to data set
if(!empty($_FILES["banner_image"]['name'])){
// banner upload
$banner_image_new_name = $this->imageUploadResize($_FILES["banner_image"]['name'],'banner_image',BANNER_IMAGE_WIDTH, BANNER_IMAGE_HEIGHT, '_banner');
$data['banner_image'] = $banner_image_new_name;
}else{
$data['banner_image'] = "";
}
When I upload one image (thumb_image or banner_image) above function is worked properly.
But when I upload both images thumb_image uploaded properly but banner_image did not upload properly. As a example assume I am going to upload Hydrangeas.jpg and Desert.jpg. Then it is working this way,
Then file uploaded this way (images names of the folder),
1493025280_Hydrangeas.jpg
1493025280_Hydrangeas_thumb.jpg
1493025280_Hydrangeas1.jpg - image name also wrong
But expected output is (images names of the folder),
1493025280_Hydrangeas.jpg
1493025280_Hydrangeas_thumb.jpg
1493025280_Desert.jpg
1493025280_Desert_banner.jpg
Can someone please help me, thank you..
A different approach that i use for file uploads is below.
I rearrange the $_FILES array because the original has some difficulties with keys and objects.
$files = array();
foreach ($_FILES['files']['name'] as $num_key => $dummy) {
foreach ($_FILES['files'] as $txt_key => $dummy) {
$files[$num_key][$txt_key] = $_FILES['files'][$txt_key][$num_key];
}
}
In new array, loop through each image and do what you need:
foreach ($files as $file) {
if ($file['error'] == 0) {
//Your code here...
//Call your function imageUploadResize
}
}
Finally found the solution, In codeigniter when upload image, we have to call,
$this->load->library('upload', $config);
Then we have to initialized the configurations.
$this->upload->initialize($config);
And when resize image we have to call,
$this->load->library('image_lib', $config);
Then have to initialized the configurations.
$this->image_lib->initialize($config);
Therefore completed function is,
protected function imageUploadResize($filename, $field_name, $width, $height, $maker){
// thumb image upload settings
$config['upload_path'] = './uploads/packages/';
$config['allowed_types'] = 'gif|jpg|png';
// thumb upload settings
$image_new_name = time().'_'.$filename;
$config['file_name'] = $image_new_name;
$this->load->library('upload', $config);
$this->upload->initialize($config);
// upload thumb image
if ( $this->upload->do_upload($field_name)) {
// resize uploaded file
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/packages/'.$image_new_name;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = $width;
$config['height'] = $height;
$config['thumb_marker'] = $maker;
$this->load->library('image_lib', $config);
$this->image_lib->initialize($config);
$this->image_lib->resize();
$error = array('error' => $this->upload->display_errors());
} else {
$imageData = array('upload_data' => $this->upload->data());
}
return $image_new_name;
}
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!
I iterate through $_FILES to create thumb images of uploaded pictures. It works fine for the first image but fails for the following pictures. Do I miss to add a special line or there is flow in my code?
Note: Original files gets uploaded successfully and exist in folder before creating thumb out of them.
When I echo error, I get this: "Your server does not support the GD function required to process this type of image.". When I upload it on its own, it works!!!!
Thanks
public function upload_image()
{
$config['upload_path'] = './web/uploads/images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 5120;
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['encrypt_name'] = true;
$this->load->library('upload');
$this->upload->initialize($config);
foreach ($_FILES as $file => $value)
{
$this->upload->do_upload($file);
$result = $this->upload->data();
if ($this->manipulate_image($result['file_name']) === false)
{
echo 'Failed to create thumb for the image ' . $value['name'] . '<br />';
}
}
}
public function manipulate_image($file_name)
{
$config['image_library'] = 'gd2';
$config['source_image'] = './web/uploads/images/' . $file_name;
$config['create_thumb'] = true;
$config['maintain_ratio'] = false;
$config['width'] = 100;
$config['height'] = 100;
//$config['master_dim'] = 'width';
$config['thumb_marker'] = '_thumb';
$this->load->library('image_lib', $config);
if (! $this->image_lib->resize())
{
$this->image_lib->clear();
return false;
}
$this->image_lib->clear();
return true;
}
I see two things, first, I would move the load on the library outside of the foreach loop then use initialize inside the loop to set the config:
$this->image_lib->initialize($config);
Also, as documented here you can use
echo $this->image_lib->display_errors();
To get more insight on your problem
Apparently loading loading image_lib multiple times in a loop causes this problem.
Solved without including in loop.