Upload array of files with thumbnail - php

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!

Related

setting width and height of image in codeigniter php not working

i have a codeigniter website where user can upload multiple images, i want all the images to be of the same size, i have done the following code in controller:
if (isset($_POST['addblog'])) {
$this->load->library('upload');
$image = array();
$ImageCount = count($_FILES['image_name']['name']);
for ($i = 0; $i < $ImageCount; $i++) {
$_FILES['file']['name'] = $_FILES['image_name']['name'][$i];
$_FILES['file']['type'] = $_FILES['image_name']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['image_name']['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES['image_name']['error'][$i];
$_FILES['file']['size'] = $_FILES['image_name']['size'][$i];
$uploadPath = './uploads/blog/';
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['width'] = 200;
$config['height'] = 250;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload('file')) {
$imageData = $this->upload->data();
$uploadImgData[] = $imageData['file_name'];
}
}
$blogimage = $uploadImgData;
}
as u can see i have set the height and width in config, the images are still uploaded in ther original size, can anyone please tell me what is wrong in here, thanks in advance
You has just made a little mistake in your config :
$config['max_width'] = 200;
$config['max_height'] = 250;
Sadly "min_width" and "min_height" seems doesn't exist yet

Codeigniter image compression not working

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';

Codeigniter - Two images upload did not work

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;
}

Failed to create thumb image in $_FILES loop

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.

Codeigniter -> Image Uploading Part 2 -> Multiple Images

I am trying to create a multiple image uploader and I have come across this link. What I am confused about in relation to my code below and the link is that do I have to have 2
$this->upload->do_upload(); functions to run my code or how do I use
$this->upload->initialize($config); in the below situation?
Code:
//Image Upload Function
$conceptOne = 'conceptOne';
$conceptTwo = 'conceptTwo';
$location = $_SERVER['DOCUMENT_ROOT'].'/_assets/quote/uploads/';
$folderName = $this->quote->getCompanyDetails()->companyName;
$folderName = str_replace(" ", "_", $folderName);
$folderName = strtolower($folderName);
if(!is_dir($location.$folderName))
{
mkdir($location.$folderName);
chmod($location.$folderName, 0777);
//Set File Settings
$config['upload_path'] = $location.$folderName;
$config['allowed_types'] = 'jpg|png|pdf';
$config['file_name'] = $conceptOne;
$config['remove_spaces'] = TRUE;
$config['overwrite'] = TRUE;
$config['max_size'] = '1024';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
print_r($config);
if(!$this->upload->do_upload($conceptOne)) { #= try upload
$data['uploadError'] = array('uploadError' => $this->upload->display_errors()); #Error
$this->load->view('layout', $data);
} // Do upload
else{
$data = array('upload_data' => $this->upload->data($conceptOne));
}// end else
}// end if folder
You need a loop to reinitialize the file upload library so that you can process some other images that are uploaded by the user.
Let's say a user uploaded 2 images. Then that means you need to put the code that initialize the file upload library and do the file upload inside that loop.
for ($i = 0; $i < 2; $i++)
{
// Change the config here if necessary
$this->upload->initialize($config);
// Call do_upload() here
}
Kemal is right: you have to iterate over the files you have. I would put the "concepts" in an array, so you can use foreach:
// Load upload library without any configuration
$this->load->library('upload');
$concepts = array('conceptOne','conceptTwo');
$location = $_SERVER['DOCUMENT_ROOT'].'/_assets/quote/uploads/';
$folderName = $this->quote->getCompanyDetails()->companyName;
$folderName = str_replace(" ", "_", $folderName);
$folderName = strtolower($folderName);
if(!is_dir($location.$folderName))
{
mkdir($location.$folderName);
chmod($location.$folderName, 0777);
}
$config['upload_path'] = $location.$folderName;
$config['allowed_types'] = 'jpg|png|pdf';
$config['remove_spaces'] = TRUE;
$config['overwrite'] = TRUE;
$config['max_size'] = '1024';
$config['max_width'] = '1024';
$config['max_height'] = '768';
// Upload 'concepts'
foreach($concepts as $concept)
{
$config['file_name'] = $concept;
$this->upload->initialize($config);
$this->upload->do_upload($concept);
}
// Upload logo
$config['file_name'] = 'logo-filename.gif';
$this->upload->initialize($config);
$this->upload->do_upload('logo');

Categories