Codeigniter: Unable to create thumbnail for multiple images - php

I am trying upload multiple images(3) at a time and create thumbnail for each image. But my code upload 3 images and create only 1 thumbnail(thumbnail of 1st image). How to create thumbnail of multiple images?
Controller: uploadImage function and create thumbnail function
function uploadImage()
{
if($this->validate()==TRUE) {
$config['upload_path'] = "images/uploads/";
$config['allowed_types'] = "gif|jpg|jpeg|png";
$config['max_size'] = "5000";
$config['max_width'] = "1907";
$config['max_height'] = "1280";
$this->load->library('upload', $config);
foreach ($_FILES as $key => $value) {
if (!empty($value['tmp_name'])) {
if ( ! $this->upload->do_upload($key)) {
$error = array('error' => $this->upload->display_errors());
//failed display the errors
}
else {
//success
$finfo=$this->upload->data();
$this->_createThumbnail($finfo['file_name']);
$data['uploadInfo'] = $finfo;
$data['thumbnail_name'] = $finfo['raw_name']. '_thumb' .$finfo['file_ext'];
}
}
}
}
}
//Create Thumbnail function
function _createThumbnail($filename)
{
$config['image_library'] = "gd2";
$config['source_image'] = "images/uploads/" .$filename;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = "80";
$config['height'] = "80";
$this->load->library('image_lib',$config);
if(!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
}

Do some changes in createthumbnail function according to this link.
Instead of
$this->load->library('image_lib',$config);
use
$this->load->library('image_lib');
// Set your config up
$this->image_lib->initialize($config);
// Do your manipulation
$this->image_lib->clear();
New createThumbnail function:
//Create Thumbnail function
function _createThumbnail($filename)
{
$this->load->library('image_lib');
// Set your config up
$config['image_library'] = "gd2";
$config['source_image'] = "images/uploads/" .$filename;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = "80";
$config['height'] = "80";
$this->image_lib->initialize($config);
// Do your manipulation
if(!$this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
$this->image_lib->clear();
}

Related

Codeigniter image compression not working

I want to upload large images on my website. I want to reduce the size of those using codeigniter. So I am doing this code
function upload_image($data) {
$config['upload_path'] = './temp/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 10000;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('image')) {
$error = array('error' => $this->upload->display_errors());
pre($error);
} else {
$config = array();
$data = array('upload_data' => $this->upload->data());
$config['image_library'] = 'gd';
$config['source_image'] = './temp/' . $data['upload_data']['file_name'];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['quality'] = 50;
$config['new_image'] = './temp/' . $data['upload_data']['file_name'];
$this->load->library('image_lib', $config);
$this->image_lib->resize();
pre($data);
}
}
But images are not being compressed. Original and uploaded size are the same. Where am I wrong?
Maybe exists an error. Check for errors:
if (!$this->image_lib->resize()){
echo $this->image_lib->display_errors();
}
Note: Use gd2 as library (default value is gd2):
$config['image_library'] = 'gd2';

Image resize not working in CodeIgniter 3

I am developing a web application. In my application, I am uploading an image to server and then resize it. I uploaded to server successfully. But when I resize image, it is not resizing image. But it is not throwing error as well.
This is my image file upload model
class file_helper extends CI_Model{
function __construct()
{
parent::__construct();
$this->load->library('image_lib');
}
function generateRandomFileName()
{
$rand = rand(10000,99999);
$file_name = time().$rand;
return time().$rand;
}
function uploadImage()
{
$name = $this->generateRandomFileName();
$config['upload_path'] = './'.UPLOAD_FOLDER.'/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['file_name'] = $name;
$this->load->library('upload', $config);
if ( !$this->upload->do_upload('userfile'))
{
return FALSE;
}
else
{
$data = $this->upload->data();
$data['virtual_path'] = UPLOAD_FOLDER."/".$name.".".$data['file_ext'];
return $data;
}
}
function resizeImage($path)
{
$config['image_library'] = 'gd2';
$config['source_image'] = '/'.$path;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 300;
$config['height'] = 50;
$this->load->library('image_lib', $config);
if ( ! $this->image_lib->resize())
{
print_r($this->image_lib->display_errors());
}
else{
echo "Ok";
}
}
}
As you can see in the model I print out "Ok" on success and print_r the error in failure. The path I passed is something like this "uploads/23344545.png". But that resize function always print out "Ok", but the image is not resized. What is wrong with my code?
You need to resize the image while uploading
function uploadImage()
{
$name = $this->generateRandomFileName();
$config['upload_path'] = './'.UPLOAD_FOLDER.'/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['file_name'] = $name;
$config['width'] = 300;
$config['height'] = 50;
$this->load->library('upload', $config);
if ( !$this->upload->do_upload('userfile'))
{
return FALSE;
}
else
{
$data = $this->upload->data();
$data['virtual_path'] = UPLOAD_FOLDER."/".$name.".".$data['file_ext'];
return $data;
}
}

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

source image is empty codeigniter crop image

I am not able to save the image in the destination folder, the directory remains empty.
$this->load->library('image_lib');
$slug = url_title(convert_accented_characters($this->input->post('title')), 'dash', TRUE);
$config['image_library'] = 'gd2';
$config['source_image'] = '/arquivos_upload/link/'. $slug .'.jpg';
$config['allowed_types'] = 'jpg|png';
$config['maintain_ratio'] = TRUE;
$config['quality'] = '100%';
$config['width'] = 405;
$config['height'] = 405;
$this->load->library('image_lib', $config);
$this->image_lib->clear();
$this->image_lib->initialize($config);
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}

