Welcome Guest, Not a member yet? Register   Sign In
[Solved] Problem with image resize codeigniter
#1

(This post was last modified: 01-20-2016, 12:53 AM by wolfgang1983.)

On my project I am creating a bootstrap modal where I can view all images in side a selected folder.

The problem I am having is I have the correct folder permissions on my uploads / cache folder but still trows error below.

Any suggestions why this is happening and how to solve this.

Code:
A PHP Error was encountered

Severity: Warning

Message: getimagesize(C:/wamp/www/riwakawebsitedesigns/uploads/cache): failed to open stream: Permission denied

Filename: libraries/Image_lib.php

Line Number: 1643

Backtrace:

File: C:\wamp\www\riwakawebsitedesigns\application\modules\admin\controllers\common\Upload.php
Line: 58
Function: initialize

File: C:\wamp\www\riwakawebsitedesigns\application\modules\admin\controllers\common\Upload.php
Line: 35
Function: resize_image

File: C:\wamp\www\riwakawebsitedesigns\index.php
Line: 295
Function: require_once
Resize Function

PHP Code:
public function resize_image($file_name$width$height) {
        $this->load->library('image_lib');

        if (!file_exists(FCPATH 'uploads/cache/' $file_name)) {

            $config['image_library'] = 'gd2';
            $config['source_image'] = './uploads/' $file_name;
            $config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = FALSE;
            $config['width'] = $width;
            $config['height'] = $height;
                
            $config
['new_image'] = './uploads/cache/' $file_name;

               $this->image_lib->initialize($config);

               $this->image_lib->resize();

        }

        return base_url('uploads/cache/' $file_name);
        
    

Controller

PHP Code:
<?php

class Upload extends MX_Controller {

    public function 
index() {

        if (
$this->input->get('directory')) {
            
$directory FCPATH 'uploads/' $this->input->get('directory');
        } else {
            
$directory FCPATH 'uploads/';
        }

        
$directories glob($directory '*'GLOB_ONLYDIR);

        
$data['images'] = array();

        if (!
$directories) {
            
$directories = array();
        }

        
// Get files
        
$files glob($directory '*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}'GLOB_BRACE);

        if (!
$files) {
            
$files = array();
        }

        
// Merge directories and files
        
$images array_merge($directories$files);

        foreach (
$images as $image) {
            
$file_name basename($image);
            
$data['images'][] = array(
                
'name' => $file_name,
                
'href' => $this->resize_image($file_name100100)
            );

        }

        
$this->load->view('template/common/upload'$data);
    }

public function 
resize_image($file_name$width$height) {
        $this->load->library('image_lib');

        if (!file_exists(FCPATH 'uploads/cache/' $file_name)) {

            $config['image_library'] = 'gd2';
            $config['source_image'] = './uploads/' $file_name;
            $config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = FALSE;
            $config['width'] = $width;
            $config['height'] = $height;
                
            $config
['new_image'] = './uploads/cache/' $file_name;

               $this->image_lib->initialize($config);

               $this->image_lib->resize();

        }

        return base_url('uploads/cache/' $file_name);
        
    
}



Attached Files
.php   Upload.php (Size: 1.49 KB / Downloads: 105)
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

Why do you merge directories and files into images array ?

I don't get the logic in that..

Code:
      // Merge directories and files
       $images = array_merge($directories, $files);
       foreach ($images as $image) {
           $file_name = basename($image);
Best VPS Hosting : Digital Ocean
Reply
#3

Code:
$directory = FCPATH . 'uploads/' . $this->input->get('directory');

Insecure.

../application
Reply
#4

You need to verify your $file_name variable a bit more. I think your error is being invoked by an empty filename.

PHP Code:
if (!empty($file_name) &&  !file_exists(FCPATH 'uploads/cache/' $file_name)) { 

And as @ivantcholakov says, properly validate your input, make shure it does not contain a '/'
Reply
#5

Thank you all for great advice
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#6

(01-19-2016, 05:30 AM)sv3tli0 Wrote: Why do you merge directories and files into images array ?

I don't get the logic in that..

Code:
      // Merge directories and files
       $images = array_merge($directories, $files);
       foreach ($images as $image) {
           $file_name = basename($image);

That is so I can click on a directory icon as well but now it is all working.
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB