codeigniter image error - php

Having a problem with image manipulation in codeigniter - it bombs when I get to $this->image_lib->resize(). I just can't see the error.
Code:
$imagemanip = array();
$imagemanip['image_library'] = 'gd2';
$imagemanip['source_image'] = '/resources/images/butera-fuma-dolce.jpg';
$imagemanip['new_image'] = '/resources/images/thumb_butera-fuma-dolce.jpg';
$imagemanip['create_thumb'] = TRUE;
$imagemanip['maintain_ratio'] = TRUE;
$imagemanip['width'] = 350;
$imagemanip['height'] = 350;
$this->load->library('image_lib', $imagemanip);
if ( ! $this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
As I said, it bombs at $this->image_lib->resize(), showing no further output, and does not generate an error.
gd2 is installed (per phpinfo()). I can view the original image with plain html tags. What am I doing wrong?

The path should be relative to the root of your website, where your index.php is located, ie:
don't do this:
$imagemanip['source_image'] = '/resources/images/butera-fuma-dolce.jpg';
do that:
$imagemanip['source_image'] = 'resources/images/butera-fuma-dolce.jpg';
Alternatively, you can use CodeIgniter's absolute path constant, like so:
$imagemanip['source_image'] = FCPATH.'resources/images/butera-fuma-dolce.jpg';

You do not need to specify the 'new_image' config option when using the 'create_thumb' option.
The library will write the file to a file of the same name with a _thumb appended.
Also make sure the correct permissions are set for writing.

Related

Summernote and absolute path in the img-upload.php file

I'm using summernote as the text editor in a backend. The text and images stored must then be displayed in the pages of the frontend.
The editor and the upload images works, but the problem is to recover the images in the frontend because of the path.
I need to use the absolute path in the img-upload.php file but it doesn't seem to accept it.
img-upload.php
if(empty($_FILES['file']))
{
exit();
}
$errorImgFile = "./img/img_upload_error.jpg";
$temp = explode(".", $_FILES["file"]["name"]);
$newfilename = round(microtime(true)) . '.' . end($temp);
$destinationFilePath = '../../images/img-uploads/'.$newfilename ;
if(!move_uploaded_file($_FILES['file']['tmp_name'], $destinationFilePath)){
echo $errorImgFile;
}
else{
echo $destinationFilePath;
}
with the relative path works:
$destinationFilePath = '../../images/img-uploads/'.$newfilename ;
but in this way not:
$path = 'http://localhost/sites/my-site/';
$destinationFilePath = $path.'images/img-uploads/'.$newfilename ;
I don't see any error.
Thanks
You have probably figured it out by now...
It seems Summernote does not render images from absolute paths, but the closest you can get is to specify the file from the root path, like:
$path = '/sites/my-site/';
$destinationFilePath = $path.'images/img-uploads/'.$newfilename ;
Results in something like: '/sites/my-site/images/img-uploads/filename.jpg'
If you save this to your database, you can render your images anywhere on your site using this rooth path address for the files.
I suggest you to use Ckfinder or Fileman with Ckeditor. I made a repository for that. Go check it out.
https://github.com/senocak/Laravel-CKEDITOR-CKFINDER-usage

Secureimage in CodeIgniter

So I wan't to use secureimage on CI coz' I don't want to use the default CAPTCHA helper since it doesn't have an audio option but I have a problem displaying the audio when using secureimage.
I copied the whole secureimage folder that I downloaded from the site to the library of CI.
Displaying the image is fine and also checking the inputted code. But my problem is displaying the audio file. I have no idea how to do it.
Here is how I displayed the image and for checking (already used the secureimage library):
function captcha_image(){
$img = new Securimage();
return $img->show();
}
public function captcha_check(){
$img = new Securimage();
$input = $this->input->post('imagecode');
$result = $img->check($input);
if($result)
$message = "success";
else
$message = "try again";
}
My view:
<img src="<?php echo site_url('form/captcha_image') ?>" alt='captcha' />
If you are using this library
https://github.com/subdesign/CI-HTI-Securimage
So The Method to get the Audio file is
$img->outputAudioFile();
But you have to configure ( settings ) the audio path directory and make sure its rewritable
$settings['audio_path'] = '....';// If you didn't configure this it will secureimage library path and follow by dir /audio/
$settings['audio_noise_path'] = '...';//save as above audio path
$settings['audio_use_noise'] = true; // true or false;
$settings['degrade_audio'] = true; // true or false;
// Then init the secureimage with the options
$img = new Securimage($settings);
$img->show(); // this will show the image src
$img->outputAudioFile(); // this will output the audio file to the browser

PHP Codeigniter Uploading Class, rename files with specific schema

I am having a spot of trouble with Codeigniter and getting files to rename within the upload process from the Upload Library it has to offer. Now before anyone says it, I am not looking for "encrypted" file names.
My Problem is in uploading images you have a good handful of types you could be dealing with. So how does one change the file name using the file_name config option to a specific schema (which I already have the schema part up and working). But maintain the same file type?
Right now I am attempting
$upload_config['file_name'] = $generated_filename_from_schema
Only problem is $generated_filename_from_schema doesnt have a file extension, and leaving the file extension out of the equation CI seems to ignore it altogether and just takes the file and append_1, _2, _3 as it goes up if the files have the same name, otherwise it just leaves the name intact.
Now I have to pass the $config to CI so it will upload the file, but how can I determin what kind of file I am working with before it trys to upload so I can use my name generation schema.
*edit*
$upload_config['upload_path'] = realpath(APPPATH.'../images/');
$upload_config['allowed_types'] = 'gif|jpg|png';
$upload_config['max_size'] = 0;
$upload_config['max_width'] = 0;
$upload_config['max_height'] = 0;
$upload_config['remove_spaces'] = true;
$upload_config['file_name'] = $this->genfunc->genFileName($uid);
if($this->input->post('uploads'))
{
$this->load->library('upload');
$this->upload->initialize($upload_config);
if (!$this->upload->do_upload())
{
//echo 'error';
echo $config['upload_path'];
$this->data['errors'] = $this->upload->display_errors();
}
else
{
//echo 'uploaded';
$this->data['upload_data'] = $this->upload->data();
}
}
You can use $_FILES array to get original name of file.
Extract extension of original file.Then, append to your new file name.
Try as below
$ext = end(explode(".", $_FILES[$input_file_field_name]['name']));
$upload_config['file_name'] = $this->genfunc->genFileName($uid).'.'.$ext;
Personally, I find CodeIgniter's file uploading class to be somewhat cumbersome. If you want a vanilla PHP solution:
function submit_image(){
$f = $_FILES['image'];
$allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
$detectedType = exif_imagetype($f['tmp_name']);
if(in_array($detectedType, $allowedTypes)){
$pi = pathinfo($f['name']);
$ext = $pi['extension'];
$target = $this->genfunc->genFileName($uid) "." . $ext;
if(move_uploaded_file($f['tmp_name'], $target)){
/*success*/
}
else {/*couldn't save the file (perhaps permission error?*/}
}
else {/*invalid file type*/}
}

CodeIgniter doesn't see images (using file_exists)

I'm learning CodeIgniter. I have a directory img with images (path /img/). I am trying to access it through CI view and check if exists with this code:
$av = '../../../img/content/users/'.$userID.'.jpg';
if(file_exists($av)) {
$avatar = $av;
} else {
$avatar = 'img/content/users/none.jpg';
}
Funny thing is, echoing <img src="'.$av.'"> works. What should I do?
CI always runs on index.php, so paths are always relative from there.
Assuming index.php and /img are at the same level in the root, try this:
$av = 'img/content/users/'.$userID.'.jpg';
if(is_file($av)) { // or better yet, make sure it's really an image
$avatar = $av;
} else {
$avatar = 'img/content/users/none.jpg';
}
Funny thing is, echoing <img src="'.$av.'"> works
It's because the browser is looking in a different place than the server. I'd recommend not using ../../relative/paths but using functions like base_url() and img(). When there are additional segments in the URL, relative paths break.
URLs and file paths are not the same. From the current URL ../../../img/content/users may and likely is something completely different than the file path on the hard disk where the view file is located.
Use following steps
1) Create a custom config file name site_config.php in config file (/application/config/) and paste following code
<?php
$config['base_url'] = "http://".$_SERVER['SERVER_NAME'] . str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
if(!defined('DOCUMENT_ROOT')) define('DOCUMENT_ROOT',str_replace('system/application/config','',substr(__FILE__, 0, strrpos(__FILE__, '/'))));
$config['base_path'] = constant("DOCUMENT_ROOT");
?>
2) Edit autoload.php to autoload site_config.php (/application/config/autoload.php)
$autoload['config'] = array('site_config');
3) Then using following code to view image
$image_path = $this->config->item('base_path').'folder_name/'.$userID.'.jpg';
if(file_exists($image_path)) {
$avatar = $this->config->item('base_url').'folder_name/'.$userID.'.jpg';
} else {
$avatar = $this->config->item('base_url').'default_folder_name/profile.jpg';
}
echo '<img src="'.$avatar.'" />';
I think it will help you
Try this:
$av = './img/content/users/'.$userID.'.jpg';

