Codeigniter image manipulator Unable to save the image - php

Well i have the following problem:
I'm using CodeIgniter 2.1.3 and want to upload an image and create two scaled images. one that is resized to the size of around 50x50 and one that is resized to around 200x200.
Uploading is doing fine.
But when i want to resize the following error occures:
Unable to save the image. Please make sure the image and file directory are writable.
I've set my permission on the server to 777, but it still doesn't compute
Here is the code of the classes:
public function uploadimage()
{
$head = array('title' => 'Upload Image');
$error = array('error' => '');
$this->load->view('head',$head);
$this->load->view('imageUpload', $error);
$this->load->view('footer');
}
public function changeimage()
{
$config = array();
$config['upload_path'] = 'img/Upload';
$config['allowed_types'] = 'gif|jpg|jpeg|png|bmp';
$config['max_size'] = '100';
$config['max_width'] = '200';
$config['max_height'] = '200';
$config['overwrite'] = TRUE;
$config['file_name'] = md5($this->session->userdata('username')).".png";
$head = array('title' => 'Upload Image');
$this->upload->initialize($config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('head',$head);
$this->load->view('imageUpload', $error);
$this->load->view('footer');
}
else
{
$uploaddata = $this->upload->data();
//Create Thumb
$config2 = array();
$config2['image_library'] = 'gd';
$config2['source_image'] = $uploaddata['full_path'];
$config2['maintain_ratio'] = TRUE;
$config2['width'] = 50;
$config2['height'] = 50;
$config2['new_image'] = '/~3612546/DateSite/img/ProfileThumbs/'.$uploaddata['file_name'];
$this->image_lib->initialize($config2);
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
//Create resized original image so images < 200x200 get resized to get as close to 200x200
$config3 = array();
$config3['image_library'] = 'gd';
$config3['source_image'] =$uploaddata['full_path'];
$config3['maintain_ratio'] = TRUE;
$config3['width'] = 200;
$config3['height'] = 200;
$config3['new_image'] = '/~3612546/DateSite/img/ProfileImages/'.$uploaddata['file_name'];
$this->image_lib->initialize($config3);
if ( ! $this->image_lib->resize())
{
echo $uploaddata['full_path'];
echo $this->image_lib->display_errors();
}
}
}

i really don't think tilde is good for upload urls:
/~3612546/DateSite/img/ProfileImages/
then, all your urls are upper/lowercase, put them all to lowercase like this:
/~3612546/dateSite/img/profileimages/
in the end post what error do you receive now that you chmod 777 your upload folder, do you added 777 only to dir or dir+all his content? (it's important dir+all his content to 777)

Related

Files not getting stored in correct folder

I have a form in CodeIgniter that allows the user to upload 2 separate files. At the backend I want these files to get stored in the different folder. In the controller i have written the upload code
public function upload()
{
/**Start uploading file**/
$config['upload_path'] = './assets/file/.';
$config['allowed_types'] = 'gif|jpg|png|doc|txt';
$config['max_size'] = 1024 * 8;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('file'))
{
$error = array('error' => $this->upload->display_errors());
echo $error;
}
else
{
$data = $this->upload->data();
echo $file = $data['file_name']; //name of file
}
/**End uploading file**/
/**Start uploading img**/
$config2['upload_path'] = './assets/img/.';
$config2['allowed_types'] = 'gif|jpg|png|doc|txt';
$config2['max_size'] = 1024 * 8;
$config2['encrypt_name'] = TRUE;
$this->load->library('upload', $config2);
if (!$this->upload->do_upload('img'))
{
$error1 = array('error' => $this->upload->display_errors());
echo $error1;
}
else
{
$data1 = $this->upload->data();
echo $img = $data1['file_name']; //name of img
}
/**End uploading img**/
}
The images are getting uploaded but they are getting uplaoded to same folder. Can anyone please tell how i can make the files get saved in seperate folders
in the second load , you need to initialiaze the loaded library because the load method don't initialize it :
$this->upload->initialize($config2);

Codeigniter - Two images upload did not work

