I am really struggling with my CodeIgniter script for uploading, resizing and cropping profile images for users. In the end, I want to upload the image with an encrypted name, resize it and save it twice (once as a thumbnail and once as a medium sized image), and then delete the original file. However, I guess I'm really missing something, because I can't get the first upload resize process to work. I definitely have an error somewhere in the code (error to follow code), but I think I may have a gap in my understanding of the logic here. Who knows? Any feedback is extremely appreciated .
public function image() {
$config['upload_path'] = './temp_images/'; // This directory has 777 access
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '0'; // For unlimited width
$config['max_height'] = '0'; // For unlimited height
$config['encrypt_name'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$data['error'] = array('error' => $this->upload->display_errors());
$this->load->view('upload', $data);
}
else
{
$image_data_array = array('upload_data' => $this->upload->data());
foreach ($image_data_array as $image_data){
$raw_name = $image_data['raw_name'];
$file_ext = $image_data['file_ext'];
}
$file_name = $raw_name . $file_ext;
$image_path = 'temp_images/' . $file_name;
$new_path = 'image/profile/'; // This directory has 777 access
$config['image_library'] = 'gd';
$config['source_image'] = $image_path;
$config['new_image'] = $new_path;
$config['maintain_ratio'] = FALSE;
$config['width'] = 140;
$config['height'] = 140;
$config['quality'] = '80%';
$new_name = $new_path . $raw_name . $file_ext;
$this->load->library('image_lib', $config);
// The following is just to test that it worked:
if ( ! $this->image_lib->resize()) {
echo $this->image_lib->display_errors();
echo 'source: ' . $config['source_image'] . '<br />';
echo 'new: ' . $config['new_image'] . '<br />';
}
else {
echo "<img src='" . base_url() . $new_name . "' />";
}
$this->image_lib->clear();
}
}
The upload process works fine. Even the resizing worked when I just called $this->image_lib->resize(). It was when I tried the error-catching that it started yelling at me. I double-verified that both of the temp_images and image/profile have 777 permissions (as aforementioned, the upload and resize actually worked at one point). Here is the error produced:
Unable to save the image. Please make sure the image and file directory are writable.
source: temp_images/8ee1bab383cf941f34218f7535d5c078.jpg
new: image/profile/
Never really used CI before and can't see the details of your code.
First, check the permission that set on the folder - you are trying to upload to. Also try to use the full server path.
Second, see if you have PHP image extensions such as GD, ImageMagick enabled.
i've tried this tutorial
it works well for image upload and create thumbs folder for the resizing image...
Please update the new_image value by including the filename also.
$config['new_image'] = 'image/profile/' . $filename;
Related
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'];
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.
I have an upload form that takes an image jpg/gif/etc, I use it to upload some images(image format is Jpg) for the gallery I use for my website, but unfortunately it doesn't work well for some images(It makes everything black and in the upper left corner(0, 0) it shows the resized images).
I tried resizing the images online and it works with no problem , So I think the problem is not with the image itself, It is in the library/configuration.
My upload AND resize image handler code:
function upload() {
//check if admin is logged in.
if ($this->session->userdata('is_logged') == 1) {
//loading the configuration for the upload library.
$config = array();
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '7000';
$config['max_width'] = '6000';
$config['max_height'] = '5000';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$data['redirect_msg'] = $this->upload->display_errors() . '<br /> go back to ' . anchor('admin/', 'main page');
$this->load->view('redirect', $data);
} else {
//getting the image's information, in order to resize it.
$upload_data = $this->upload->data();
$image_path = $upload_data['full_path'];
/* loading the configuration for the image manuiplation library. */
$config['image_library'] = 'gd';
$config['source_image'] = $image_path;
$config['width'] = '800';
$config['height'] = '600';
$config['maintain_ratio'] = FALSE; //tried setting it to TRUE aswell.
//loading the library
$this->load->library('image_lib', $config);
//starting the resize proccess.
$this->image_lib->resize();
//checking if the is an error in the resizing proccess.
if (!$this->image_lib->resize()) {
/* displaying the errors */
$data['redirect_msg'] = $this->image_lib->display_errors();
$this->load->view('redirect', $data);
} else {
/* show positive message. */
$data['redirect_msg'] = 'Upload successful!<br />redirecting to ' . anchor('admin/', 'homepage') . ' in 3 seconds';
$this->load->view('redirect', $data);
//saving the image name to a database table, so we can retrieve it when needed(for the slideshow).
$this->db_model->save_image($upload_data['orig_name']);
//doing the redirect.
header('refresh:3;url=' . site_url('admin/'));
}
}
} else { //if not logged in, show negative message.
$data['redirect_msg'] = 'you are not logged in. < br/> login ' . anchor('admin/', 'here');
$this->load->view('redirect', $data);
}
}
You are resizing that image two times:
//starting the resize proccess.
$this->image_lib->resize();
//checking if the is an error in the resizing proccess.
if (!$this->image_lib->resize()) {
just do it once:
//starting the resize proccess.
//checking if the is an error in the resizing proccess.
if (!$this->image_lib->resize()) {
I am working on media library for a website at the moment, and one of the features is that user can create a cropped version of an image that they upload.
My problem however is this, when I try and crop the image, I get the following error,
The path to the image is not correct.
Here is my code,
$config['image_library'] = 'gd2';
$config['source_image'] = "/media/images/products/".$this->data['image'];
//die($config['source_image']);
$config['maintain_ratio'] = FALSE;
$config['x_axis'] = $this->input->post('x');
$config['y_axis'] = $this->input->post('y');
$config['width'] = $this->input->post('w');
$config['height'] = $this->input->post('h');
$config['dynamic_output'] = FALSE;
$config['create_thumb'] = TRUE;
$this->load->library('image_lib', $config);
if(!$this->image_lib->crop()) {
if($this->input->post('isAjax') == "1") {
echo json_encode($this->image_lib->display_errors());
} else {
$this->data['image_error'] = $this->image_lib->display_errors();
$this->template->build('/admin/media/crop', $this->data);
}
} else {
$filename = base_url()."media/images/products/".$this->data['image'];
$extension_pos = strrpos($filename, '.'); // find position of the last dot, so where the extension starts
$thumb = substr($filename, 0, $extension_pos) . '_thumb' . substr($filename, $extension_pos);
if($this->input->post('isAjax') == 1) {
echo json_encode($success = array('message' => 'Image Cropped'));
} else {
$this->data['success'] = "Image Cropped";
$this->template->build('/admin/media/crop', $this->data);
}
}
So I though I would change $config['source_image'] to the following,
$config['source_image'] = "./media/images/products/".$this->data['image'];
however that just leaves with this message,
Your server does not support the GD function required to process this type of image.
Am I doing something fundamentally wrong? I am only trying to crop a simple .png, and the file certainly exists on my server, and I most definatly have GD2 installed.
The is most likely just a file path issue (CI is pretty good about accurate, relevant error messages). Here are the steps I would take to resolve it:
var_dump(file_exists($config['source_image']) to see if PHP finds the file. I'd be shocked by a "true" response here.
If the filesystem is case-sensitive, make sure that's not the problem
Check the include path (I assume regular includes are working fine if CI gets this far...)
More of an anti-step, but DON'T use $_SERVER['DOCUMENT_ROOT'], as a commenter suggests. FC_PATH (or other CI-defined constants) should get you the base path you need.
Happy hunting.
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.