Codeigniter upload file and resize - codeigniter

I'm trying to resize pre-uploaded images and then send them to another server with FTP, but it doesnt seems to be working. The uploading is working fine, the ftp is working fine too but whenever I download the image and check the size it's just the same as the uploaded file.
This is my controller:
if ($this->upload->do_upload())
{
$data = $this->upload->data();
$image = $data['file_name'];
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/devices/'.$image;
$config['maintain_ratio'] = TRUE;
$config['width'] = 400;
$config['height'] = 300;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$localPath = './uploads/devices/'.$image;
$remotePath = 'webspace/httpdocs/uploads/devices/'.$image;
$this->load->library('ftp');
$config['hostname'] = '';
$config['username'] = '';
$config['password'] = '';
$config['port'] = 21;
$config['passive'] = TRUE;
$this->ftp->connect($config);
$this->ftp->upload($localPath, $remotePath);
$this->ftp->close();
}
What I want to achieve is upload the image, resize and replace it and upload the resized image after that.
Help is very much appreciated!

Final Edit:
Used initialize to pass the configs instead of passing them directly to load->library:
if ($this->upload->do_upload())
{
$data = $this->upload->data();
$image = $data['file_name'];
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/devices/'.$image;
$config['maintain_ratio'] = TRUE;
$config['width'] = 400;
$config['height'] = 300;
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->image_lib->resize();
$localPath = './uploads/devices/'.$image;
$remotePath = 'webspace/httpdocs/uploads/devices/'.$image;
$this->load->library('ftp');
$config['hostname'] = '';
$config['username'] = '';
$config['password'] = '';
$config['port'] = 21;
$config['passive'] = TRUE;
$this->ftp->connect($config);
$this->ftp->upload($localPath, $remotePath);
$this->ftp->close();
}

Related

Codeigniter Image watermark using image library

I want apply watermark on multiple images after uploading. Right now the images are uploading but all are without watermark and also I want to reduce the quality of uploading image.
public function do_upload()
{
$this->load->library('upload');
$name_array = array();
$files = $_FILES;
$cpt = count($_FILES['userfile1']['name']);
for($i=0; $i<=$cpt-1; $i++)
{
$_FILES['userfile']['name']= $files['userfile1']['name'][$i];
$_FILES['userfile']['type']= $files['userfile1']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile1']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile1']['error'][$i];
$_FILES['userfile']['size']= $files['userfile1']['size'][$i];
$this->upload->initialize($this->set_upload_options());
if($data = $this->upload->do_upload()){
$config['source_image'] = $files['userfile1']['tmp_name'][$i]; //get original image
$config['wm_type'] = 'overlay';
$config['wm_overlay_path'] = './portfolio_img/ninja.png';
$config['quality'] = 50;
$config['wm_vrt_alignment'] = 'bottom';
$config['wm_hor_alignment'] = 'right';
$this->load->library('image_lib', $config);
$this->image_lib->watermark();
}
$name_array[] = $this->upload->data('file_name');
}
return $name_array;
}
private function set_upload_options()
{
//upload an image options
$config = array();
$config['upload_path'] = './portfolio_img/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
return $config;
}
Watermark method need image path you are passing tem_path hope this will work
try this
$config['source_image'] = './portfolio_img/' . $files['userfile1']['name'][$i]
it is working for me
UDATE
please load library before the loop and change source_image name each time.
$config['source_image'] = '';
$config['wm_type'] = 'overlay';
$config['wm_overlay_path'] = './portfolio_img/ninja.png';
$config['quality'] = 50;
$config['wm_vrt_alignment'] = 'bottom';
$config['wm_hor_alignment'] = 'right';
$this->load->library('image_lib', $config);
for($i=0; $i<=$cpt-1; $i++)
{
$_FILES['userfile']['name']= $files['userfile1']['name'][$i];
$_FILES['userfile']['type']= $files['userfile1']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile1']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile1']['error'][$i];
$_FILES['userfile']['size']= $files['userfile1']['size'][$i];
$this->upload->initialize($this->set_upload_options());
if($data = $this->upload->do_upload()){
$config['source_image'] = './portfolio_img/' . $files['userfile1']['name'][$i]
$this->image_lib->initialize($config);
$this->image_lib->watermark();
}
$name_array[] = $this->upload->data('file_name');
}

I want to resize uploaded image in codeigniter but it is not working