PHP's file_exists() will not work for me?

For some reason this PHP code below will not work, I can not figure it out.
It is very strange,
file_exists does not seem to see that the image does exist, I have checked to make sure a good file path is being inserted into the file_exists function and it is still acting up
If I change file_exists to !file_exists it will return an images that exist and ones that do not exist
define('SITE_PATH2', 'http://localhost/');
$noimg = SITE_PATH2. 'images/userphoto/noimagesmall.jpg';
$thumb_name = 'http://localhost/images/userphoto/1/2/2/59874a886a0356abc1_thumb9.jpg';
if (file_exists($thumb_name)) {
$img_name = $thumb_name;
}else{
$img_name = $noimg;
}
echo $img_name;
file_exists() needs to use a file path on the hard drive, not a URL. So you should have something more like:
$thumb_name = $_SERVER['DOCUMENT_ROOT'] . 'images/userphoto/1/2/2/59874a886a0356abc1_thumb9.jpg';
if(file_exists($thumb_name)) {
some_code
}
http://us2.php.net/file_exists
docs say:
As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to List of Supported Protocols/Wrappers for a listing of which wrappers support stat() family of functionality.
file_exists does only work on the local file system.
So try this if you’re using localhost:
$thumb_name = 'images/userphoto/1/2/2/59874a886a0356abc1_thumb9.jpg';
if (file_exists($_SERVER['DOCUMENT_ROOT'].$thumb_name)) {
$img_name = SITE_PATH2.$thumb_name;
} else {
$img_name = $noimg;
}
Have you enabled the option which allows you to use external URLs? You can set it in php.ini:
allow_url_fopen = 1
You have to write the file path like "file:///C:/Documents%20and%20Settings/xyz/Desktop/clip_image001.jpg".
http://php.net/manual/en/function.file-exists.php
did you check the comments below?
Just reading parts of it, but there seem to be several issues.
Caching may be a problem.
When opening FTP urls it always returns true (they say in the comments)
...
Try Below one. Its working for me
define('SITE_PATH2', 'http://localhost/');
$noimg = SITE_PATH2. 'images/userphoto/noimagesmall.jpg';
$thumb_name = 'http://localhost/images/userphoto/1/2/2/59874a886a0356abc1_thumb9.jpg';
if ($fileopen = #fopen($thumb_name)) {
$img_name = $thumb_name;
fclose($fileopen);
}else{
$img_name = $noimg;
}
echo $img_name;

Categories