CodeIgniter does not add watermark with image_lib - php

I'm trying to add watermark on uploaded image, but it doesn't works.. There is my code, I can't see any errors there..
$data['photo_name'] = $data['slogan'];
$data['photo_ext'] = end(explode('.', $photoValue));
$data['photo_name'] = $data['photo_name'] . '.' . $data['photo_ext'];
file_put_contents($_SERVER["DOCUMENT_ROOT"] .'/'. PHOTOS_UPLOAD_FOLDER . $data['photo_name'], file_get_contents($photoValue));
// $this->resize_image($_SERVER["DOCUMENT_ROOT"] .'/'. PHOTOS_UPLOAD_FOLDER . $data['photo_name']);
$this->simpleimage->load($data['photosData']['uploadFolder'] . $data['photo_name']);
$this->simpleimage->cutFromCenter(450, 450);
$this->simpleimage->save($_SERVER["DOCUMENT_ROOT"] .'/'. PHOTOS_UPLOAD_FOLDER . $data['photo_name']);
// add watermark
$configImg = array();
$configImg['image_library'] = 'gd2';
$configImg['wm_type'] = 'overlay';
$configImg['source_image'] = $_SERVER["DOCUMENT_ROOT"] .'/'. PHOTOS_UPLOAD_FOLDER . $data['photo_name'];
$configImg['wm_overlay_path'] = base_url('assets/logo.png');
$configImg['wm_opacity'] = '50';
$configImg['wm_vrt_alignment'] = 'middle';
$configImg['wm_hor_alignment'] = 'center';
$this->image_lib->initialize($configImg);
$this->image_lib->watermark();
$this->image_lib->clear();
$this->image_lib->display_errors();
print_r($configImg);
and it does not printing any errors.
it prints only my array
Array
(
[image_library] => gd2
[wm_type] => overlay
[source_image] => ../king-include/uploads/possibly-the-best-youtube-comment-ever-.jpg
[wm_overlay_path] => http://www.domain.info/parser/assets/logo.png
[wm_opacity] => 100
[wm_vrt_alignment] => top
[wm_hor_alignment] => center
)

source_image : Sets the source image name/path. The path must be a relative or absolute server path, not a URL.
The problem is with your watermarking overlay image. Set it with correct path and try again.

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 Resize Image function is not working

I have some problem in this code. This code is working on my local windows based system. But the same code in not working on online server. I have a function that convert the big images in to small size images according to the parameters. the code of this function is given below.
public function do_resize($source_file, $target_folder, $height = 128, $width = 128){
$filename = $source_file;
$temp_data = explode('/',$filename);
$new_filename = end($temp_data);
$temp_data = explode('.', $new_filename);
$ext = end($temp_data);
$new_filename = $temp_data[0] . $width .'-'. $height .'.'. $ext;
$source_path = $filename;
$folder_path = '';
$temp_folder = explode('/',$target_folder);
foreach ($temp_folder as $folder) {
$folder_path .=$folder . '/';
if (!file_exists($folder_path)) {
mkdir($folder_path);
}
}
$target_path = $target_folder;
if (file_exists($source_file)) //file_exists of a url returns false.It should be real file path
{
return $folder_path . $new_filename;
}
if(isset($config_manip)){
unset($config_manip);
}
$config_manip = array(
'image_library' => 'gd2',
'source_image' => $source_path,
'maintain_ratio' => FALSE,
'new_image' => $target_path,
'create_thumb' => TRUE,
'thumb_marker' => $width . '-'. $height,
'width' => $width,
'height' => $height
);
$CI =& get_instance();
$CI->load->library('image_lib');
$CI->image_lib->initialize($config_manip);
if (!$CI->image_lib->resize()) {
echo $CI->image_lib->display_errors();
echo "<br>";
echo $config_manip['source_image'];
}
// clear //
$CI->image_lib->clear();
return $folder_path . $new_filename;
}
I am calling this function like this.
$name = $this->imageresize->do_resize($brand['logo'], $target_folder, 50, 50);
Then input parameter $brand['logo'] have this value "uploads/images/system/placeholder.png" and parameter $target_folder have this value "uploads/images/cache/brands/brand-logo".
I did not get any error from this code. but It is not resizing the images also. I also set the permission of the directory to 777.
Any one have some solution for this. Thanks
Change:
if (file_exists($source_file)) //file_exists of a url returns false.It should be real file path
{
return $folder_path . $new_filename;
}
on
if (file_exists($folder_path . $new_filename)) //file_exists of a url returns false.It should be real file path
{
return $folder_path . $new_filename;
}
AND $config_manip:
'new_image' => $target_path,
ON
'new_image' => $folder_path . $new_filename,
:)