I tried to upload two images using a form. To do that I wrote a image function.
protected function imageUploadResize($filename, $field_name, $width, $height, $maker){
// thumb image upload settings
$config['upload_path'] = './uploads/packages/';
$config['allowed_types'] = 'gif|jpg|png';
// thumb upload settings
$image_new_name = time().'_'.$filename;
$config['file_name'] = $image_new_name;
$this->load->library('upload', $config);
// upload thumb image
if ( $this->upload->do_upload($field_name)) {
// resize uploaded file
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/packages/'.$image_new_name;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = $width;
$config['height'] = $height;
$config['thumb_marker'] = $maker;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$error = array('error' => $this->upload->display_errors());
} else {
$imageData = array('upload_data' => $this->upload->data());
}
return $image_new_name;
}
And I access that function this way,
// allocate image name to data set
if(!empty($_FILES["thumb_image"]['name'])){
// thumb upload
$thumb_image_new_name = $this->imageUploadResize($_FILES["thumb_image"]['name'],'thumb_image',THUMB_IMAGE_WIDTH, THUMB_IMAGE_HEIGHT, '_thumb');
$data['thumb_image'] = $thumb_image_new_name;
}else{
$data['thumb_image'] = "";
}
// allocate image name to data set
if(!empty($_FILES["banner_image"]['name'])){
// banner upload
$banner_image_new_name = $this->imageUploadResize($_FILES["banner_image"]['name'],'banner_image',BANNER_IMAGE_WIDTH, BANNER_IMAGE_HEIGHT, '_banner');
$data['banner_image'] = $banner_image_new_name;
}else{
$data['banner_image'] = "";
}
When I upload one image (thumb_image or banner_image) above function is worked properly.
But when I upload both images thumb_image uploaded properly but banner_image did not upload properly. As a example assume I am going to upload Hydrangeas.jpg and Desert.jpg. Then it is working this way,
Then file uploaded this way (images names of the folder),
1493025280_Hydrangeas.jpg
1493025280_Hydrangeas_thumb.jpg
1493025280_Hydrangeas1.jpg - image name also wrong
But expected output is (images names of the folder),
1493025280_Hydrangeas.jpg
1493025280_Hydrangeas_thumb.jpg
1493025280_Desert.jpg
1493025280_Desert_banner.jpg
Can someone please help me, thank you..
A different approach that i use for file uploads is below.
I rearrange the $_FILES array because the original has some difficulties with keys and objects.
$files = array();
foreach ($_FILES['files']['name'] as $num_key => $dummy) {
foreach ($_FILES['files'] as $txt_key => $dummy) {
$files[$num_key][$txt_key] = $_FILES['files'][$txt_key][$num_key];
}
}
In new array, loop through each image and do what you need:
foreach ($files as $file) {
if ($file['error'] == 0) {
//Your code here...
//Call your function imageUploadResize
}
}
Finally found the solution, In codeigniter when upload image, we have to call,
$this->load->library('upload', $config);
Then we have to initialized the configurations.
$this->upload->initialize($config);
And when resize image we have to call,
$this->load->library('image_lib', $config);
Then have to initialized the configurations.
$this->image_lib->initialize($config);
Therefore completed function is,
protected function imageUploadResize($filename, $field_name, $width, $height, $maker){
// thumb image upload settings
$config['upload_path'] = './uploads/packages/';
$config['allowed_types'] = 'gif|jpg|png';
// thumb upload settings
$image_new_name = time().'_'.$filename;
$config['file_name'] = $image_new_name;
$this->load->library('upload', $config);
$this->upload->initialize($config);
// upload thumb image
if ( $this->upload->do_upload($field_name)) {
// resize uploaded file
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/packages/'.$image_new_name;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = $width;
$config['height'] = $height;
$config['thumb_marker'] = $maker;
$this->load->library('image_lib', $config);
$this->image_lib->initialize($config);
$this->image_lib->resize();
$error = array('error' => $this->upload->display_errors());
} else {
$imageData = array('upload_data' => $this->upload->data());
}
return $image_new_name;
}

Resize image and upload

I want to write a php code for resizing images that are retrieved from database and upload it in the site. But i can't resize the images. That are directly uploaded into site without resize.. My code is given below ..thanks in advance ...
public function editimg()`enter code here`
{
$config['image_library'] = 'gd2';
$id=$this->input->post('hiddenimgid');
$config['upload_path'] = './assets/img/movies';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '150';
$config['max_width'] = '199';
$config['max_height'] = '199';
//echo "hii";die;
// print_r($config);die;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
//echo "not uploaded";die;
$error = array('error' => $this->upload->display_errors());
$data['message'] = "Image Cannot be Uploaded,Try Again !!";
$data['error']=$error;
$this->index_one($data);
} //if
else
{
//echo "hii";die;
$config['image_library'] = 'gd2';
$image_data = $this->upload->data();
$filename = $image_data['file_name'];
$source_path = 'assets/img/movies/'.$filename ;
$new_image_path = 'assets/img/movies/'.$image_data['raw_name'].'_thumb'.$image_data['file_ext']; //name of resized image
$target_path = 'assets/img/movies/';
$thumb['image_library'] = 'gd2';
$thumb['source_image'] = $source_path;
$thumb['new_image'] = $target_path;
$thumb['create_thumb'] = TRUE;
$thumb['maintain_ratio'] = TRUE;
$thumb['width'] = 140;
$thumb['height'] = 200;
//print_r($thumb);die;
//$this->load->library('image_lib', $config);
$this->load->library('image_lib', $thumb);
if ( !$this->image_lib->resize())
{
//echo "hii";die;
//$this->session->set_flashdata('errors', $this->image_lib->display_errors('', ''));
$this->session->set_flashdata('errors', $error= $this->image_lib->display_errors('', ''));
print_r($error);die;
}
else
{
//echo "hii";die;
//print_r($thumb);die;
$data=array(
'image'=>$new_image_path
);
//print_r($new_image_path);die;
if($this->movies_model->updateMovies($data,$id))
{
$data['message'] = "Movies added successfully !!";
//$this->index_one($data);
redirect("movies/index", 'refresh');
//print_r($thumb);die;
}
else
{
$data['message'] = "not_uploaded !!";
$this->index_one($data);
}
}
}
}
`
It simple to use Simple image
<?php
include('src/abeautifulsite/SimpleImage.php');
try {
$img = new abeautifulsite\SimpleImage('/*image name*/');
$img->resize(320, 200)->save('/*name of resize image*/');resize image and save resized image
} catch(Exception $e) {
echo 'Error: ' . $e->getMessage();
}
Oleh is correct, if you're struggling with image resizing it's best to use a library to handle all the edge cases and gotchas.
Here's how to do it with SimpleImage 2.x:
$image = new \abeautifulsite\SimpleImage('image.jpg');
$image->resize(320, 200)->save('result.jpg');
And SimpleImage 3.x:
$image = new \claviska\SimpleImage('image.jpg');
$image->resize(320, 200)->toFile('output.jpg');
The SimpleImage library can be installed with Composer:
composer require claviska/simpleimage
Or downloaded from GitHub and included manually.

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

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.

Categories