Watermark problem with CodeIgniter when I am trying to call a function - php

I am trying to watermark some images on upload by calling a function. However when I try doing so, I get the following error: Unable to load the requested language file: language/default/imglib_lang.php . The piece of code where I am trying to watermark the image is the following:
$this->load->library('upload');
$this->load->library('image_lib');
$config['image_library'] = 'GD2';
try {
$image = new ImageResize($path);
$image->quality_jpg = 85;
$image->resizeToWidth(1920);
$new_name = 'img_1920x_' . generate_unique_id() . '.jpg';
$new_path = 'uploads/' . $folder . '/' . $new_name;
$config['source_image'] = base_url($new_path);
$config['wm_overlay_path'] =
base_url('uploads/logo/waterLogo.png');
$config['wm_type'] = 'overlay';
$config['width'] = '200';
$config['height'] = '100';
$this->image_lib->initialize($config);
$this->image_lib->watermark();
$image->save(FCPATH . $new_path, IMAGETYPE_JPEG);
return $new_name;
} catch (ImageResizeException $e) {
return null;
}
If I am trying to use the watermark function in another place of the application I don't encounter this problem. What do you think I'm doing wrong here? Thanks in advance!

Related

Watermark function returns true but the image is not modified

All I am trying to do is watermark an image on upload. Although the watermark function returns TRUE the image is not modified.
This is the function I wrote, even with try and catch it's not working:
public function product_small_image_upload($path, $folder)
{
try {
$image = new ImageResize($path);
$image->quality_jpg = 85;
$image->resizeToHeight(300);
$new_name = 'img_x300_' . generate_unique_id() . '.jpg';
$new_path = 'uploads/' . $folder . '/' . $new_name;
$imgConfig = array();
$imgConfig['image_library'] = 'GD2';
$imgConfig['source_image'] = $new_path;
$imgConfig['wm_text'] = 'Copyright 2019';
$imgConfig['wm_type'] = 'text';
$imgConfig['wm_font_size'] = '16';
$this->load->library('image_lib', $imgConfig);
$this->image_lib->initialize($imgConfig);
$this->image_lib->watermark();
$image->save(FCPATH . $new_path, IMAGETYPE_JPEG);
return $new_name;
} catch (ImageResizeException $e) {
return null;
}
}
The $new_path variable contains this string upload/images/img_x300_1289.png, the name being randomly generated.
I also tried base_url().$new_path because I thought that I have to be more specific about the image path but it is still not working.
How can I see what doesn't work, why the image gets no watermark even tho' I am doing this on upload?
First add $new_path = FCPATH . 'uploads/' . $folder . '/' . $new_name;
You don't need to use $imgConfig['image_library'], and you haven't use aligment of the water mark .
why DOn't you use the correct way like this:
$config['source_image'] = $new_path;
$config['wm_text'] = 'Copyright 2019';
$config['wm_type'] = 'text';
$config['wm_font_path'] = './system/fonts/texb.ttf';
$config['wm_font_size'] = '8';
$config['wm_font_color'] = '000000';
$config['wm_vrt_alignment'] = 'bottom';
$config['wm_hor_alignment'] = 'right';
use this code and you willl get the water mark just choose the color of the text you want

Codeigniter: How to get resized image path?

