开发者

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 functio开发者_C百科n 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"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