Codeigniter Resize function not working - php

Hi friends I am trying to use codeigniters resize image_lib .. and I am not able to resize the image using this one. Please help me to solve this issue
Error:
using gd:
The path to the image is not correct.Your server does not support the GD function required to process this type of image.
using ImageMagick:
The path to the image is not correct.The path to your image library is not correct. Please set the correct path in your image preferences.
Code:
$this->load->library('upload');
$config['upload_path'] = $path;
$config['file_name'] = $file_name;
$config['allowed_types'] = 'jpg|png';
$config['overwrite'] = false;
$this->upload->initialize($config);
if ( ! $this->upload->do_upload())
{
return false;
}
else{
$this->load->library('image_lib');
$resize['image_library'] = 'gd2';
$resize['source_image'] = $path.$file_name;
$resize['maintain_ratio'] = FALSE;
$resize['width'] = 40;
$resize['height'] = 40;
$resize['quality'] = 100;
// print_r($path.$file_name);
// Here the path of the image is assets/Data/adv_images/2/2-537f2a3651300
// which is absolutely right also tried base_url().$path.$file_name
$this->image_lib->initialize($resize);
if ( ! $this->image_lib->resize()){
echo $this->image_lib->display_errors();
}
return true;
}

try to set path by this way:
$resize['source_image'] = $this->upload->data()['full_path'];

Related

How to upload image, resize it and how the resized image is displayed in codeigniter?

my controller:
public function addgalleryProcess()
{
$height = $this->input->POST('height');
$width = $this->input->POST('width');
$config['upload_path'] = './assets/images/gallery';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '';
$config['max_height'] = '';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('admin/addgallery', $error);
}
else
{
$upload_data = $this->upload->data();
//resize:
$config1['image_library'] = 'gd2';
$config1['source_image'] = $upload_data['full_path'];
$config1['maintain_ratio'] = TRUE;
$config1['create_thumb'] = TRUE;
$config1['width'] = $width;
$config1['height'] = $height;
$this->load->library('image_lib', $config1);
$this->image_lib->resize();
$this->adminModel->galleryimages($upload_data);
$this->load->view('admin/homeView');
}
my model:
public function galleryimages($image_data = array())
{
$data = array(
'image' => $image_data['file_name'],
);
$this->db->insert('gallery', $data);
}
Here image is uploaded properly and working, but resize is not working. I have to display the resized image with specified width and height. I am new to this.
Thanking in advance.
Try this:
For image resize I suggest to use "TimThumb" TimThumb – PHP Image Resizer
https://www.binarymoon.co.uk/projects/timthumb/
I have use timthumb in my project like
<img src="<?php echo base_url('assets/common/timthumb') . '/timthumb.php?src=./assets/myuploads/myimagename.jpg&w=245&h=164'; ?>" alt="">
I have put timthumb.php in "assets" folder and I uploaded my images in "assets/myuploads" folder
Not to be to distracting, if you want to resize the image before upoading through the networks, you can javascript it to a canvas and then send the canvas image in toDataURL. This would help prevent someone from uploading something massive and your network and server struggle with it.
This also depends if your client in a HTML client.
The following Article can help with this,:
https://stackoverflow.com/a/10334170/811827

Images get rotated by 90 Whole resizing using gd2 in CodeIgniter