I am using default library of codeigniter to resize my image to get different thumbs. I have refer to Documentation of codeigniter for this.
My images gets resize fine. Now, I want to store resized image path in database. For that I checked library file and other details as well but not found a way to get resized image path.
Please check my code
protected function createThumbs($params)
{
if( !is_dir($params['targetPath']) ) {
mkdir($params['targetPath'], 0777, TRUE);
}
$tConfig['image_library'] = 'gd2';
$tConfig['source_image'] = $params['sourcePath'];
$tConfig['new_image'] = $params['targetPath'];
$tConfig['create_thumb'] = TRUE;
$tConfig['maintain_ratio'] = TRUE;
$tConfig['width'] = $params['width'];
$tConfig['height'] = $params['height'];
$this->load->library('image_lib', $tConfig);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
// clear //
$this->image_lib->clear();
}
Does anyone knows how to get it?
Try
protected function createThumbs($params)
{
if( !is_dir($params['targetPath']) ) {
mkdir($params['targetPath'], 0777, TRUE);
}
$tConfig['image_library'] = 'gd2';
$tConfig['source_image'] = $params['sourcePath'];
$tConfig['new_image'] = $params['targetPath'];
$tConfig['create_thumb'] = TRUE;
$tConfig['maintain_ratio'] = TRUE;
$tConfig['width'] = $params['width'];
$tConfig['height'] = $params['height'];
$this->load->library('image_lib', $tConfig);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
// clear
$this->image_lib->clear();
#return
$file_info = pathinfo($tConfig['new_image']);
return $file_info['dirname'].'/'.$file_info['filename'].'_thumb.'.$file_info['extension'];
}
Your thumb file will be called with _thumb at the end of file name.
For example if your file is: image.jpg your resized thumb will be called image_thumb.jpg
public function addUserImages()
{
$image = preg_replace("/\s+/", "_", $_FILES['user_image']['name']);
$config['upload_path'] = "./Images/ProfileImages/";
$config['allowed_types'] = "gif|jpg|png|jpeg|JPG|JPEG|PNG|GIF";
$config['file_name'] = $image;
$this->load->library('upload',$config);
if($this->upload->do_upload("user_image")==false)
{
$error = array('error' => $this->upload->display_errors());
echo $error['error'];
return $error->error;
}
else
{
$data = $this->upload->data();
$newImage = $data['file_name'];
$config['image_library'] = 'gd2';
$config['source_image'] = './Images/ProfileImages/'.$newImage;
$config['new_image'] = './Images/ProfileImages/small_'.$newImage;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['quality'] = '100';
$config['width'] = 250;
$config['height'] = 250;
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->load->library('image_lib', $config);
if(!$this->image_lib->resize())
{
return $this->image_lib->display_errors();
}
else {
$newImage = "small_".$newImage;
}
}
}

Image Manipulation library will not create a thumbnail

I have created a library file that manages gallery functions, when a user uploads an image.
This code will not create a thumbnail - it tries to resize the original image - am i missing something obvious?
private function _create_thumbnail($file_path)
{
$config['image_library'] = 'gd2';
$config['source_image'] = $file_path;
$config['create_thumb'] = 1;
$config['maintain_ratio'] = 1;
$config['width'] = 90;
$config['height'] = 90;
$this->CI->load->library('image_lib', $config);
$this->CI->image_lib->initialize($config);
$result = $this->CI->image_lib->resize();
print_r( $this->CI->image_lib->display_errors());
print_r($config);
// if(!$result) {
// // echo $this->CI->image_lib->display_errors();
// print_r($this->CI->image_lib->display_errors());
// }
return $result;
}
Remove the below line and try again
$this->CI->image_lib->initialize($config);

CodeIgniter image upload and resize failing

