Codeigniter Image watermark using image library - codeigniter

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

Related

How to get the name of userfiles in controller to add to database

This is the code so far.
I have 2 images that are getting uploaded and added to the desired map.
My question is, how do i get both names from the images so I can add just the names to the database together with my other form information.
public function CreateTypesForGamma() {
$type = new stdClass();
$type->TypeID = $this->input->post('typeID') == 0 ? null : $this->input->post('typeID');
$type->Titel = $this->input->post('titel');
$type->TechnischeFiche = $this->input->post('technischeFiche');
$type->GammaID = $this->input->post('gammaID');
$type->ExtLink = $this->input->post('extLink');
$type->InfoNL = $this->input->post('infoNL');
$type->InfoFR = $this->input->post('infoFR');
$type->InfoEN = $this->input->post('infoEN');
$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];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
$fileName = $_FILES['userfile']['name'];
if ($i == 0)
$images[] = $fileName;
}
$fileName = implode(',',$images);
$this->gamma_model->upload_image($fileName);
system.out.println($images);
if ($type->TypeID == 0) {
$type->TypeID = $this->type_model->insert($type);
} else {
$this->type_model->update($type);
}
redirect('gamma/viewAdminGamma');
}
private function set_upload_options()
{
// upload an image options
$config = array();
$config['upload_path'] = APPPATH . 'images/types'; //give the path to upload the image in folder
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5120';
$config['overwrite'] = FALSE;
return $config;
}
Change the code to following
$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];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
$fileName = $_FILES['userfile']['name'];
$images[] = $fileName;
$type->TypeFoto=$fileName[0];// this gives name of first
image
$type->Lastentabel =$fileName[1];
}
$fileName = implode(',',$images);
$this->gamma_model->upload_image($fileName);
system.out.println($images);
if ($type->TypeID == 0) {
$type->TypeID = $this->type_model->insert($type);
} else {
$this->type_model->update($type);
}
redirect('gamma/viewAdminGamma');
}
private function set_upload_options()
{
// upload an image options
$config = array();
$config['upload_path'] = APPPATH . 'images/types'; //give the path to upload the image in folder
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5120';
$config['overwrite'] = FALSE;
return $config;
}

Codeigniter upload file and resize

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

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

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

Codeigniter - cropped image turns black

When I crop an image it turns completely black?? Why??
$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'].'/website/uploads/original/'; $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$this->load->library('upload'); $this->upload->initialize($config);
if(!$this->upload->do_upload()) { $error = array('error' => $this->upload->display_errors()); $this->load->view('submit', $error); } else { $data['upload_data'] = array('upload_data' => $this->upload->data()); $file_name
= $this->upload->file_name;
list($image_width, $image_height) = getimagesize($_SERVER['DOCUMENT_ROOT'].'/website/uploads/original/'.$file_name);
// create small size $config['image_library'] = 'GD2'; $config['source_image'] = $_SERVER['DOCUMENT_ROOT'].'/website/uploads/original/'.$file_name; $config['new_image'] = $_SERVER['DOCUMENT_ROOT'].'/website/uploads/small/'.$file_name; $config['maintain_ratio'] = TRUE; $config['width'] = 181; $config['height'] = 115; $config['master_dim'] = 'width';
$this->load->library('image_lib'); $this->image_lib->initialize($config);
if($image_width >= $config['width'] AND $image_height >= $config['height']) {
if (!$this->image_lib->resize())
{
echo $this->image_lib->display_errors();
} else {
if(file_exists($_SERVER['DOCUMENT_ROOT'].'/website/uploads/small/'.$file_name))
{
list($image_width, $image_height) = getimagesize($_SERVER['DOCUMENT_ROOT'].'/website/uploads/small/'.$file_name);
if($image_height > '115')
{
$config['source_image'] = $_SERVER['DOCUMENT_ROOT'].'/website/uploads/small/'.$file_name;
$y_axis = $image_height - 115;
$config['y_axis'] = $y_axis;
$config['x_axis'] = 181;
$this->image_lib->initialize($config);
if (!$this->image_lib->crop())
{
echo $this->image_lib->display_errors();
} else {
echo "cropped";
}
}
} } }
I believe CI uses the standard PHP GD module. Previously I've found that if the image you're using is slightly corrupt it will still display normally but once you resize you simply get a black box.
Have you tried using any other images? Images of different types (png/jpg/etc)?

Resources