Thumbs images upload error in codeigniter - php

<?php
$image = array(
'name' => 'userfile',
'id' => 'userfile',
);
$submit = array(
'name' => 'submit',
'id' => 'submit',
'value' => 'Upload'
);
?>
<?php echo form_open_multipart('upload/upload_image', 'id=upload_file'); ?> <!-- must autoload form helper for this -->
<?php echo form_upload($image); ?>
<?php echo form_submit($submit); ?>
<?php echo form_close(); ?>
this is main.php in view folder
<?php
class Upload_model extends CI_Model{
var $original_path;
var $resized_path;
var $thumbs_path;
//initialize the path where you want to save your images
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');
}
function do_upload(){
$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
);
$this->load->library('upload', $config);
$image_data = $this->upload->data(); //upload the image
print_r($image_data);
if($image_data){
echo "uploaded";
}else {echo "not upload";}
//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();
}
}
this is my model
and controller only load main.php that allow me select image click submit and then controller call model above to load image
the problem is --> there are no error generate but image and thumbs not uploaded to directory why please?

Related

Creating multiple resized images in codeigniter?

I was trying to upload an image and create multiple resized copies of that image to be used in different locations in a website. I was using the CodeIgniter Image Manipulation Class and it worked perfectly and I was able to get a resized image, however when I tried to create more than one resized image it didn’t work.
//controller
<?php
class Upload extends CI_Controller{
public function upload_image(){
$this->upload_model->do_upload(); //execute the upload function
}
}
//model
<?php
class User_model extends CI_Model{
var $original_path;
var $resized_path;
var $thumbs_path;
//initialize the path where you want to save your images
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');
}
function do_upload(){
$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
);
$this->load->library('upload', $config);
$image_data = $this->upload->data(); //upload the image
// 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->image_lib->initialize($config);
$this->image_lib->resize();
when i am using CI Image manipulation Class, I thought that calling the resize function twice will produce two resized images but that didn’t actually work and after some research and tutorials I found out that I have to initialize the image_lib class every time I call the resize() function.
So Your function should look like this
function do_upload(){
$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
);
$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
);
//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();
desires $config array
//here is the second thumbnail, notice the call for the initialize() function again
$this->image_lib->initialize($config);
$this->image_lib->resize();
}
}

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 create mutiple thumbnails for multiple image in code igniter

i try to create multiple thumbnail for multiple image. but it create for one image only and try increase original image width and height and its not increasing
please correct my code
here my code....
uploading multiple image
<?php
if(!$this->input->post('upload')){
$this->create();
}
else{
$data['banner_img1'] =$_FILES['files']['name'][0];
$data['banner_img2'] =$_FILES['files']['name'][1];
$data['banner_img3'] =$_FILES['files']['name'][2];
$update_id=$this->uri->segment(3);
/* Create the config for upload library */
$config=array(
'allowed_types' => 'jpg|jpeg|png|gif',
'upload_path' => $this->gallery_path,
'max_size' => '2000'
);
/* Load the upload library */
$this->load->library('upload');
$this->upload->initialize($config);
if($this->upload->do_multi_upload('files')){
}else{
echo $this->upload->display_errors();
}
$image_data=$this->upload->data();
/* Create the config for image library */
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path.'/banner_thumbs',
'maintain_ratio' =>true,
'width' => '1200',
'height'=> '600'
);
/* Load the image library */
$this->load->library('image_lib');
$this->image_lib->initialize($config);
if($this->image_lib->resize('files')){
if(is_numeric($update_id)){
$this->_update($update_id, $data);
}else{
$this->_insert($data);
}
}else{
echo $this->image_lib->display_errors();
}
?>

Codeigniter Uploading files into gallery folder