I am really struggling with my CodeIgniter script for uploading, resizing and cropping profile images for users. In the end, I want to upload the image with an encrypted name, resize it and save it twice (once as a thumbnail and once as a medium sized image), and then delete the original file. However, I guess I'm really missing something, because I can't get the first upload resize process to work. I definitely have an error somewhere in the code (error to follow code), but I think I may have a gap in my understanding of the logic here. Who knows? Any feedback is extremely appreciated .
public function image() {
$config['upload_path'] = './temp_images/'; // This directory has 777 access
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '0'; // For unlimited width
$config['max_height'] = '0'; // For unlimited height
$config['encrypt_name'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$data['error'] = array('error' => $this->upload->display_errors());
$this->load->view('upload', $data);
}
else
{
$image_data_array = array('upload_data' => $this->upload->data());
foreach ($image_data_array as $image_data){
$raw_name = $image_data['raw_name'];
$file_ext = $image_data['file_ext'];
}
$file_name = $raw_name . $file_ext;
$image_path = 'temp_images/' . $file_name;
$new_path = 'image/profile/'; // This directory has 777 access
$config['image_library'] = 'gd';
$config['source_image'] = $image_path;
$config['new_image'] = $new_path;
$config['maintain_ratio'] = FALSE;
$config['width'] = 140;
$config['height'] = 140;
$config['quality'] = '80%';
$new_name = $new_path . $raw_name . $file_ext;
$this->load->library('image_lib', $config);
// The following is just to test that it worked:
if ( ! $this->image_lib->resize()) {
echo $this->image_lib->display_errors();
echo 'source: ' . $config['source_image'] . '<br />';
echo 'new: ' . $config['new_image'] . '<br />';
}
else {
echo "<img src='" . base_url() . $new_name . "' />";
}
$this->image_lib->clear();
}
}
The upload process works fine. Even the resizing worked when I just called $this->image_lib->resize(). It was when I tried the error-catching that it started yelling at me. I double-verified that both of the temp_images and image/profile have 777 permissions (as aforementioned, the upload and resize actually worked at one point). Here is the error produced:
Unable to save the image. Please make sure the image and file directory are writable.
source: temp_images/8ee1bab383cf941f34218f7535d5c078.jpg
new: image/profile/
Never really used CI before and can't see the details of your code.
First, check the permission that set on the folder - you are trying to upload to. Also try to use the full server path.
Second, see if you have PHP image extensions such as GD, ImageMagick enabled.
i've tried this tutorial
it works well for image upload and create thumbs folder for the resizing image...
Please update the new_image value by including the filename also.
$config['new_image'] = 'image/profile/' . $filename;

Multiple resizing in CodeIgniter

I need to make two images of a single loaded picture. This images must have fixed width, - 180 and 300 pixels.
At the bottom of my current results. This function can resize and create just one of two images. Everybody failed on second image, I trying whole day, but I'm can't find reason. Need help.
$this->_resize($data['upload_data']['file_name'], 300);
$this->_resize($data['upload_data']['file_name'], 180);
private function _resize($file_name, $size) {
$config['image_library'] = 'gd2';
$config['source_image'] = 'img/upload/' . $file_name;
$config['dest_image'] = base_url() . 'img/';
$config['create_thumb'] = TRUE;
$config['thumb_marker'] = '_' . $size;
$config['maintain_ratio'] = FALSE;
$config['width'] = $size;
$config['height'] = $size;
$this->load->library('image_lib', $config);
$result = $this->image_lib->resize();
$this->image_lib->clear();
return;
}
I'm use CodeIgniter 2.02
Dont load image_lib multiple times. Add image_lib in autoload libs and change
$this->load->library('image_lib', $config);
to
$this->image_lib->initialize($config);
This could help you, from user guide
A good practice is use the processing
function conditionally, showing an
error upon failure, like this:
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
Nill
Think that problem takes place because on the first run your script moves initial file to another folder. Try to use:
$config['new_image'] = base_url() . 'img/';
instead of
$config['dest_image'] = base_url() . 'img/';
I found myself this problem. In my case, I put the image source and new_image without base_url or REAL_PATH:
public function create_thumbnail($file_name='2012_02_23_15_06_00_1.jpg'){
$this->layout = false;
$image_url = PATH_TO_IMAGE_ARTICLE.DIRECTORY_SEPARATOR;
$config['image_library'] = 'gd2';
$config['source_image'] = 'assets/img/content/article/'.$file_name;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 210;
$config['height'] = 160;
$config['new_image'] = 'assets/img/content/article/thumb/thumb_' . $file_name;
$this->load->library('image_lib', $config);
if(!$this->image_lib->resize())
{
echo $this->image_lib->display_errors();exit;
}
return TRUE;
}
See?
you don't put
$config['new_image'] = base_url().'assets/img/content/article/thumb/thumb_' . $file_name;
but
$config['new_image'] = 'assets/img/content/article/thumb/thumb_' . $file_name;

Categories