create image thumbnail with php codeigniter - php

I am working on a codeigniter project where users can create profiles and upload their images..I want to create an image thumbnail (200 X 200) and save it in the directory. My code works perfectly as it uploads the image and also stores the image name in database but the problem is it does not resize the image..I want to resize all images (200 X 200) and then store them in directory.
This is controller
public function FunctionName()
{
$fname = ucwords($this->input->post('fname'));
$lname = ucwords($this->input->post('lname'));
$prof_pic = $_FILES['profile_pic']['name'];
$config = array ('upload_path' => './images/students/',
'allowed_types' => "jpeg|jpg|png",
'overwrite' => TRUE,
'file_name' => $Maxtype."_".$fname."_".$lname,
'remove_spaces' => TRUE,
'image_library' => gd2,
'source_image' => $prof_pic,
'new_image' => './images/students/',
'maintain_ratio' => TRUE,
'height' => 200,
'width' => 200
);
$this->load->library('upload', $config);
$this->upload->do_upload('profile_pic');
$extension = pathinfo($prof_pic);
$ext = $extension['extension'];
$image = $config['file_name'].".".$ext;
$data = array('std_fname' => $fname,
'std_lname' => $lname,
'profile_pic' => $image
);
}

Related

Codeigniter Image Upload PathInfo Extension 'The filetype you are attempting to upload is not allowed'

I am uploading multiple images and I need to get their different file format.
But the problem is i only got the first file image format and there's an error when I do resize images because of the incorrect file format.
I am having an error The filetype you are attempting to upload is not allowed. which occur when I put the PATHINFO_EXTENSION when getting file.
Here's my code
for($i = 0; $i < $count_upload; $i++) {
$_FILES['userfile']['name'] = pathinfo($_FILES['product_image']['name'][$i], PATHINFO_EXTENSION);
$_FILES['userfile']['type'] = $_FILES['product_image']['type'][$i];
$_FILES['userfile']['tmp_name'] = $_FILES['product_image']['tmp_name'][$i];
$_FILES['userfile']['error'] = $_FILES['product_image']['error'][$i];
$_FILES['userfile']['size'] = $_FILES['product_image']['size'][$i];
$file_name = $file_name;
$config = array(
'file_name' => $file_name,
'allowed_types' => 'jpg|jpeg|png',
'max_size' => 3000,
'overwrite' => TRUE,
'encrypt_name' => FALSE,
'remove_spaces' => TRUE,
'upload_path'
=> $directory
// => './assets/img/products'
);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$response = array(
'status' => false,
'environment' => ENVIRONMENT,
'message' => $error['error'],
'csrf_name' => $this->security->get_csrf_token_name(),
'csrf_hash' => $this->security->get_csrf_hash()
);
echo json_encode($response);
die();
}else {
$file_name = $this->upload->data()['file_name'];
echo $file_name;
echo '<br>';

what is the way to reduce the size of image without reducing height and width in codeigniter?

I'm trying to create image upload functionality in Codeigniter. While uploading a file I want to maintain the height and width but the size should be reduced for example from 10kb to 5 kb.
The below code is working fine, the file is getting uploaded but the size is the same.
what changes can be done to make it correct
if($_FILES['logo']['name'] != '')
{
$getimagesize = getimagesize($_FILES['logo']['tmp_name']);
$w = $getimagesize[0];
$h = $getimagesize[1];
#NEW CODE
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'jpg|jpeg|png';
$this->load->library('upload',$config);
if(!$this->upload->do_upload('logo'))
{
echo $this->upload->display_errors();
}#EOF if(!$this->upload->do_upload('logo'))
else
{
$data = $this->upload->data();
$config = [
'image_library' => 'gd',
'source_image' => './upload/'.$data['file_name'],
'create_thumb' => FALSE,
'maintain_ratio' => FALSE,
'quality' => '60',
'height' => $h,
'width' => $w,
'new_image' => './upload/'.$data['file_name']
];
$this->load->library('image_lib',$config);
if($this->image_lib->resize())
{
$this->session->set_flashdata('success','image saved');
redirect('welcome/school_details');
}
#Adding data into databse
}
Change config upload:
'quality' => '60'
to
'quality' => '60%'

change image name and store it in database with php codeigniter

I working on a project with PHP Codeigniter. I want to upload an image, change the name of the image and store it in database (with extension). I have written the following code. It works perfectly but the problem is that the image name is stored without its extension in database. For example, when I run the code, the name that is stored in database is 'imagename', instead of 'imagename.jpg' or 'imagename.png'. I want to store the complete name with extension.
This is my Controller
public function FunctionName()
{
$fname = ucwords($this->input->post('fname'));
$lname = ucwords($this->input->post('lname'));
$prof_pic = $_FILES['profile_pic']['name'];
$config = array ('upload_path' => './images/students/',
'allowed_types' => "jpeg|jpg|png",
'overwrite' => TRUE,
'file_name' => $fname."_".$lname,
'remove_spaces' => TRUE,
'file_ext_tolower' => TRUE
);
$this->load->library('upload', $config);
$this->upload->do_upload('profile_pic');
$image_name = $config['file_name'].".".$config['file_type'];
$data = array('std_fname' => $std_fname,
'std_lname' => $std_lname,
'profile_pic' => $image
);
$this->General_Model->create_record($data, "table_name");
}
To get uploaded file name use
$this->upload->data('file_name');
AS
$this->upload->do_upload('profile_pic');
$image_name = $this->upload->data('file_name');
Read Information about the uploaded file
Try with the below code
$prof_pic = $_FILES['profile_pic']['name'];
$newName = "<Desired name>".".".pathinfo($prof_pic, PATHINFO_EXTENSION);
$config = array ('upload_path' => './images/students/',
'allowed_types' => "jpeg|jpg|png",
'overwrite' => TRUE,
'file_name' => $newName,
'remove_spaces' => TRUE,
'file_ext_tolower' => TRUE
);
the line
$newName = "<Desired name>".".".pathinfo($prof_pic, PATHINFO_EXTENSION);
Will assign the new name with the file extension of the actual file

Codeigniter profile picture resize

I'm currently struggling with the auto resizing of an oversize image.
I want 2 things to happen, #1 that the image automatically resizes to the given dimensions and 2 that it generates a thumb that can be used for example in the navbar etc..
This is the current code I got
EDITED Today
// avatar upload
if (isset($_FILES['userfile']['name']) && !empty($_FILES['userfile']['name'])) {
function __construct(){
parent::__construct();
//return the full path of the directory
//make sure these directories have read and write permessions
$this->original_path = realpath(APPPATH.'../uploads/original');
$this->resized_path = realpath(APPPATH.'../uploads/resized');
$this->thumbs_path = realpath(APPPATH.'../uploads/thumbs');
}
$this->load->library('image_lib');
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png', //only accept these file types
'max_size' => 2048, //2MB max
'upload_path' => $this->original_path, //upload directory
'width' => 250,
'height' => 250
);
$this->load->library('upload', $config);
$image_data = $this->upload->data(); //upload the image
//your desired config for the resize() function
$config = array(
'source_image' => $image_data['full_path'], //path to the uploaded image
'new_image' => $this->resized_path, //path to
'maintain_ratio' => true,
'width' => 128,
'height' => 128
);
//this is the magic line that enables you generate multiple thumbnails
//you have to call the initialize() function each time you call the resize()
//otherwise it will not work and only generate one thumbnail
$this->image_lib->initialize($config);
$this->image_lib->resize();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->thumbs_path,
'maintain_ratio' => true,
'width' => 36,
'height' => 36
);
//here is the second thumbnail, notice the call for the initialize() function again
$this->image_lib->initialize($config);
$this->image_lib->resize();
}
But Now I get these errors in browser
https://gyazo.com/4ec7360df1d3cc2703d19941583982fd
Any help would be appreciated

