image cropping with codeigniter no GD library - GD2 is intalled - php

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.

Related

Image_lib in Codeigniter doesn't produce output

It would seem as though this is a rather common problem, but I haven't been able to locate an answer to my problem, nor have I been able to come up with a solution myself. I am trying to use the resize method of the image_lib class in order to make some images smaller. Here's the code:
function manipulate_picture($img_path, $hashed_id) {
$destination_image = 'img/entries_small/' . $hashed_id;
$config['image_library'] = 'GD2';
$config['source_image'] = $img_path;
$config['maintain_ratio'] = FALSE;
$config['width'] = 130;
$config['height'] = 75;
$config['new_image'] = $destination_image;
$this->CI->load->library('image_lib', $config);
$this->CI->image_lib->resize();
echo '<img src="'.base_url($config['new_image']).'">';
return $destination_image;
}
Which is called by the original image's path and the hashed_id of the same image. The echo code is obviously just for testing - however it is not producing any output (and there's no files to be found on the server either). I have checked that GD2 is enabled through phpinfo(). I've also checked that the image_lib gets loaded after having called load.
I really have no idea on what to do with this one.

CodeIgniter image upload and resize failing

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;

Codeigniter Image Manipulation Class : Resize and Crop on multiple files

I'm struggling to get the CodeIgniter Image Manipulation working correctly. Either it's a bug or I'm just not seeing it. I hope someone can help me with it. Thanks in advance!
On the script: I want to create thumbnails (176w x 132h). The input images are in different sizes and ratios. In order to always get this size I first resize them to fit the max width or height (depending on image orientation) and then crop in the center.
I've tried to do it all in 1 method. That didn't work, so I created two seperate methods.
resize_img()
and
crop_img().
When I run resize_img() on 3 different files, it works. If after that I use crop_img() on these thumbnails the 1th method created, it works. If I combine the two, or use them after one another, it doesn't.
I've tried $this->image_lib->clear();, unsetting the config files. I even created two config files, just to be sure.
I'm getting different all kind of errors from GD2, but the problem is, that after resize_img() creates the thumbnail, the crop_img() won't crop it. After that it all goes south, and the next images can't be opened. Write premissions are checked, both on folder and files.
Unable to save the image. Please make sure the image and file
directory are writable. The path to the image is not correct. Your
server does not support the GD function required to process this type
of image.
Full code:
<?PHP
class Imagetest extends MY_Controller {
function __construct()
{
parent::__construct();
$this->load->library('image_lib');
}
function index()
{
$testimg1 = 'uploads/test/1.png';
$testimg2 = 'uploads/test/2.png';
$testimg3 = 'uploads/test/3.png';
$this->resize_img($testimg1);
$this->crop_img($testimg1);
$this->resize_img($testimg2);
$this->crop_img($testimg2);
$this->resize_img($testimg3);
$this->crop_img($testimg3);
}
function image_thumb_name($img = null)
{
if(!empty($img)) {
$exploded = explode('.', $img);
return $exploded['0'] . '_thumb.' . $exploded['1'];
}
}
function get_axis($img)
{
// Default values
$output['x_axis'] = 0;
$output['y_axis'] = 0;
// Settings
$config['height'] = 132;
$config['width'] = 176;
if ($img_dim = getimagesize($img)) {
list($thumb_width, $thumb_height) = $img_dim;
} else {
echo '<h1> ERROR HERE TOO</h1>';
return false;
}
if ($thumb_width > $config['width']) {
$output['x_axis'] = (($thumb_width - $config['width']) / 2) ;
} else if ($thumb_height > $config['height']) {
$output['y_axis'] = (($thumb_height - $config['height']) / 2);
}
return $output;
}
function resize_img($img)
{
$config = array();
echo 'Processing: '. $img .'<br/>'; // Debug
if ($img_dim = getimagesize($img)) {
list($image_width, $image_height) = $img_dim;
} else {
echo '<h1> ERROR HERE </h1>';
}
// create a thumbnail
$config['image_library'] = 'GD2';
$config['source_image'] = $img;
$config['quality'] = 100;
$config['height'] = 132;
$config['width'] = 176;
$config['create_thumb'] = TRUE;
$config['maintain_ratio']= TRUE;
$config['master_dim'] = ($image_width > $image_height) ? 'height' : 'width';
$this->image_lib->initialize($config);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
echo '<img src="../'.$this->image_thumb_name($img).'" /> <br/>'; // Debug
$this->image_lib->clear();
unset($config);
}
function crop_img($img)
{
$config2 = array();
// Crop that thumbnail
$config2['image_library'] = 'GD2';
$config2['quality'] = 100;
$config2['height'] = 132;
$config2['width'] = 176;
$config2['source_image'] = $this->image_thumb_name($img);
$axis = $this->get_axis($config2['source_image']);
$config2['x_axis'] = $axis['x_axis'];
$config2['y_axis'] = $axis['y_axis'];
$config2['maintain_ratio'] = FALSE;
$config2['create_thumb'] = FALSE;
$this->image_lib->initialize($config2);
if (!$this->image_lib->crop()) {
echo $this->image_lib->display_errors();
}
echo '<img src="../'.$config2['source_image'].'" /> <br/>'; // Debug
$this->image_lib->clear();
unset($config2);
}
}
Got it!
I've set the create_thumb option to FALSE, and used the new_image parameter in the resize_img method. The effect is the same, but the built-in create_tumb function is not being used.
It's a bug IMHO, but it's working now :)
$config['create_thumb'] = FALSE;
$config['new_image'] = $this->image_thumb_name($img);
The problem is with your $config['source_image']
var_dump($config['source_image']);
see what you get.
If the file is in images folder in root folder, the $config['source_image] = 'images/'.$img.
Also make sure all the exploding functions returning the correct file name. (return and echo to check).
1st, eliminate file not found error. After that, see what error comes.
You can use Kodem's Image Resize Service. You can resize, crop any image with just a http call. Can be used casually in the browser or used in your production app.

Problems creating image thumbnails with CodeIgniter

I'm trying to use the image manipulation class to create thumbnails of images. Here's my relevant method:
public function create_thumbnail($source_img, $name, $file_ext)
{
$config['image_library'] = 'gd2';
$config['source_image'] = $source_img;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 50;
$config['new_image'] = 'C:\xampp\htdocs\\' . $name . 't' . $file_ext;
$this->load->library('image_lib', $config);
if(!$this->image_lib->resize())
{
echo $this->image_lib->display_errors();exit;
}
return TRUE;
}
When the script is run, I get this error:
Unable to save the image. Please make sure the image and file directory are writable.
The array element $config['new_image'] evaluates to something like this: C:\xampp\htdocs\EuzIDct.jpg, so I know it's not that. The source file also exists at the time the method is called, so it's not that either.
Does anyone have any clue what's going on here? I Google'd the problem but the solution from the CI forums is saying specify an absolute path nor a relative one, which is what I'm doing but alas it doesn't work.
Thanks!
First try removing the extra backslash in your filepath.
C:\xampp\htdocs\
Next, right click and check the file permissions on that directory.
Failing that, run the xampp server as admin as a short term fix to see if it is some other permissions issue.
Probably you are running a permission problem. Just attempt to change your folder permissions.
Although your image_lib->resize() approach isn't incorrect, I hardly recommend you using another way to resize those images. Usually 777 permissions expose your application in terms of security.
I recommend using FTP to move those thumbnails instead of codeigniter image_lib.
If you still insist using write permissions, right-click on C:\xampp\htdocs\ -> proprieties, tab security, edit, add user everyone and give him full-access(windows). For Linux-Mac, just a simple chmod -R 777.
your new image path is going to wrong.
it store image in root directory.
$config['new_image'] = '/folder name/new_image.jpg';
I have had this problem before. In my case, I put the image source and new_image without base_url or REAL_PATH:
public function create_thumbnail($file_name='2012_02_23_15_06_00_1.jpg'){
$config['image_library'] = 'gd2';
$config['source_image'] = 'assets/img/content/article/'.$file_name;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 210;
$config['height'] = 160;
$config['new_image'] = 'assets/img/content/article/thumb/thumb_' . $file_name;
$this->load->library('image_lib', $config);
if(!$this->image_lib->resize())
{
echo $this->image_lib->display_errors();exit;
}
return TRUE;
}
See?
you don't put
$config['new_image'] = base_url().'assets/img/content/article/thumb/thumb_' . $file_name;
but
$config['new_image'] = 'assets/img/content/article/thumb/thumb_' . $file_name;
using base_url(), either your image_source or your new_image will have prefix http://domain_or_localhost.
using REAL_PATH like C://htdocs/blablabla/ just complicates things such as unwritable, etc, etc

Resizing an image on the fly using CodeIgniter

I am working on a script that downloads a file from Dropbox, supposed to resize that image and then shoot it up to an S3 bucket.
For some reason, I can't get the image to resize.
I keep getting the following error:
The path to the image is not correct.
Your server does not support the GD function required to process this type of image.
Code Base:
public function resize_test() {
$postcard_assets = $this->conn->getPostcardDirContent("folder_name", "Photos", TRUE);
foreach($postcard_assets['contents'] as $asset) {
$file = pathinfo($asset['path']);
$original_file = $this->conn->downloadFile($asset['path']);
$raw_file = sha1($file['basename']);
$s3_file_name = "1_{$raw_file}.{$file['extension']}";
$this->resize_photo($original_file);
$this->s3->putObject($s3_file_name, $original_file, 's3-bucket-name', 'public-read');
$s3_check = $this->s3->getObjectInfo($s3_file_name, 's3-bucket-name');
if($s3_check['content-length'] > 0) {
krumo($s3_check);
exit();
}
}
}
private function resize_photo($photo) {
$config['image_library'] = 'imagemagick';
$config['source_image'] = $photo;
$config['maintain_ratio'] = TRUE;
$config['width'] = 640;
$config['height'] = 480;
$this->load->library('image_lib', $config);
if(!$this->image_lib->resize()) {
exit($this->image_lib->display_errors());
}
}
Dropbox API DownloadFile:
public function downloadFile($file) {
$this->setTokens();
return $this->conn->getFile($file);
}
Anyone know what I might be doing wrong?
Dont load image_lib multiple times. Add image_lib in autoload libs and change
$this->load->library('image_lib', $config);
to
$this->image_lib->initialize($config);
I'm doing image resizing with CI using ImageMagick just like you are. You need the following to get this to work:
imagemagick should be installed. You can test it from the command line using the 'convert' command
imagick needs to be installed, this is the PHP library that binds to imagemagick
ImageMagick itself depends on various other libraries such as libjpeg and libpng. Make sure those are installed as well
Simpy do a phpinfo() and scroll down to 'imagick'. Check whether it is there and then check the 'supported file formats' heading to see if the file type you are wanting to resize is there.
If all of the above are correct and it still does not work, you should not forget to include the path to imagemagick in your code:
$config['library_path'] = '/usr/local/bin';
I went through all this pain before so I hope this helps you :)
For Multiple resize below code worked for me.
$config['create_thumb'] = FALSE; //to avoid _thumb prefixing
$config['maintain_ratio'] = TRUE;
$config['width'] = 250;
$config['height'] = 250;
$config['new_image'] = 'thumb_250x250_'.$file_name; // new name
$CI->load->library('image_lib', $config, 'abc'); //abc to avoid instance caching.
$CI->abc->resize();
unset($CI->abc); //unsetting instance.
$config['width'] = 100;
$config['height'] = 100;
$config['new_image'] = 'thumb_100x100_'.$file_name; // new name
$CI->load->library('image_lib', $config, 'xyz'); // xyz to avoid instance caching.
$CI->xyz->resize();
unset($CI->xyz); // unsetting instance.
You need to use $config['new_image'] = '/path/to/new_image.jpg'; in your resize_photo function.
Read http://codeigniter.com/user_guide/libraries/image_lib.html
Actually you are trying to load the image library twice. Since you also initialize the config array on the very same line, the array never gets loaded into the library.
Change your code to this:
//this
$this->load->library('image_lib', $config);
//to this
$this->load->library('image_lib');
$this->image_lib->initialize($config);
and it will work perfectly.
See if you can actually open the original saved image before you try to resize it. I was decoding a base64 uploaded image while using a preg_replace. For some reason, which I still can't track down... it was removing like so
$file = preg_replace('/data.*base64,/', '', chunk_split($this->post('myimg'));
it would return this: [removed]/9....etc. which when base64 decoded... obviously isnt a valid image file.. so the resize wouldnt work. I had to add a
$file = substr($file,9);
to then remove the [removed]. extra work and took me while to figure out, but now I can resize images.
Side Question... Why is preg_replace adding [removed]??? Sigh... php.
//Make controller named "image.php"
class Image extends CI_Controller {
public function index($width, $height, $image_path)
{
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/'.$image_path;
$config['dynamic_output'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = $width;
$config['height'] = $height;
$this->load->library('image_lib', $config);
$this->image_lib->initialize($config);
echo $this->image_lib->resize();
}
}
?>
//Call from view page
<img src="<?php echo ("index.php/image/index/150/150/".$luserdata[0]'profile_image']);?>" alt="resized mage1"/>

Categories