Codeigniter 3 upload not saving thumb to directory

I am trying to save the thumbnail image to the directory, but it is not working. I am able to save the original image and save info to database, but for some reason the thumbnail is not saving to the thumbs directory I create. The directory has read/write access. I will also note I am using Dropzone.js.
Here is my partial code:
if ( ! $this->upload->do_upload('file'))
{
//upload failed, echo back negative response to dropzone.js
}
else
{
//upload success, upload file(s)
$image_data = $this->upload->data();
$img_config['source_image'] = '/uploads/videos/'.$image_data['file_name'];
$img_config['new_image'] = '/uploads/videos/thumbs/'.$image_data['file_name'];
$img_config['create_thumb'] = TRUE;
$img_config['maintain_ratio'] = TRUE;
$img_config['width'] = 251;
$img_config['height'] = 180;
$this->load->library('image_lib', $img_config);
$this->image_lib->resize();
$file = array(
'user_id' => $this->my_auth->logged_in(),
'img_name' => $image_data['raw_name'],
'thumb_name' => $image_data['raw_name'].'_thumb',
'ext' => $image_data['file_ext'],
'upload_date' => time()
);
$this->upload_model->add_image($file);
$data['img_success'] = '<div class="alert alert-success" id="imgSuccess">Your image <strong>' . $image_data['file_name'] . '</strong> was successfully uploaded!</div>';
$this->load->view('templates/frontend/front_header', $data);
$this->load->view('templates/frontend/front_navbar');
$this->load->view('frontend/upload_images', $data);
$this->load->view('templates/frontend/front_footer', $data);
}
I am able to successfully save the original image, just not the thumbnail. Can anyone see anything wrong here?
I added this to my __construct:
$this->original_path = realpath(APPPATH.'../uploads/images');
$this->thumbs_path = realpath(APPPATH.'../uploads/images/thumbs');
and changed the source_image and new_image configs:
$img_config['source_image'] = $image_data['full_path'];
$img_config['new_image'] = $this->thumbs_path;
This seems to have fixed it.

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;

how to automatically copy an entire directory as thumbnails using phpthumb (or some other library)

I would like all images dropped in a directory to be copied into a separate directory as thumbnails that i can then view on my site. Right now i'm researching phpthumb, but have also downloaded wideimage and zen photo.
Thanks if you can find an exact duplicate to this, near matches may also be helpful.
Here is the script that only copies a single files:
require_once '../ThumbLib.inc.php';
//include the thumb library
$thumb = PhpThumbFactory::create('test.jpg');
//create a new image from the targeted jpegs
$thumb->adaptiveResize(100, 100);
//resize
$thumb->save('test.png', 'png');
//save as 'test' with the file type png
//echo "<img src='$thumb' class='thumb'>";
$thumb->show();
//print the thumbnail to the screen
Try
$dir = "photos" ;
$destination = "thumb" ;
$images = scandir($dir);
foreach ( $images as $image ) {
if(is_file($image))
{
$ext = pathinfo ( $dir . DIRECTORY_SEPARATOR . $image, PATHINFO_EXTENSION );
$thumb = PhpThumbFactory::create ( $dir . DIRECTORY_SEPARATOR . $image );
$thumb->adaptiveResize ( 100, 100 );
$thumb->save ( $destination . DIRECTORY_SEPARATOR . $image, $ext );
$thumb->show ();
}
}

Categories