I had used gd2 library to resize image.But some images got rotated by 90 degree after resized but not all image.I hadn't used any rotation.What may be the reason behind this?Do I need to set anything other configuration?
My code is here :
if(isset($_FILES) && !empty($_FILES)){
$item_images = $_FILES;
$this->load->library('upload');
$this->load->library('image_lib');
$config['upload_path'] = './temp/';
$config['allowed_types'] = '*';
$this->upload->initialize($config);
//print_r($item_images);
foreach($item_images as $ind=>$img_name){
if($this->upload->do_upload($ind)){
$data_image = $this->upload->data();
//for cropping
$config_crop['image_library'] = 'gd2';
$config_crop['source_image'] = $config['upload_path'] . $data_image['file_name'];
$config_crop['create_thumb'] = FALSE;
$config_crop['maintain_ratio'] = TRUE;
$config_crop['width'] = 300;
$config_crop['height'] = 300;
//$config_crop['rotation_angle'] = '0';
//$config_crop['x_axis'] = '100';
//$config_crop['y_axis'] = '40';
$this->image_lib->initialize($config_crop);
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
}
$image_thumb = $this->s3_model->upload_image('thumb_'.$img_name['name'] ,$config['upload_path'] . $data_image['file_name']);
if($image_thumb){
$result = $this->s3_model->upload_image($img_name['name'] ,$img_name['tmp_name']);
//unlink($config['upload_path'] . $data_image['file_name']);
}
if(! $result){
$flag = false; //Error in uploading image.
break;
}
}
}
Do you have a OSx working machine?
In my case, a pic, that in my Mac was shown correctly, indeed was turned sideways, that is because OSX preview detects CellPhone orientation while the pic was taken....
The server not, neither windows machines... so that was driving me crazy...
Being or not this your case, you can use exif_read_data() function. This will give you orientation data if it exists on photo's metadata.
Check here: How to detect shot angle of photo, and auto rotate for website display like desktop apps do on viewing?

Failed to create thumb image in $_FILES loop

I iterate through $_FILES to create thumb images of uploaded pictures. It works fine for the first image but fails for the following pictures. Do I miss to add a special line or there is flow in my code?
Note: Original files gets uploaded successfully and exist in folder before creating thumb out of them.
When I echo error, I get this: "Your server does not support the GD function required to process this type of image.". When I upload it on its own, it works!!!!
Thanks
public function upload_image()
{
$config['upload_path'] = './web/uploads/images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 5120;
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['encrypt_name'] = true;
$this->load->library('upload');
$this->upload->initialize($config);
foreach ($_FILES as $file => $value)
{
$this->upload->do_upload($file);
$result = $this->upload->data();
if ($this->manipulate_image($result['file_name']) === false)
{
echo 'Failed to create thumb for the image ' . $value['name'] . '<br />';
}
}
}
public function manipulate_image($file_name)
{
$config['image_library'] = 'gd2';
$config['source_image'] = './web/uploads/images/' . $file_name;
$config['create_thumb'] = true;
$config['maintain_ratio'] = false;
$config['width'] = 100;
$config['height'] = 100;
//$config['master_dim'] = 'width';
$config['thumb_marker'] = '_thumb';
$this->load->library('image_lib', $config);
if (! $this->image_lib->resize())
{
$this->image_lib->clear();
return false;
}
$this->image_lib->clear();
return true;
}
I see two things, first, I would move the load on the library outside of the foreach loop then use initialize inside the loop to set the config:
$this->image_lib->initialize($config);
Also, as documented here you can use
echo $this->image_lib->display_errors();
To get more insight on your problem
Apparently loading loading image_lib multiple times in a loop causes this problem.
Solved without including in loop.

Trying to resize an image twice in a function and it's only resizing it once