Codeigniter image resize resizing only one image

What I am basically trying to do is resize a image into two different size images. However I am only being able to resize the first featured_$filename I am not being able to resize thumb_$filename .
Initially I tried creating a single function and passed the configuration as an array and it didnt worked but same thing, only single image was resized.
$this->resizeImage($imagePath, $file['upload_data']['file_name']);
$this->resizeThumb($imagePath, $file['upload_data']['filename']);
public function resizeImage($imagePath, $filename){
$config['image_library'] = 'gd2';
$config['source_image'] = $imagePath;
$config['create_thumb'] = FALSE;
$config['new_image'] = 'featured_'.$filename;
$config['maintain_ratio'] = TRUE;
$config['width'] = 570;
$config['height'] = 372;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->clear();
}
public function resizeThumb($imagePath, $filename){
$config['image_library'] = 'gd2';
$config['source_image'] = $imagePath;
$config['create_thumb'] = FALSE;
$config['new_image'] = 'thumb_'.$filename;
$config['maintain_ratio'] = TRUE;
$config['width'] = 180;
$config['height'] = 135;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->clear();
}
You need to load the library only once but initialize the library twice with different configs. E.g. :
$this->load->library('image_lib');
/* size 64*72 for comments */
$configThumb = array();
$configThumb['image_library'] = 'gd2';
$configThumb['create_thumb'] = TRUE;
$configThumb['new_image'] = './profile_images/thumbs/';
$configThumb['maintain_ratio'] = TRUE;
$configThumb['width'] = 64;
$configThumb['height'] = 72;
$configThumb['thumb_marker'] = "";
//$this->load->library('image_lib');
/* size 64*72 for comments */
/* size 167*167 for profile page */
$configThumbMedium = array();
$configThumbMedium['image_library'] = 'gd2';
$configThumbMedium['create_thumb'] = TRUE;
$configThumbMedium['new_image'] = './profile_images/medium/';
$configThumbMedium['maintain_ratio'] = TRUE;
$configThumbMedium['width'] = 167;
$configThumbMedium['height'] = 167;
$configThumbMedium['thumb_marker'] = "";
/* size 167*167 for profile page */
if(!$this->upload->do_upload('image')){
return 0;
}
$uploadedDetails = $this->upload->data();
if($uploadedDetails['is_image'] == 1){
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
$this->image_lib->initialize($configThumbMedium);
$this->image_lib->resize();
}
Hope it helps.
hi load library first and use $this->image_lib->initialize($config) to pass config and also in new_image config pass path with new image name.
$this->load->library('image_lib');
$config['new_image'] = 'image_dir/thumb_'.$filename
//ALL your other configs
$this->image_lib->initialize($config)

Categories