I am pretty new to codeigniter and just building my first application with it, but I am abit stumped when it comes to generating a thumbnail from an image.
The image uploads correctly but the thumb isnt generating and I am getting no errors :(
I hope someone can give me a helping hand, chances are I am just being a tit and its something really simple like mis spelling var.
Heres the code for my image model:
<?php
class Image_model extends CI_Model {
var $image_path;
function Image_model(){
parent::__construct();
$this->image_path = realpath(APPPATH.'../'.$this->config->item('dir_dynamic_images'));
}
function do_upload(){
$config = array(
'allowed_types' => "jpeg|gif|jpg|png",
'upload_path' => $this->image_path,
'max_size' => 2000
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->image_path . '/thumbs',
'maintain_ratio' => true,
'width' => 200,
'height' => 200
);
echo $config['new_image'];
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
}
?>
I think you need to set the create_thumb parameter to true and specify the image library-
$config = array(
'image_library' => 'gd2',
'source_image' => $image_data['full_path'],
'create_thumb' => true,
'new_image' => $this->image_path . '/thumbs',
'maintain_ratio' => true,
'width' => 200,
'height' => 200
);
try finding the error with -
if(!$this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
This piece of code is working for me. Hopefully it will works for you . you can also check the image manipulation helper in codeigniter. Don'f forget to initialize the config.
$config['image_library'] = 'imagemagick';
$config['library_path'] = '/usr/X11R6/bin/';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['x_axis'] = '100';
$config['y_axis'] = '60';
$this->image_lib->initialize($config);
if ( ! $this->image_lib->crop())
{
echo $this->image_lib->display_errors();
}
Related
I'm trying to create image upload functionality in Codeigniter. While uploading a file I want to maintain the height and width but the size should be reduced for example from 10kb to 5 kb.
The below code is working fine, the file is getting uploaded but the size is the same.
what changes can be done to make it correct
if($_FILES['logo']['name'] != '')
{
$getimagesize = getimagesize($_FILES['logo']['tmp_name']);
$w = $getimagesize[0];
$h = $getimagesize[1];
#NEW CODE
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'jpg|jpeg|png';
$this->load->library('upload',$config);
if(!$this->upload->do_upload('logo'))
{
echo $this->upload->display_errors();
}#EOF if(!$this->upload->do_upload('logo'))
else
{
$data = $this->upload->data();
$config = [
'image_library' => 'gd',
'source_image' => './upload/'.$data['file_name'],
'create_thumb' => FALSE,
'maintain_ratio' => FALSE,
'quality' => '60',
'height' => $h,
'width' => $w,
'new_image' => './upload/'.$data['file_name']
];
$this->load->library('image_lib',$config);
if($this->image_lib->resize())
{
$this->session->set_flashdata('success','image saved');
redirect('welcome/school_details');
}
#Adding data into databse
}
Change config upload:
'quality' => '60'
to
'quality' => '60%'
public function converter(){
$pdf = "ci.pdf";
$this->load->library('image_lib');
$config = array(
'image_library' => 'imagemagick',
'library_path' => 'D:\Program Files\ImageMagick-6.9.3-Q16',
'source_image' => " /theme/assets/pdf".$pdf,
'new_image' => " /theme/assets/img/a.jpg",
'maintain_ratio' => true,
'width' => 980,
'quality' => '90%',
);
$this->image_lib->initialize( $config );
if (! $this->image_lib->resize()) {
$error_msg = $this->image_lib->display_errors();
print_r($error_msg);
}
else {
echo "Done";
}
}
Hi friends i'm trying to convert pdf file to jpg but while executing the controller it replies a message .
The path to the image is not correct. Image processing failed. Please
verify that your server supports the chosen protocol and that the path
to your image library is correct.
As i'm new to Codeigniter I was not able to achieve the result .could any one help me how to execute this code .
I'm currently struggling with the auto resizing of an oversize image.
I want 2 things to happen, #1 that the image automatically resizes to the given dimensions and 2 that it generates a thumb that can be used for example in the navbar etc..
This is the current code I got
EDITED Today
// avatar upload
if (isset($_FILES['userfile']['name']) && !empty($_FILES['userfile']['name'])) {
function __construct(){
parent::__construct();
//return the full path of the directory
//make sure these directories have read and write permessions
$this->original_path = realpath(APPPATH.'../uploads/original');
$this->resized_path = realpath(APPPATH.'../uploads/resized');
$this->thumbs_path = realpath(APPPATH.'../uploads/thumbs');
}
$this->load->library('image_lib');
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png', //only accept these file types
'max_size' => 2048, //2MB max
'upload_path' => $this->original_path, //upload directory
'width' => 250,
'height' => 250
);
$this->load->library('upload', $config);
$image_data = $this->upload->data(); //upload the image
//your desired config for the resize() function
$config = array(
'source_image' => $image_data['full_path'], //path to the uploaded image
'new_image' => $this->resized_path, //path to
'maintain_ratio' => true,
'width' => 128,
'height' => 128
);
//this is the magic line that enables you generate multiple thumbnails
//you have to call the initialize() function each time you call the resize()
//otherwise it will not work and only generate one thumbnail
$this->image_lib->initialize($config);
$this->image_lib->resize();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->thumbs_path,
'maintain_ratio' => true,
'width' => 36,
'height' => 36
);
//here is the second thumbnail, notice the call for the initialize() function again
$this->image_lib->initialize($config);
$this->image_lib->resize();
}
But Now I get these errors in browser
https://gyazo.com/4ec7360df1d3cc2703d19941583982fd
Any help would be appreciated
<?php
$image = array(
'name' => 'userfile',
'id' => 'userfile',
);
$submit = array(
'name' => 'submit',
'id' => 'submit',
'value' => 'Upload'
);
?>
<?php echo form_open_multipart('upload/upload_image', 'id=upload_file'); ?> <!-- must autoload form helper for this -->
<?php echo form_upload($image); ?>
<?php echo form_submit($submit); ?>
<?php echo form_close(); ?>
this is main.php in view folder
<?php
class Upload_model extends CI_Model{
var $original_path;
var $resized_path;
var $thumbs_path;
//initialize the path where you want to save your images
function __construct(){
parent::__construct();
//return the full path of the directory
//make sure these directories have read and write permessions
$this->original_path = realpath(APPPATH.'../uploads/original');
$this->resized_path = realpath(APPPATH.'../uploads/resized');
$this->thumbs_path = realpath(APPPATH.'../uploads/thumbs');
}
function do_upload(){
$this->load->library('image_lib');
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png', //only accept these file types
'max_size' => 2048, //2MB max
'upload_path' => $this->original_path //upload directory
);
$this->load->library('upload', $config);
$image_data = $this->upload->data(); //upload the image
print_r($image_data);
if($image_data){
echo "uploaded";
}else {echo "not upload";}
//your desired config for the resize() function
$config = array(
'source_image' => $image_data['full_path'], //path to the uploaded image
'new_image' => $this->resized_path, //path to
'maintain_ratio' => true,
'width' => 128,
'height' => 128
);
//this is the magic line that enables you generate multiple thumbnails
//you have to call the initialize() function each time you call the resize()
//otherwise it will not work and only generate one thumbnail
$this->image_lib->initialize($config);
$this->image_lib->resize();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->thumbs_path,
'maintain_ratio' => true,
'width' => 36,
'height' => 36
);
//here is the second thumbnail, notice the call for the initialize() function again
$this->image_lib->initialize($config);
$this->image_lib->resize();
}
}
this is my model
and controller only load main.php that allow me select image click submit and then controller call model above to load image
the problem is --> there are no error generate but image and thumbs not uploaded to directory why please?
hi according to the ci document you can resize images with image_lib and there are options that suggest we can create additional thumbnail from that image
create_thumb FALSE TRUE/FALSE (boolean) Tells the image processing function to create a thumb. R
thumb_marker _thumb None Specifies the thumbnail indicator. It will be inserted just before the file extension, so mypic.jpg would become mypic_thumb.jpg R
so here is my code
$config_manip = array(
'image_library' => 'gd2',
'source_image' => "./uploads/avatar/tmp/{$this->input->post('new_val')}",
'new_image' => "./uploads/avatar/{$this->input->post('new_val')}",
'maintain_ratio'=> TRUE ,
'create_thumb' => TRUE ,
'thumb_marker' => '_thumb' ,
'width' => 150,
'height' => 150
);
$this->load->library('image_lib', $config_manip);
$this->image_lib->resize();
i would assume this code resizes my image and also creates a thumbnail , but i only get one image with specified dimensions and _tump postfix
i've also tried to add this code to create second image manually but still it doesn't work and i get only one image
$this->image_lib->clear();
$config_manip['new_image'] =
"./uploads/avatar/thumbnail_{$this->input->post('new_val')}";
$config_manip['width'] = 30 ;
$config_manip['height'] = 30 ;
$this->load->library('image_lib', $config_manip);
$this->image_lib->resize();
It seems path is the issue in your code. I modified and tested myself it works.
public function do_resize()
{
$filename = $this->input->post('new_val');
$source_path = $_SERVER['DOCUMENT_ROOT'] . '/uploads/avatar/tmp/' . $filename;
$target_path = $_SERVER['DOCUMENT_ROOT'] . '/uploads/avatar/';
$config_manip = array(
'image_library' => 'gd2',
'source_image' => $source_path,
'new_image' => $target_path,
'maintain_ratio' => TRUE,
'create_thumb' => TRUE,
'thumb_marker' => '_thumb',
'width' => 150,
'height' => 150
);
$this->load->library('image_lib', $config_manip);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
// clear //
$this->image_lib->clear();
}
Hope this helps you. Thanks!!
Your code is Okay but you need to do a small change.
$this->load->library('image_lib');
$this->image_lib->initialize($config_manip);
A simple way to create a thumbnail.
function _create_thumbnail($fileName, $width, $height)
{
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['source_image'] = $_SERVER['DOCUMENT_ROOT']. $fileName;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = $width;
$config['height'] = $height;
$config['new_image'] = $_SERVER['DOCUMENT_ROOT']. $fileName;
$this->image_lib->initialize($config);
if (! $this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
}
If you want to create more than one image using resize() method, you need to call $this->image_lib->initialize($config); each time you attempt a resize.
This tutorial solved it for me Upload Image and Create Multiple Thumbnail Sizes in CodeIgniter