I am trying to resize multiple images using codeigniter, my file upload code in controller looks like below:
$this->load->library('upload');
$image = array();
$ImageCount = count($_FILES['pimage']['name']);
for($i = 0; $i < $ImageCount; $i++){
$_FILES['file']['name'] = $_FILES['pimage']['name'][$i];
$_FILES['file']['type'] = $_FILES['pimage']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['pimage']['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES['pimage']['error'][$i];
$_FILES['file']['size'] = $_FILES['pimage']['size'][$i];
// File upload configuration
$uploadPath = './uploads/products/';
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'jpg|jpeg|png|gif';
// Load and initialize upload library
$this->load->library('upload', $config);
$this->upload->initialize($config);
// Upload file to server
if($this->upload->do_upload('file')){
// Uploaded file data
$imageData = $this->upload->data();
$uploadImgData[] = $imageData['file_name'];
$this->resizeImage($imageData['file_name']);
}
}
public function resizeImage($filename)
{
$source_path = './uploads/products/' . $filename;
$target_path = './uploads/products/';
$config_manip = array(
'image_library' => 'gd2',
'source_image' => $source_path,
'new_image' => $target_path,
'maintain_ratio' => TRUE,
'width' => 500,
);
$this->load->library('image_lib', $config_manip);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
$this->image_lib->clear();
}
Here I have two issues, first one is only the first image is getting resized, multiple images are not. Second thing is: if portrait image is uploaded its turned into landscape after conversion. Can anyone please tell me what is wrong in here, thanks in advance
Related
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 have this controller which inserts the images to upload folder without resizing them.
public Function Upload() {
$this->load->library('upload');
$config['upload_path'] = FCPATH . 'uploads/property-images';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = 'property_image_1';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
for ($i = 0; $i < $number_of_files; $i++) {
$_FILES['uploadedimage']['name'] = $files['name'][$i];
$_FILES['uploadedimage']['type'] = $files['type'][$i];
$_FILES['uploadedimage']['tmp_name'] = $files['tmp_name'][$i];
$_FILES['uploadedimage']['error'] = $files['error'][$i];
$_FILES['uploadedimage']['size'] = $files['size'][$i];
$this->upload->initialize($config);
if ($this->upload->do_upload('uploadedimage', $i)) {
$data['uploadedimage'] = $this->upload->data();
$image_name[$i] = $data['uploadedimage']['file_name'];
//$this->_property_images_resize($data); // Private Function For Resize
$data['images'] = implode(',',$image_name);
$this->model_users->insert_property_details($data))
redirect('view');
} else {
$this->form_validation->set_message('fileupload_check', $this->upload->display_errors());
return FALSE;
}
}
}
I further needed to resize the images with the help of a private function in controller:
private function _property_images_resize($data) {
$this->load->library('image_lib');
$config = array(
'image_library' => 'gd2',
'source_image' => 'uploads/property-images/'.$data['uploadedimage']['file_name'],
'new_image' => 'uploads/profile/test1/',
'create_thumb' => TRUE,
'maintain_ratio' => TRUE,
'width' => 10,
'height' => 10
);
$this->image_lib->initialize($config);
if(!$this->image_lib->resize()) echo $this->image_lib->display_errors();
}
After adding this second function, it changes nothing. Images uploads the way they were before without resizing.
Looking for someone's help as can't figure it out.
For validation Callback
function if_image_was_selected(){
$this->form_validation->set_message('if_image_was_selected', 'Please select at least 1 image in jpg/jpeg/png/gif format.');
$number_of_files = count($_FILES['uploadedimages']['tmp_name']);
$files = $_FILES['uploadedimages'];
for($i=0;$i<$number_of_files;$i++) {
if($_FILES['uploadedimages']['error'][$i] != 0) {
return false;
}else{
return true;
}
}
}
Are you sure your not suppose to call:
$this->load->library('image_lib', $config);
instead of
$this->image_lib->initialize($config);
Does your webserver user have write permissions to the uploads/profile/test1/ directory?
Do you get any error message? Have you checked the PHP log or Apache log?
Try adding this code below, above your code to see the error echo'd out to the screen if you can.
error_reporting(E_ALL);
ini_set("display_errors", 1);
I am trying to make a simple app with Codeigniter where i need to store thumbnail image path in MySql database. The images along with the thumbnail versions upload correctly in the upload folder and I can store the image path in my database while uploading but i can not store the thumbnail image path in the database. I tried this in the controller:
$thumbnail= $this->resize($data['upload_data']['full_path'], $data['upload_data'] ['file_name']);
$thumbnail_path = './uploads/'. $thumbnail;
$newRow = array(
"title" => $this->input->post('title'),
"img_path" => $imgpath,
"thumbnail_path" => $thumbnail_path,
"post_body" => $this->input->post('post_body')
)
But it only stores "./uploads/" in the thumbnail_path column in my table, not the actual thumbnail path. Where i expect it to store something like "./uploads/Lighthouse_thumb.jpg". I can not figure out how to get the path of the thumbnail images to store them in the database. Here is my full upload controller:
class Upload extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('form');
}
function index() {
$this->load->view('upload_form', array('error'=>''));
}
function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
} else {
$image_data = $this->upload->data();
$imgpath = './uploads/' . $image_data['file_name'];
$data = array('upload_data'=>$this->upload->data());
$thumbnail= $this->resize($data['upload_data']['full_path'], $data['upload_data']['file_name']);
$thumbnail_path = './uploads/'. $thumbnail;
$newRow = array(
"title" => $this->input->post('title'),
"img_path" => $imgpath,
"thumbnail_path" => $thumbnail_path,
"post_body" => $this->input->post('post_body')
);
$this->load->model('postimage_path');
$this->postimage_path->insert($newRow);
$this->load->view('upload_success', $data);
}
}
function resize($path, $file) {
$config['image_library'] = 'gd2';
$config['source_image'] = $path;
$config['create_thumb'] = TRUE;
$config['maintian_ratio'] = TRUE;
$config['width'] = 100;
$config['height'] = 100;
$config['new_image'] = './uploads/' . $file;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
}
$thumbnail_path = './uploads/'. $thumbnail;
So thumbnail_path contains only "./uploads/" : that tells us that $thumbnail is false (or null or empty string etc.), meaning that the return value of the call to resize is wrong - it seems you expect it to return the file name.
function resize($path, $file) {
$config['image_library'] = 'gd2';
$config['source_image'] = $path;
$config['create_thumb'] = TRUE;
$config['maintian_ratio'] = TRUE;
$config['width'] = 100;
$config['height'] = 100;
$config['new_image'] = './uploads/' . $file;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
resize returns nothing! And you assign that nothing to $thumbnail_path. That's your problem.
You seem to have simply copied the code from the CodeIgniter docs. From said docs (emphasis mine):
The above code tells the image_resize function to look for an image called mypic.jpg located in the source_image folder, then create a thumbnail that is 75 X 50 pixels using the GD2 image_library. Since the maintain_ratio option is enabled, the thumb will be as close to the target width and height as possible while preserving the original aspect ratio. The thumbnail will be called mypic_thumb.jpg
So there you have it! If your filename is $data['upload_data']['file_name'], your thumbnail will be $data['upload_data']['file_name'] . "_thumb". Have a look at your file system, the files should be there for you to see.
So to fix your problem, this should do the trick:
$pathinfo = pathinfo($data['upload_data']['file_name']);
$thumbnail_path = './uploads/'. $data['upload_data']['file_name'] . "_thumb" . $pathinfo['extension'];
I am trying to resize the uploaded image to height of 163px maintaining aspect ratio and then upload it to a folder. I tried with the following code:
$id=1; // user id
$this->load->library('image_lib');
$filename=$_FILES['file']['name'];
$config['image_library'] = 'gd2';
$config['upload_path'] = './userdata/'.$id;
$config['height'] = '163px';
$config['maintain_ratio'] = TRUE;
//$config['master_dim'] = 'height';
$config['source_image'] = $filename;
$this->load->library('upload', $config);
$this->image_lib->initialize($config);
$this->image_lib->resize();
if(!$this->upload->do_upload('file'))
{
echo $this->data['error'] = $this->upload->display_errors();
}
However this is uploading the image to the correct folder but the image is not resized. I uploaded an image of size *170*128* and it is uploaded to folder as it is without resizing. What is wrong with my code?
Can anyone help me to find the problem?
Thanks in advance.
Try this cleaner version, the config for height or width doesn't need a px, hence your code is a little bit confusing :
$id = 1;
$config = array(
'upload_path' => './userdata/'.$id,
'allowed_types' => 'gif|jpg|jpeg|png',
'encrypt_name' => true,
);
$this->load->library('upload', $config);
$field_name = "file"; // change this with your file upload part's field name if different
if ($this->upload->do_upload($field_name)) {
$image = $this->upload->data();
$config = array(
'image_library' => 'gd2',
'source_image' => $image['full_path'],
'maintain_ratio' => true,
'height' => 163,
);
$this->load->library('image_lib', $config);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
$this->image_lib->clear();
}
Simple...
You have to do one thing Go to Captcha_helper.php and search
instead of
$x = mt_rand(0, $img_width / ($length / 3));
Do Like this
$x = mt_rand(0, $img_width / ($length / 1));
This is my upload model
function upload_avatar()
{
$id = $this->tank_auth->get_user_id();
//config upload parameters and upload image
$config = array(
'allowed_types' => 'jpeg|jpg|png',
'upload_path' => $this->upload_path,
'max_size' => 2048,
'encrypt_name' => TRUE,
'overwrite' => FALSE,
);
$this->load->library('upload', $config);
$this->upload->do_upload();
//get upload data, config, resize uploaded image, save in avatars subfolder
$image_data = $this->upload->data();
if ($image_data['file_size'] < 2048) {
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->upload_path . '/avatars',
'maintain_ratio' => TRUE,
'width' => 125,
'height' => 125
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
//only burn avatar path to user_profiles table if no upload errors
if (!$this->upload->display_errors()) {
$data = array('avatar' => base_url() . 'images/avatars/' . $image_data['file_name']);
$this->db->where('id', $id);
$this->db->update('user_profiles', $data);
}
//delete the original file from server
$this->load->helper('file');
unlink($image_data['full_path']);
} else {
echo $this->upload->display_errors();
}
}
I can't get the error message to echo straight to the browser when I try uploading a file > 2MB.
To be fair, CI ignores this large file, and uploads correctly when a file is < 2MB.
The only thing is that I can't get the error message to show on the front-end to give the suer some feedback.
Any ideas what's wrong here?
$config['upload_path'] = 'uploads/category/'.$id.'/';
//echo $file_name;die;
//echo $config['upload_path'];
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '2048';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$this->load->library('upload');
foreach ($_FILES as $key => $value) {
//print_r($key);
if (!empty($key['name'])) {
$this->upload->initialize($config);
if (!$this->upload->do_upload($key)) {
// echo 'test';die;
// rmdir('uploads/category/'.$id);
$errors = $this->upload->display_errors();
flashMsg($errors);
}
}
}
try this!!
Is your post_max_size limit less than 2MB? (http://ca3.php.net/manual/en/ini.core.php#ini.post-max-size) If so the file may have been discarded before your code is invoked.
Update:
If you take out your function call in the else block, and just drop in an exit('too big'); are you able to see errors then? If so there may be an issue with how you're pasing the call off.