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);
Related
Here is my code from controller
`
$type= explode('.', $_FILES['picture']['name']);
$type = $type[count($type)-1];
$url = "uploads/products/images/".uniqid(rand()).".".$type;
if(in_array($type, array('jpg','jpeg','png','JPG','JPGE','PNG') ) )
{
if(is_uploaded_file($_FILES['picture']['tmp_name']))
{
move_uploaded_file($_FILES['picture']['tmp_name'],$url);
}
}
'
Since you're using CodeIgniter, it has it's own Image Manipulation class:
https://www.codeigniter.com/userguide3/libraries/image_lib.html
$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
Set the width and height variables to what you need to resize the image to.
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;
}
}
}
I am using following code but unable to resize image even $this->image_lib->resize() is returning true, don't know what is going wrong:
if(file_exists($_SERVER['DOCUMENT_ROOT']."/uploads/avatars/".str_replace('_','-',$image)))
{
$config['source_image'] = $_SERVER['DOCUMENT_ROOT']."/uploads/avatars/".str_replace('_','-',$image);
}
else if (!file_exists("./uploads/avatars/".$image) || $image=="")
{
$config['source_image'] = $_SERVER['DOCUMENT_ROOT'].'/uploads/avatars/photo.jpg';
}
else
{
$config['source_image'] = $_SERVER['DOCUMENT_ROOT']."/uploads/avatars/".$image;
}
$config['image_library'] = 'gd2';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['dynamic_output'] = TRUE;
$config['width'] = $width;
$config['height'] = $height;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
Try the new_image tag:
$config['new_image'] = '/path/to/new_image.jpg';
Better to use SimpleImage.php as helper in CodeIgniter
$source=base_url().'/uploads/avatars/photo.jpg';
$image = new SimpleImage();
$image->load($source);
$image->resize(600,400);
$image->save($name_and_location);
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)
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;