I want to resize uploaded image in codeigniter but it is not working with this code. Image uploading successfully but I want to resize it to 110x110 and display it !
my code is here
class Upload_photo extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index(){
session_start();
$u = $_SESSION['username'];
$config['upload_path'] = 'user/'.$u.'/'.$filename;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '30000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload',$config);
if(!$this->upload->do_upload('file1'))
{
echo "Error". $this->upload->display_errors();
}
else {
$config['image_library'] = 'GD2';
$config['source_image'] = $config['upload_path'];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 110;
$config['height'] = 110;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
echo 'Photo Uploaded Successfully';
}
}
}
First you upload the original image so set the configs and upload the original image, then you resize the image. With that said:
$fullPath = 'user/' . $u . '/';
$config['upload_path'] = $fullPath;
$config['file_name'] = 'theNameofFile';
$config['allowed_types'] = 'jpg|png|bmp';
$config['max_size'] = '30000';
$config['overwrite'] = FALSE;
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if($this->upload->do_upload('file1') == FALSE)
{
echo "Error:" . $this->upload->display_errors();
return;
}
At this point the the file was uploaded. Now you need to grab it and resize.
// Reset
$config = array();
$config['image_library'] = 'gd2';
$config['source_image'] = $fullPath . $this->upload->data()['file_name'];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 100;
$config['height'] = 100;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
Finally, if you wish to, you can delete the original image.
If you want both images 1>uploded image 2>Thumbnail Image and want to store Thumbnail image to different folder you should use $config['new_image'] and path should not your url but should be absolute server path. If you don't know how to get absolute server path ? you can see my code below.
I got the solution, my code is here
class Upload_photo extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index(){
session_start();
$u = $_SESSION['username'];
$config['upload_path'] = 'user/'.$u;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '30000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload',$config);
$filename = $_FILES['file1']['name'];
//echo $filename;
//echo $source;
if(!$this->upload->do_upload('file1'))
{
echo "Error". $this->upload->display_errors();
return;
}
$config = array();
$config['image_library'] = 'gd2';
$config['source_image'] = 'user/'.$u.'/'.$filename ;
$config['new_image']= FCPATH . 'user/'.$u.'/Thumb/';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 110;
$config['height'] = 110;
$this->load->library('image_lib',$config);
if(!$this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
}
}

Upload Multiple Images and Generate Large Size

I have already generated a code that will upload multiple images in codeigniter, however I can't generate a large size of the uploaded images to other directory using codeigniter image library.
Using the codes below, it will only generate one image even though if the user uploaded multiple images.
function insert_prod(){
$foldername = $_POST['folder'];
$path = './uploads/products/'.$foldername;
mkdir($path, 0777, true);
mkdir($path.'/zoomimages', 0777, true);
$config = array();
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
$this->load->library('upload');
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $i++){
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];
$config['file_name'] = 'img_'.$i;
$this->upload->initialize($config);
$this->upload->do_upload();
$upload_data = array('upload_data' => $this->upload->data());
$full_path = $upload_data['upload_data']['full_path'];
$u_data = $upload_data['upload_data'];
// var_dump( $this->upload->data());
// var_dump($upload_data);
echo $cpt;
$config1['image_library'] = 'gd2';
$config1['source_image'] = $full_path;
$config1['new_image'] =$path.'/zoomimages';
$config1['create_thumb'] = FALSE;
$config1['maintain_ratio'] = TRUE;
$config1['width'] = 300;
$config1['height'] = 300;
$this->load->library('image_lib', $config1);
$this->image_lib->resize();
$this->image_lib->clear();
}
}
You are loading library with $this->load->library('image_lib', $config1); inside your loop so codeigniter see the class is already load so its not setting your $config1 again
Move $this->load->library('image_lib'); out side your loop
Then add $this->image_lib->initialize($config1); instant of $this->load->library('image_lib', $config1);

SImple solution to Codeigniter image class?

