Image_lib in Codeigniter doesn't produce output - php

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.

Related

CodeIgniter file path image resizing error

public function resize($image_data) {
$img = substr($image_data['full_path'], 51);
echo $img;
$config['image_library'] = 'gd2';
$config['source_image'] = $image_data['full_path'];
$config['new_image'] = '' . $img;
$config['width'] = $this->input->post('width');
$config['height'] = $this->input->post('height');
}
These lines of code produce the following file path: codeigniter.mainsitehere.com/manipulation_controller/0.com/uploads/name_of_image6.jpg
(which is not where the file is stored)
The result I would like is:
codeigniter.mainsitehere.com/uploads/name_of_image6.jpg
or better yet
codeigniter.mainsitehere.com/uploads/6
Although this form will probably require a lot more fundemental changes to other parts of the site.
Any suggestions are greatly appreciated. I will also respond with more information/code if needed.
Edit:
This is the source I am using: https://www.formget.com/codeigniter-image-library/
And the question, if I had to put it in one sentence, is how do I manipulate this function and the PHP objects in it to create/display the correct filepath for a picture?

PHP ImageMagick Multiple sizes for images - Cant get it to work

Hi all : ) Thank you in advance for any assistance with this question.
I am using CodeIgniter and have images uploading, rotating and resizing successfully.
It is when I try to create multiple sizes fro an image that I am a little lost.
Here is my code:
// First I make two copies of the origional image - there is no problem here - these always work. I always have the successfully copied images.
$Copy_Mid_Size_File = "Some_Path_For_The_Newly_Copied_Mid_Size_Image"
if(!copy($source_image, $Copy_Mid_Size_File)){
echo "failed to copy $Copy_Mid_Size_File - Please contact the system administrator. \n";
die();
}
$Copy_Thumbnail_Size_File = "Some_Path_For_The_Newly_Copied_Thumbnail_Size_Image"
if(!copy($source_image, $Copy_Thumbnail_Size_File)){
echo "failed to copy $Copy_Thumbnail_Size_File - Please contact the system administrator. \n";
die();
}
// Now I resize for the mid size image
$config['image_library'] = 'imagemagick';
$config['library_path'] = '/usr/bin';
$config['source_image'] = $Copy_Mid_Size_File;
$config['maintain_ratio'] = TRUE;
$config['quality'] = '100%';
$config['master_dim'] = 'auto';
$config['width'] = 200;
$config['height'] = 200;
$this->load->library('image_lib', $config);
if(!$this->image_lib->resize()){
echo $this->image_lib->display_errors();// This has never reported an error yet
echo "Image Re-size for Mid Size Image FAILED!";// This has never reported an error yet
die();
}else{
echo" Mid-Size Re-size Worked!";
}
// Now I try to resize for the Thumbnail
$config['image_library'] = 'imagemagick';
$config['library_path'] = '/usr/bin';
$config['source_image'] = $Copy_Thumbnail_Size_File;
$config['maintain_ratio'] = TRUE;
$config['quality'] = '100%';
$config['master_dim'] = 'auto';
$config['width'] = 50;
$config['height'] = 50;
$this->load->library('image_lib', $config);
if(!$this->image_lib->resize()){
echo $this->image_lib->display_errors();// This has never reported an error yet
echo "Image Re-size for Thumbnail Size Image FAILED!";// This has never reported an error yet
die();
}else{
echo" Thumbnail Re-size Worked!";
}
I always end up with the proper number of images correctly named - The thumbnail image just does not re-size - there is no error reported. It always says that is succeeded.
If I put the thumbnail re-size code first - the thumbnail re-sizes correctly - but then the mid-size image does not.
I realize there are other ways to load the libraries - but I don't see why the first re-size works but the second does not.
Any ideas?
Thanks.
Regards,
Ken
You only need to load the library once, but then you need to initialize it with the new config each time. So your code will look like this:
// load the library
$this->load->library('image_lib');
// first image:
// set some config
$config['image_library'] = 'imagemagick';
...
// initialize the library with this config
$this->image_lib->initialize($config);
// Do the resize:
$this->image_lib->resize();
// clear the config:
$this->image_lib->clear();
// second image:
// set some config
$config['image_library'] = 'imagemagick';
...
// re-initialize the library with the new config
$this->image_lib->initialize($config);
// Do the resize:
$this->image_lib->resize();
I suspect you could even just update $config['width'] and $config['height'] without having to clear, but I've not tested that!
As a side note, you don't really have to copy the image into the correct directories first; the CodeIgniter image library can create a new one for you, if you set your config like this:
$config['new_image'] = '/path/to/new_image.jpg';
This will save the image into the new path without having to create copies all over the place.
Reference: http://codeigniter.com/user_guide/libraries/image_lib.html

image cropping with codeigniter no GD library - GD2 is intalled

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.

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