Update: Complete Controller
the code below only produces the thumbnail of the first image and successfully uploads all the other images. the problem that i am facing is that the other images do get uploaded but their thumbnails aren't.
<?php
class Upload extends CI_Controller{
function __construct()
{
parent::__construct();
$this->upload_path = realpath(APPPATH . '../uploads');
}
function index()
{
$this->load->view('upload_view', array('error' => ''));
}
function start()
{
// var_dump($_FILES); die();
$config['upload_path'] = $this->upload_path;
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$this->load->library('upload', $config);
$status = $this->upload->do_multi_upload();
for($i=0;$i<count($status);$i++)
{
$conf = array(
'source_image' => $status[$i]['full_path'],
'new_image' => $this->upload_path . '/thumbs',
'maintain_ratio' => true,
'width' => 200,
'height' => 200
);
$this->load->library('image_lib', $conf);
$this->image_lib->resize();
$this->image_lib->clear();
}
if (!$status)
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_view', $error);
}
else
{
var_dump($status);
}
}
}
?>
Load the library prior to starting the loop and without passing parameters to it.
Then, for each of the elements in $status do this:
$this->image_lib->initialize($conf);
... and you don't need to use clear() afterwards.
Related
I have a form where some image file fields are, submitting all those value to my controller,
After Receiving all fields i'm able to resize only one field of image file rest of image fields are not going to resizing.
Here is my view
<input type="file" name="image" size="20" />
<input type="file" name="image2" size="20" />
<input type="submit" value="upload" />
here is my controller
public function resizeImage($filename){
$source_path = './upload/' . $filename;
$target_path = './upload/';
$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();
}
public function uploadImage() {
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1024;
$this->load->library('upload', $config);
if ($this->upload->do_upload('image')) {
$error = array('error' => $this->upload->display_errors());
$uploadedImage = $this->upload->data();
$this->resizeImage($uploadedImage['file_name']);
print_r('Image Uploaded Successfully.');
/*exit;*/
}
if ($this->upload->do_upload('image2')) {
$error = array('error' => $this->upload->display_errors());
$uploadedImage = $this->upload->data();
$this->resizeImage($uploadedImage['file_name']);
print_r('Image Uploaded Successfully.');
/*exit;*/
}
}
To resize image i have made a function where i'm passing the image details and image can be resize their size. But its only resizing only one time, second time image uploading with their normal size.
When you use codeigniter upload library, to change the config you must use initialize.
public function resizeImage($filename){
$source_path = './upload/' . $filename;
$target_path = './upload/';
$config_manip = array(
'image_library' => 'gd2',
'source_image' => $source_path,
'new_image' => $target_path,
'maintain_ratio' => TRUE,
'width' => 500,
);
//change load library to initialize
$this->image_lib->initialize($config_manip);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
$this->image_lib->clear();
}
$this->load->library('upload', $config);
$this->load->library('image_lib');//load library outside your resizeImage function
if ($this->upload->do_upload('image')) {
$error = array('error' => $this->upload->display_errors());
$uploadedImage = $this->upload->data();
$this->resizeImage($uploadedImage['file_name']);
print_r('Image Uploaded Successfully.');
/*exit;*/
}
if ($this->upload->do_upload('image2')) {
$error = array('error' => $this->upload->display_errors());
$uploadedImage = $this->upload->data();
$this->resizeImage($uploadedImage['file_name']);
print_r('Image Uploaded Successfully.');
/*exit;*/
}
We are working on a social network project based on CI framework. I searched on the web but got confused. How to upload photo, allow user to crop the photo and save it using database so that it could be retrieved at the time of call? I'm using this for profile picture.
On view:
<?php echo form_open_multipart ('index.php/Main/do_upload'); ?>
<table class="table">
<tr>
<td>Title</td>
<td><?php echo form_input('title'); ?></td>
</tr>
<tr>
<td>Image</td>
<td><?php echo form_upload('userfile'); ?></td>
</tr>
<tr>
<td></td>
<td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
<?php echo form_close(); ?>
</tr>
</table>
On controller:
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('Material_view', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
Use Bluimp Multiple fileupload (https://blueimp.github.io/jQuery-File-Upload) and Croppr (https://fengyuanchen.github.io/cropperjs/) plugins together.
In Bluimp you can get enough code (there is a codeigniter class in their site) to handle file upload in codeigniter.
Please try with cropper js. i have used this and it is very easy and it will full fill your requirement
https://github.com/fengyuanchen/cropper
Create A Image model and paste the code
<?php
class Image_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->library('upload');
$this->load->library('image_lib');
}
public function do_resize($filename)
{
$source_path = 'uploads/' . $filename;
$target_path = 'uploads/thumb/thumb_'.$filename;
$config_manip = array(
'image_library' => 'gd2',
'source_image' => $source_path,
'new_image' => $target_path,
'maintain_ratio' => TRUE,
'width' => 150,
'height' => 150
);
$this->image_lib->initialize($config_manip);
$this->load->library('image_lib', $config_manip);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
die();
}
}
public function img_upload()
{
$config = array(
'upload_path' => "uploads",
'allowed_types' => "*",
'overwrite' => TRUE,
'max_size' => "5048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "3000",
'max_width' => "3000"
);
$this->upload->initialize($config);
$this->load->library('upload', $config);
if($this->upload->do_upload()) {
$response = array('upload_data' => $this->upload->data());
$this->do_resize($response['upload_data']['file_name']);
}
/*else{
$error = array('error'=>$this->upload->display_errors());
//print_r($error);die();
}*/
}
}
Call this function like this in your controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Post extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('image_model');
}
public function add() {
if(isset($_FILES)){
$config = $this->image_model->img_upload();
$file_data = $this->upload->data();
$data['feature_image'] = $file_data['file_name'];
}
$lat_id = $this->your_model->save($data);
}
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 create thumbnails original image is been uploaded to upload/large
but not thumbnails are created in upload/thumbs I was using the CodeIgniter Image Manipulation Class and it's not working.Help me to sort out problem
My Controller
var $file_path; //for original image
var $file_path_url; //for thumbnails
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->load->library('session');
$this->is_login();
$this->load->helper(array('form', 'url'));
$this->load->model('Edit_profile');
//return full path of directory
//Make sure these directory have read and write permission
$this->file_path = realpath(APPPATH . '../upload/large');
$this->file_path_url = realpath(APPPATH.'../upload/thumbs');
// $this->load->model('Insert_article');
}
function upload()
{
//loading image class
$session_data = $this->session->userdata('sessiondata');
$user_id = $session_data['user_id'];
//post image
$img=$this->input->post("filename");
//set preferences
$config['remove_spaces']=TRUE;
$config['encrypt_name'] = TRUE; // for encrypting the name
$config['upload_path'] = LARGEPATH;
$config['allowed_types'] = 'jpg|png|gif';
$config['max_size'] = '78000';
//load moadel ********
$this->load->model('Edit_profile');
//load upload class library
$this->load->library('upload', $config);
//$this->upload->do_upload('filename') will upload selected file to destiny folder
if (!$this->upload->do_upload('filename'))
{
// case - failure
$upload_error = array('error' => $this->upload->display_errors());
$this->load->view('edit_profile', $upload_error);
}
else
{
// case - success
//callback returns an array of data related to the uploaded file like the file name, path, size etc
$upload_data = $this->upload->data();
// call to model function *********
$data['images'] = $this->Edit_profile->insert_new_post($upload_data);
//now creating thumbnails
$config1 = array(
'source_image' => $upload_data['full_path'],
'create_thumb' =>true,
'overwrite' =>false,
'maintain_ratio' =>true,
'new_image' => $this->file_path_url,
'maintain_ratio' => true,
'width' => 36,
'height' => 36
);
print_r($config1);
$this->load->library('image_lib');
$this->image_lib->initialize($config1);
$this->image_lib->resize();
//here is the second thumbnail, notice the call for the initialize() function again
//$this->image_lib->initialize($config);
//$this->image_lib->resize();
$data['success_msg'] = '<div class="alert alert-success text-center">Your file <strong>' . '</strong> was successfully uploaded!</div>';
redirect(base_url("Display_profilepic/index"));
}
}
//Model
function insert_new_post($upload_data)
{
$session_data = $this->session->userdata('sessiondata');
$user_id = $session_data['user_id'];
$filePath = ltrim(LARGEPATH.$upload_data['file_name'],'.');
//print_r(LARGEPATH);
$query = "UPDATE `tbl_usrs` set profile_picture='".$filePath."' where user_id='".$user_id."'";
// $this->db->query($query,array($img));
$arg=array ($upload_data);
if($this->db->query($query,$arg)==true)
{
return true; // if added to database
}else {
return false;
}
}
Please update the new_image value by including the filename also.
$config1 = array(
'image_library'=>'gd2',
'source_image' => $upload_data['full_path'],
'create_thumb' =>true,
'overwrite' =>false,
'maintain_ratio' =>true,
'new_image' => $this->file_path_url.'/'.$upload_data['file_name'], //add slash
'maintain_ratio' => true,
'width' => 36,
'height' => 36
);
Try to use the following code
$data = $this->upload->data(); $this->thumb($data);
//Function for creating Thumbnails
function thumb($data) {
$config['image_library'] = 'gd2';
$config['source_image'] = $data['full_path'];
$config['create_thumb'] = TRUE;
// $config['maintain_ratio'] = TRUE;
$config['width'] = 350;
$config['height'] = 250;
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
return true;
}
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.