How to code multiple file upload using CodeIgniter?

I want to have a multiple file upload code.
For example:
Koala.jpg
Penguins.jpg
Jellyfish.jpg
There is input text where the user can set the new name of the image.
The user will now upload the images and the inputted text for new image name is "Animals"
Now, what I want is when this uploaded the output should be Animals1.jpg, Animals2.jpg, Animals3.jpg.
The problem is when I tried to upload all these images, only one image is uploading.
I tried to make research and applied some codes on my program, but still not working.
Controller
public function do_upload() {
$config = array(
'image_library' => 'gd2',
'file_name' => $this->input->post('finame'),
'upload_path' => './public/img/uploads',
'upload_url' => base_url().'public/img/uploads',
'allowed_types' => 'gif|jpg|jpeg',
'max_size' => '1024KB',
'max_width' => '1024',
'max_height' => '768',
'maintain_ratio'=> TRUE,
'overwrite' => false,
);
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$error_msg = "<div class='alert alert-error'>".$this->upload->display_errors()."</div>";
$error = array('error' => $error_msg);
}
else {
$upload_data = $this->upload->data();
$data['thumbnail_name'] = $upload_data['raw_name']. '_thumb' .$upload_data['file_ext'];
$file_array = array(
'image' => $data['thumbnail_name'],
'image_name' => $upload_data['file_name'],
//'description' => "",
'date_created' => date('Y-m-d H:i:s', now()),
'date_modified' => date('Y-m-d H:i:s', now()),
'author' => $this->session->userdata('username'),
'size' => $upload_data['file_size'],
'type' => $upload_data['image_type'],
'width' => $upload_data['image_width'],
'height' => $upload_data['image_height'],
//'document_name' => $field,
//'department' => $field2,
//'notes' => "",
);
$this->session->set_userdata('image_print', $file_array);
$this->load->database();
$this->db->insert('tbl_image', $file_array);
$data = array('upload_data' => $this->upload->data());
$user_level['records']=$this->user_model->get_records();
$this->load->view('v_dashboard/page/header_view', $user_level);
$this->load->view('v_document/upload/upload_result_view', $data);
$this->load->view('v_dashboard/page/footer_view');
}
}
I have this on my HTML
<label for="file"><strong>Select File To Upload:</strong></label>
<input type="file" name="userfile[]" multiple class="btn transcolor btn-file"/>
<br/><br/>
By the way, I'm using BLOB on my database.
I tried to refer to this links
Ellislab
GitHub
StackOverflow
StackOverflow
CodingLikeASir
You have to run the for loop till the count of the uploaded image file.
Like this:
for ($i=0; $i < count($_FILES['userfile']['name']); $i++)
{
// function to add the image name one by one into database.
}

Categories