I have some messy code, even i use SimpleImage, i know i can use CodeIgniter image class, but config is little big, can someone post a little elegant and better solution, this is my code for now, i want to get rid of SimpleImage, and image class is initialized in controller.Here is what i have:
// Main config
$config['image_library'] = 'gd2';
$config['maintain_ratio'] = TRUE;
$config['height'] = '1';
$config['master_dim'] = 'width';
$config['overwrite'] = TRUE;
// Resize image with SimpleImage
$novaslika="img/proizvodi/".$last.".jpg";
$image = new SimpleImage();
$image->load($_FILES['slika']['tmp_name']);
$image->resizeToWidth(800);
$image->save($novaslika);
// Create PNG
$config['source_image'] = $_FILES['maska']['tmp_name'];
$config['width'] = 800;
$config['new_image'] = "./img/proizvodi/".$last."_maska.png";
$this->image_lib->initialize($config);
$this->image_lib->resize();
// Create thumb
$config['source_image'] = './img/proizvodi/'.$last.'.jpg';
$config['create_thumb'] = TRUE;
$config['new_image'] = './img/proizvodi/thumbs/'.$last.'_thumb.jpg';
$this->image_lib->initialize($config);
$this->image_lib->resize();
You can do something like this:
function index()
{
$this->load->library('image_lib');
$a = array(
'source_image' => 'images/1.jpg',
'width' => 100,
'height' => 100,
'new_image' => 'images/2.jpg',
'create_thumb' => TRUE,
'overwrite' => FALSE
);
$image = $this->_image_manipulation($a);
if($image === TRUE)
{
echo "IMAGE OK";
}
else
{
echo $image;
}
}
private function _image_manipulation($configs = '')
{
if($configs)
{
$config['image_library'] = 'gd2'; //static
$config['maintain_ratio'] = TRUE; //static
$config['master_dim'] = 'width'; //static
$config['source_image'] = $configs['source_image'];//required
$config['height'] = (isset($configs['height']))?$configs['height']:NULL;
$config['width'] = (isset($configs['width']))?$configs['width']:NULL;
$config['overwrite'] = (isset($configs['overwrite']))?$configs['overwrite']:NULL;
$config['new_image'] = (isset($configs['new_image']))?$configs['new_image']:NULL;
$config['create_thumb'] = (isset($configs['create_thumb']))?$configs['create_thumb']:NULL;
$this->image_lib->initialize($config);
if ( ! $this->image_lib->resize())
{
return $this->image_lib->display_errors();
}
else
{
return TRUE;
}
}
}
But will still need the SimpleImage library to convert to png's UNLESS, and I can't confirm, SimpleImage is using ImageMagick. If it is, that means it's installed on the system and you can change
$config['image_library'] = 'gd2';
to
$config['image_library'] = 'ImageMagick';
and CodeIgniter will handle the image conversion for you too; all you need to do is rename the file:
$a = array(
'source_image' => 'images/1.jpg',
'new_image' => 'images/1.png',
);

CodeIgniter upload one image to two directory

I would like to upload one image to different directory in CodeIgniter, so one images are stored in 2 folders.
Path one etc/www/image1/ and path two etc/www/image2/
Code
$config[‘upload_path’] =‘etc/www/image1/’;
$config[‘allowed_types’] = ‘jpg|jpeg|gif|png’;
$config[‘file_name’]=“imageone.jpg”;
$config[‘max_size’] = ‘10000’;
$this->upload->initialize($config);
if(!$this->upload->do_upload(‘userfile’)){
echo $this->upload->display_errors();
}else {
$this->upload->data(‘userfile’);
}
Just use PHP's Copy() function after you have uploaded the file successfully in your CI controller method...
EX:
$file = '/www/image1/example.txt';
$newfile = '/www/image1/example.txt';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
$config['upload_path'] = FCPATH.'upload/';
$config['allowed_types'] = FCPATH.'gif|jpg|jpeg|png';
$config['encrypt_name'] = TRUE;
$this->load->library('upload',$config);
$upload = $this->upload->do_upload("resim");
if($upload){
$resim = $this->upload->data();
$resimadi = $resim['file_name'];
$resimkayit = 'upload/'.$resimyolu.'';
$resimhotnews = 'upload/hotnews'.$resimadi.'';
$resimlastnews = 'upload/lastnews'.$resimadi.'';
$config['image_library'] = 'gd2';
$config['source_image'] = 'upload/'.$resimadi.'';
$config['new_image'] = 'upload/hotnews'.$resimadi.'';
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = FALSE;
$config['quality'] = '%80';
$config['width'] = 500;
$config['height'] = 350;
$this->load->library('image_lib', $config);
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
//tmb folder resim uploading end
//mini folder resim uploading start
$config1['image_library'] = 'gd2';
$config1['source_image'] = 'upload/'.$resimadi.'';
$config1['new_image'] = 'upload/lastnews/'.$resimadi.'';
$config1['create_thumb'] = FALSE;
$config1['maintain_ratio'] = FALSE;
$config1['quality'] = '%80';
$config1['width'] = 200;
$config1['height'] = 150;
$this->load->library('image_lib', $config1);
$this->image_lib->initialize($config1);
$this->image_lib->resize();
$this->image_lib->clear();
//on mac computers you have to give a writable permission for the folders

Resources