I'm trying to have a user upload a file and then my function will resize that photo to something like 800x600 and then resize that file to 165x165. With my code, it's only resizing it to 165x165, but if I comment that section out, it resizes it to 800x600.
What can I do to get it to resize both?
/**
* addPhoto - function which shows the add photo page
*/
function addPhoto($album)
{
$album = rawurldecode($album);
ini_set('upload_max_filesize', '4M');
ini_set('post_max_size', '12M');
ini_set('max_input_time', 300);
ini_set('max_execution_time', 300);
$config['upload_path'] = './assets/images/photoAlbums/'.$album.'/';
$config['allowed_types'] = 'jpg|jpeg|pjpeg';
$config['max_size'] = '4096'; // that's 4MBs
$config['max_width'] = '4000';
$config['max_height'] = '3000';
$config['remove_space'] = TRUE;
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
if(!$this->upload->do_upload('userFile')) { // if there are errors uploading the file...
$content_data['error'] = array('error' => $this->upload->display_errors());
} else { // else there are NO errors, we need to resize it, and ditch that huge ass original
$image = $this->upload->data();
$uploadedFile = $image['file_name'];
$this->load->library('image_lib');
$thumbConfig['image_library'] = 'gd2';
$thumbConfig['source_image'] = $image['full_path'];
$thumbConfig['create_thumb'] = FALSE;
$thumbConfig['maintain_ratio'] = TRUE;
$thumbConfig['width'] = 800;
$thumbConfig['height'] = 600;
$this->image_lib->initialize($thumbConfig);
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
$this->image_lib->clear();
$thumbConfig['image_library'] = 'gd2';
$thumbConfig['source_image'] = $image['full_path'];
$thumbConfig['create_thumb'] = TRUE;
$thumbConfig['maintain_ratio'] = FALSE;
$thumbConfig['width'] = 165;
$thumbConfig['height'] = 165;
$this->image_lib->initialize($thumbConfig);
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
$content_data['firstName'] = $this->session->userdata('firstName');
$data['sess'] = $this->session;
$data['content'] = $this->load->view('member/addPhoto_success', $content_data, true);
$this->load->view('template/admin', $data);
}
}
for the second time running the image_lib, you're going to want to avoid overwriting the file by specifying the new file for it to create:
after:
$thumbConfig['height'] = 165;
add:
$thumbConfig['new_image'] = $image['file_path'] . "thumb_" . $image['file_name'];
so if your resized 800 x 600 image resides at /my/upload/path/foo.jpg,
this will create a thumb at /my/upload/path/thumb_foo.jpg.
If you want to create two new resized images and leave the original intact, you need to add
$config['create_thumb'] = TRUE;
or
$config['new_image'] = '/path/to/new_image.jpg';
to the first section.
According to your code, you will overwrite the original file with an 800x600 version and then create a 165x165 thumbnail.
I think you are missing this line on the first resize:
$thumbConfig['source_image'] = $image['full_path'];
I know this is a very old question, but I just ran into the same issue.
$config['create_thumb'] = true
did not create a new file.
Adding the thumb marker configuration:
$config['thumb_marker'] = '_thumb'
worked.
It seems that _thumb is not the default for thumb_marker.

Upload problem with PHP: Thumbnail picking up the image name

I have the following model(codeigniter) code to upload image and thumbnail.
However the result of image path becomes, images/comfort_big.jpg and images/comfort_big.jpg.jpg.
The thumbnail image picks up the name of image and add .jpg.
I am uploading images/confort_thumb.jpg for thumb.
Can anyone tell me what's wrong please?
if ($_FILES){
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '200';
$config['remove_spaces'] = true;
$config['overwrite'] = false;
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if (strlen($_FILES['image']['name'])){
if(!$this->upload->do_upload('image')){
$this->upload->display_errors();
exit();
}
$image = $this->upload->data();
if ($image['file_name']){
$data['image'] = "images/".$image['file_name'];
}
}
if (strlen($_FILES['thumbnail']['name'])){
if(!$this->upload->do_upload('thumbnail')){
$this->upload->display_errors();
exit();
}
$thumb = $this->upload->data();
if ($thumb['file_name']){
$data['thumbnail'] = "images/".$thumb['file_name'];
}
}
}
I had a quick look at File Uploading Class in the user guide
In the preferences tables it states:
Note:The filename should not include a file extension.
So my guess is that you get the user to name the file
$config['file_name'] = "User defined";
or remove the extension programmatically. Since you know and list the extensions already you could do
$types = array(".gif",".jpg",".png");
$image['file_name'] = str_replace($types , "", $image['file_name'] );
Your problem is that when you create a thumb you are passing the full image.
So it will add another extension to it.
Use Below code to remove extension from file name.
<?php
$filename=$image['file_name'];
$file_name_with_dot = substr($filename,0, strrpos($filename, '.') + 1);
$only_file_name = substr($file_name_with_dot,0,strlen($file_name_with_dot)-1);
?>
Now pass this $only_file_name variable to your thumb function..
It will be better to use automatic thumb creation from main image.
Hope this will help for you.

Categories