it gives blank screen :/ it uploads first image, thats fine. than it call _create_thumbnail and it gives blank screen at line "$this->image_lib->resize()" :/
any idea what can be the problem?
Thanks!!
/**
* ==================================================================
* Upload photo
*
* Thumb = 210px - 160px
* Original = 500px - 385px
*
*/
function img_upload()
{
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$config['width'] = 500;
$config['height'] = 385;
$this->load->library('upload', $config);
if(!$this->upload->do_upload()) echo $this->upload->display_errors();
else {
$fInfo = $this->upload->data();
$this->_create_thumbnail($fInfo['file_name']);
$data['uploadInfo'] = $fInfo;
$data['thumbnail_name'] = $fInfo['raw_name'] . '_thumb' . $fInfo['file_ext'];
// set view
$this->load->view('upload_success', $data);
}
}
function _create_thumbnail($fileName)
{
$config['image_library'] = 'gd2';
$config['source_image'] = 'uploads/' . $fileName;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 210;
$config['height'] = 160;
$this->load->library('image_lib', $config);
if(!$this->image_lib->resize()) echo $this->image_lib->display_errors();
}
Check your curly braces here:
if(!$this->upload->do_upload()) echo $this->upload->display_errors();
else {
it looks like you're mixing syntax styles.
try this
function _create_thumbnail($fileName)
{
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/' . $fileName;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 210;
$config['height'] = 160;
$config['new_image'] = './uploads/'.$fileName;
$this->image_lib->initialize($config);
if(!$this->image_lib->resize()) echo
$this->image_lib->display_errors();
}
Try removing $fileName:
Instead of this: $config['source_image'] = 'uploads/' . $fileName;
Use this: $config['source_image'] = 'uploads/'
Related
I'm trying to resize and then upload an image in CodeIgniter. It uploads the image but it doesn't resize it.
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['upload_path'] = './assets/uploads';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['quality'] = '60%';
$config['width'] = 200;
$config['height'] = 200;
$this->load->library('image_lib', $config);
$this->load->library('upload', $config);
$this->image_lib->resize()
if ($this->upload->do_upload('resim')) {
$data_upload_files = $this->upload->data();
$image = $data_upload_files['file_name'];
} else {
//echo $this->upload->display_errors('<p>', '</p>');
}
Firstly, you are attempting to resize before the file is actually uploaded. Secondly, you need to give the image manipulation class the full path to the uploaded image (again, after you uploaded the image). Thirdly, it a good idea to use different variable names for the different configs:
$config['upload_path'] = './assets/uploads';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
if ($this->upload->do_upload('resim')) {
$file_data = $this->upload->data();
$resize['image_library'] = 'gd2';
$resize['create_thumb'] = TRUE;
$resize['maintain_ratio'] = TRUE;
$resize['quality'] = '60%';
$resize['width'] = 200;
$resize['height'] = 200;
$resize['source_image'] = $file_data['full_path'];
$this->load->library('image_lib', $resize);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
} else {
echo $this->upload->display_errors('<p>', '</p>');
}
I have problem with image class in CI?
This is example
On controller:
$this->load->library( array('image_lib') );
On view i have this:
foreach($results as $row)
{
$config['image_library'] = 'gd2';
$config['source_image'] = '/img/proizvodi/'.$row->proizvodid.'.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();
}
But i got no error, and thumb image is not created?
I dont know where is mistake?
You shouldn't load image_lib in foreach. Try to use code below
$this->load->library('image_lib');
foreach($results as $row) {
$config['image_library'] = 'gd2';
$config['source_image'] = '/img/proizvodi/'.$row->proizvodid.'.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();
}
If it wont work - check the upload folder permissions.
function uploadimage()
{
$file_name = $this->input->post('fileName');
$imageName = 'set_'.file_name($_FILES["file"]['name'],date('Ymd_his'));
$config['image_library'] = 'gd2';
$config['source_image'] = $imageName;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['master_dim'] = 'width';
upload_resize('file','settings', $imageName );
$config['width'] = 200;
$config['height'] = 62;
$this->load->library('image_lib', $config);
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
$json_data = array(
'error' =>false,
'tmp_name' =>$file_name,
'file_name' =>base_url().'assets/uploads/settings/'.$imageName.'?dummy='.rand(0,1000),
'file_path' =>$imageName
);
echo json_encode($json_data);
}
}
this code upload image to folder before save to table.
$config['upload_path'] = './berkas/content/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 0;
$config['max_width'] = 1024000;
$config['max_height'] = 7680000;
$config['encrypt_name'] = TRUE;
$config['overwrite'] = FALSE;
$this->load->library('upload', $config);
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
$params['gambar'] = $file_name;
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['source_image'] = './berkas/content/'.$file_name;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 550;
$config['height'] = 350;
$params = array('gambar' => $file_name);
If the destination image contains a different aspect ratio than the source image, then the source image will be cropped in the same ratio of the destination image have.
public function resize_image($image_data){
$this->load->library('image_lib');
$w = $image_data['image_width']; // original image's width
$h = $image_data['image_height']; // original images's height
$n_w = 273; // destination image's width
$n_h = 246; // destination image's height
$source_ratio = $w / $h;
$new_ratio = $n_w / $n_h;
if($source_ratio != $new_ratio){
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/uploaded_image.jpg';
$config['maintain_ratio'] = FALSE;
if($new_ratio > $source_ratio || (($new_ratio == 1) && ($source_ratio < 1))){
$config['width'] = $w;
$config['height'] = round($w/$new_ratio);
$config['y_axis'] = round(($h - $config['height'])/2);
$config['x_axis'] = 0;
} else {
$config['width'] = round($h * $new_ratio);
$config['height'] = $h;
$size_config['x_axis'] = round(($w - $config['width'])/2);
$size_config['y_axis'] = 0;
}
$this->image_lib->initialize($config);
$this->image_lib->crop();
$this->image_lib->clear();
}
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/uploaded_image.jpg';
$config['new_image'] = './uploads/new/resized_image.jpg';
$config['maintain_ratio'] = TRUE;
$config['width'] = $n_w;
$config['height'] = $n_h;
$this->image_lib->initialize($config);
if (!$this->image_lib->resize()){
echo $this->image_lib->display_errors();
} else {
echo "done";
}}
I have prepared a step by step guide on how to resize image in codeigniter without loosing quality
I have tried below code. Please check.
if (isset($_FILES['image']) && !empty($_FILES['image']['name'])) {
$upload_path="uploads/foldername/";
$newFileName = explode(".",$_FILES['image']['name']);
$filename = time()."-".rand(00,99).".".end($newFileName);
$filename_new = time()."-".rand(00,99)."_new.".end($filename);
$config['file_name'] = $filename;
$config['upload_path'] = $upload_path;
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
if ($this->upload->do_upload('image'))
{
//Image Resizing
$config1['source_image'] = $this->upload->upload_path.$this->upload->file_name;
$config1['new_image'] = 'uploads/foldername/'.$filename_new;
$config1['maintain_ratio'] = FALSE;
$config1['width'] = 181;
$config1['height'] = 181;
$this->load->library('image_lib', $config1);
if ( ! $this->image_lib->resize()){
$this->session->set_flashdata('message', $this->image_lib->display_errors('', ''));
}
$post_data['image'] = $filename;
}
else
{
$this->session->set_flashdata('msg',$this->upload->display_errors());
}
}
The problem is that you are loading the library inside the loop, that is the error, if you edit 10 images you will load the library 10 times, big mistake, the library must be loaded in the constructor or out of the loop.
Wrong
foreach($results as $row)
{
$config['image_library'] = 'gd2';
$config['source_image'] = '/img/proizvodi/'.$row->proizvodid.'.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();
}
Right
$this->load->library('image_lib');
foreach($results as $row)
{
$config['image_library'] = 'gd2';
$config['source_image'] = '/img/proizvodi/'.$row->proizvodid.'.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
}
May this function will help someone
function compressimg($gbr,$path){
$wd =$gbr['image_width']*0.4; //0.4 mean compress to 40% image_size
$hh =$gbr['image_height']*0.4; //modify yours
//Compress Image
$config['image_library'] = 'gd2';
$config['source_image'] = $path.$gbr['file_name'];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = FALSE;
$config['quality'] = '50%';
$config['width'] = round($wd);
$config['height'] = round($hh);
$config['new_image'] = $path.$gbr['file_name'];
$this->load->library('image_lib', $config);
$this->image_lib->resize();
return $gbr['full_path'];
// Put This On Uploader , by user4xn
// return $this->compressimg($this->upload->data(),$config['upload_path']);
}
I'm uploading profile pictures to my website and the first upload is working fine, but when I try and resize the image and reupload it to the same folder it breaks. From testing, I know it is going through the array and it is inserting the data into the database, but just not re-saving into the folder.
This is what I have going one.
$config['upload_path'] = './uploads/';
$config['file_name'] = $this->user->pen_name.'.jpg';
$config['file_path'] = './uploads/'.$this->user->pen_name.'.jpg';
$config['max-size'] = 2000;
$config['overwrite'] = TRUE;
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['remove_spaces'] = true;
$config['max_width'] = 2000;
$config['max_height'] = 3000;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload()){
$error = array('error' => $this->upload->display_errors());
}else{
$resize['source_image'] = base_url().'/uploads/'.$config['file_name'];
$resize['new_image'] = './uploads/';
$resize['file_path'] = './uploads/'.$this->user->pen_name.'.jpg';
$resize['create_thumb'] = false;
$resize['maintain_ratio'] = true;
$resize['width'] = 200;
$resize['height'] = 300;
$resize['overwrite'] = TRUE;
$this->load->library('image_lib', $resize);
$this->image_lib->resize();
$this->load->library('upload', $resize);
$this->users_model->uploadPic($resize['file_path']);
redirect($_SERVER['HTTP_REFERER']);
}
Any help would be much appreciated!
Just had the same issue, and solved it as follows:
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2048';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('logoUpload')) {
$this->data['error'] = array('error' => $this->upload->display_errors('<div class="alert alert-danger">', '</div>'));
//error
} else {
$upload_data = $this->upload->data();
//resize:
$config['image_library'] = 'gd2';
$config['source_image'] = $upload_data['full_path'];
$config['maintain_ratio'] = TRUE;
$config['width'] = 150;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
//add to the DB
$this->settings_model->upload_logo($upload_data);
}
These are the default config from codeigniter, just put unlink at the end:
$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();
//remove original file
#unlink( $config['source_image'] );
I've been attempting to resize images but with no luck! I have the resize inside the upload function but not sure what is happening wrong! The code below shows my upload function (and resize within it):
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2048';
$this->load->library('image_lib', $config);
$this->load->library('upload', $config);
$fullImagePath = '';
if (! $this->upload->do_upload())
{
$this->upload->display_errors('<p style="color: maroon; font-size:large;">', '</p>');
$error = array('file_error' => $this->upload->display_errors());
// print_r($error);
$this->load->view('layout/header');
$this->load->view('add_gallery', $error);
$this->load->view('layout/footer');
}else{
//echo "UPLOAD SUCCESS";
// set a $_POST value for 'image' that we can use later
/*Image Manipulation Class*/
$config['image_library'] = 'gd2';
$config['source_image'] = $fullImagePath;
echo $fullImagePath;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['max_width'] = '480';
$config['max_height'] = '640';
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$upload_data = $this->upload->data();
$fullImagePath = '/uploads/' . $upload_data['file_name'];
}
return $fullImagePath;
}
The upload works fine and I get the fullImagePath (link) to store in database. ANyway, just not sure how to handle the resize.
The resize() function requires that you first have configured the source_image before calling it, so it can apply the resize to the specified image.
You are sending an empty path.
Here you declare the path as empty string $fullImagePath = ''; and then here you define it to the configuration options $config['source_image'] = $fullImagePath; and after that you call the $this->image_lib->resize(); , so it can not do resize to an empty image.
Also you are loading twice the image_lib which is not needed.
I modified your code. Test it and see if it works
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2048';
$this->load->library('upload', $config);
$fullImagePath = '';
if (! $this->upload->do_upload()){
$this->upload->display_errors('<p style="color: maroon; font-size:large;">', '</p>');
$error = array('file_error' => $this->upload->display_errors());
$this->load->view('layout/header');
$this->load->view('add_gallery', $error);
$this->load->view('layout/footer');
}else{
$upload_data = $this->upload->data();
$fullImagePath = '/uploads/' . $upload_data['file_name'];
$config['image_library'] = 'gd2';
$config['source_image'] = $fullImagePath;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['max_width'] = '480';
$config['max_height'] = '640';
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
return $fullImagePath;
}
I tried the following code to crop an image in codeigniter but it did not work. Might be I am missing minute thing. Helper is loaded and the image is also exists
The code is
public function cropAndSave(){
$config['image_library'] = 'gd2';
$config['allowed_types'] = 'gif|jpg|png';
$config['source_image'] = './img-lab/xxx.jpg';
$config['create_thumb'] = true;
$config['maintain_ratio'] = false;
$config['width'] = 150;
$config['height'] = 190;
$config['new_image'] = "thumb_shahid.jpg";
$this->load->library('image_lib', $config);
$this->image_lib->resize();
echo '
<script>
window.parent.location="'. base_url().'"
</script>
';
}
Call the initialize() function instead of load() because your image library already loaded
public function cropAndSave(){
$config['image_library'] = 'gd2';
$config['allowed_types'] = 'gif|jpg|png';
$config['source_image'] = './img-lab/xxx.jpg';
$config['create_thumb'] = true;
$config['maintain_ratio'] = false;
$config['width'] = 150;
$config['height'] = 190;
$config['new_image'] = "thumb_shahid.jpg";
$this->image_lib->initialize($config);
$this->image_lib->resize();
echo '
<script>
window.parent.location="'. base_url().'"
</script>
';
}