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.
Related
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'>";
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!
Im trying to upload multiple files from a single form but for some reason i get "The upload path does not appear to be valid." I have checked permissions and they are correct, so i dont know if this is a problem with the images it self.. these are the functions i use to create the images
<input type="file" name="images[]" multiple id="img"></input>
function upload_image(){
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 2*1024;
$config['max_width'] = '1024';
$config['max_height'] = '1024';
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('images'))
{
$error = array('error' => $this->upload->display_errors());
return $error;
}
else
{
for($i = 0, $t = count($_FILES['images']); $i < $t; $i++) {
$id = $this->files->create_thumb($i);
}
}
}
function create_thumb($i){
$file = $this->upload->data();
$files = $file['file_name'];
$fields = array('files' => $files[$i]);
print_r($fields);
exit;
for ($i = 0; $i < count($files); $i++) {
$config['image_library'] = 'gd2';
$config['source_image'] = 'files/images/'.$files[$i];
$config['new_image'] = 'files/images/thumbs/'.$files[$i];
$config['thumb_marker'] = '';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 120;
$config['height'] = 120;
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
}
}
Move your config to config/upload.php. As an alternative solution, you can use the following line of code to initialize the config after loading the library:
$this->upload->initialize($config);
From the documentation:
If you prefer not to set preferences using the above method, you can instead put them into a config file. Simply create a new file called the upload.php, add the $config array in that file. Then save the file in: config/upload.php and it will be used automatically. You will NOT need to use the $this->upload->initialize function if you save your preferences in a config file.
To check if your directory is accessible by PHP, you can use is_dir():
var_dump(is_dir('/path/to/upload/folder/'));
Hope this helps!
I'm using Codeigniter's File Uploading Class for uploading user avatars. Is there a way to replace a user's image file whenever he/she uploads a new one? I want to replace an existing avatar with the newest one uploaded.
My image uploading controller
function upload_avatar()
{
$config['upload_path'] = './uploads/avatars/';
$config['allowed_types'] = 'jpg|png';
$config['overwrite'] = FALSE; //overwrite user avatar
$config['encrypt_name'] = TRUE;
$config['max_size'] = '200'; //in KB
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = $this->upload->display_errors();
$this->session->set_flashdata('error', $error);
redirect('/settings/avatar');
}
else
{
$config['image_library'] = 'gd2';
$config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 120;
$config['height'] = 120;
$this->load->library('image_lib', $config);
$this->image_lib->crop();
//Add image path to database
$avatar_path = 'uploads/avatars/' . $this->upload->file_name;
$user_id = $this->tank_auth->get_user_id();
$this->Settings_model->update_avatar($avatar_path, $user_id);
$this->session->set_flashdata('success', 'Avatar updated!');
redirect('/settings/avatar');
}
}
There is a public attribute overwrite that dictates the decision to overwrite the original file. By default, a new filename is created based on the original. Here's the source from Upload.php in CI:
/*
* Validate the file name
* This function appends an number onto the end of
* the file if one with the same name already exists.
* If it returns false there was a problem.
*/
$this->orig_name = $this->file_name;
if ($this->overwrite == FALSE)
{
$this->file_name = $this->set_filename($this->upload_path, $this->file_name);
if ($this->file_name === FALSE)
{
return FALSE;
}
}
So all you need to do to get the overwrite working is:
$this->load->library('upload', $config);
$this->upload->overwrite = true;
Simple set "overide" be true in your config.
$this->upload->initialize(array(
"upload_path"=>$path,
"allowed_types"=>"jpg|png|jpeg",
"overwrite"=>true
));
did you try to change
$config['overwrite'] = FALSE;
to
$config['overwrite'] = TRUE;