This is what im trying to do in codeigniter:
I have member profile pages that all have their own gallery of photos. The user to be able to login and upload photos. I want the photos to then be placed inside of a folder with the usersname. So if the directory doesnt exsit for the user i want it to be created once he uploads a photo. The last thing of this is I want those photos uploaded to relate to that user and display on his profile page.
What I have working:
I can upload photos but it goes inside of the regular upload folder, I can also see the filename inside of the database that relates to that photo and I can view the files.
I need the user photos to be created inside of a folder with the username or id. and only the user photos to be displayed on his page. here is my gallery model:
<?php
class Gallery_model extends CI_Model{
// initalizes the gallery path variable
var $gallery_path;
var $gallery_path_url;
public function __construct(){
parent::__construct();
//sets the gallery path to applications folder and gallery image path
$this->gallery_path = realpath(APPPATH . '../portfolio');
$this->gallery_path_url = base_url().'portfolio/';
}
function do_upload(){
$config = array(
// requried variable allowed types is needed also delcared the upload path to gallery path
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 3000
);
// loads the library and sets where its going to go to
$this->load->library('upload',$config);
// this performs the upload operation
$this->upload->do_upload();
//returns data about upload ( file location )
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path . '/thumbs',
'maintain_ration' => true,
'width' => 250,
'height' => 220
);
$data = array(
'username' => $this->input->post('username'),
'category' => $this->input->post('category'),
'filename' => $image_data['file_name'],
);
$this->db->insert('portfolio', $data );
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
function get_images(){
$files = scandir($this->gallery_path);
// substracts these out of array
$files = array_diff($files, array('.', '..','thumbs'));
$images = array();
foreach ($files as $file){
$images [] = array(
'url' => $this->gallery_path_url . $file,
'thumb_url' => $this->gallery_path_url . 'thumbs/' .$file
);
}
return $images;
}
}
and here is my view:
function gallery(){
$this->load->model('user_model');
$data = array(
//$session_id = $this->session->userdata('session_id')
); // if no data is there and you wont get an error
if($query = $this->user_model->get_profile())
{
$data['records'] = $query;
}
// loads the model. if the upload posts, call the do model method from the gallery model Model.
$this->load->model('gallery_model');
if ($this->input->post('upload')){
$this->gallery_model->do_upload();
}
// displays the images
$data['images'] = $this->gallery_model->get_images();
$data['main_content'] = '/pages/gallery';
$this->load->view('templates/template',$data);
}
You see the original filename in the portfolio folder because you missed out the "file_name" config variable for the upload library. Example:
$config = array(
// requried variable allowed types is needed also delcared the upload path to gallery path
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 3000,
'file_name' => "the user id or username"
);
I also see that you do not have "overwrite" variable set. Can be an problem because you are storing the filename as username as CI will not overwrite the file when the user upload a new photo.

Generating image thumb in codeigniter

I am pretty new to codeigniter and just building my first application with it, but I am abit stumped when it comes to generating a thumbnail from an image.
The image uploads correctly but the thumb isnt generating and I am getting no errors :(
I hope someone can give me a helping hand, chances are I am just being a tit and its something really simple like mis spelling var.
Heres the code for my image model:
<?php
class Image_model extends CI_Model {
var $image_path;
function Image_model(){
parent::__construct();
$this->image_path = realpath(APPPATH.'../'.$this->config->item('dir_dynamic_images'));
}
function do_upload(){
$config = array(
'allowed_types' => "jpeg|gif|jpg|png",
'upload_path' => $this->image_path,
'max_size' => 2000
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->image_path . '/thumbs',
'maintain_ratio' => true,
'width' => 200,
'height' => 200
);
echo $config['new_image'];
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
}
?>
I think you need to set the create_thumb parameter to true and specify the image library-
$config = array(
'image_library' => 'gd2',
'source_image' => $image_data['full_path'],
'create_thumb' => true,
'new_image' => $this->image_path . '/thumbs',
'maintain_ratio' => true,
'width' => 200,
'height' => 200
);
try finding the error with -
if(!$this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
This piece of code is working for me. Hopefully it will works for you . you can also check the image manipulation helper in codeigniter. Don'f forget to initialize the config.
$config['image_library'] = 'imagemagick';
$config['library_path'] = '/usr/X11R6/bin/';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['x_axis'] = '100';
$config['y_axis'] = '60';
$this->image_lib->initialize($config);
if ( ! $this->image_lib->crop())
{
echo $this->image_lib->display_errors();
}

Categories