Image_moo & CodeIgniter framework won't generate thumbnails

272 views Asked by At

Attempting to generate thumbnails with Image_moo & CodeIgniter framework. Image_moo does not output any errors, however, thumbnail images are never generated.

dir structure app - controllers/ Admin.php ... - libraries/ Image_moo.php ... - models/ Admin_photos_model.php

Admin.php

public function photo_upload() {

            $rules = [
                [
                    'field' => 'caption',
                    'label' => 'Caption'//,
                    //'rules' => 'required'
                ],[
                    'field' => 'description',
                    'label' => 'Description'//,
                    //'rules' => 'required'
                ],[
                    'field' => 'series',
                    'label' => 'Series',
                    'rules' => 'required'
                ]
            ];

            $this->form_validation->set_rules($rules);

            if ($this->form_validation->run() == FALSE) {
                $this->load->view('admin/photos/upload');
            } else {
                $series = str_replace(' ', '', strtolower($_POST['series']));
                $upload_path = './img/photos/'.$series.'/';                        

                $config = [
                    'upload_path'   => $upload_path, //'./img/photos/'.$series.'/',
                    'allowed_types' => 'gif|jpg|jpeg|png'
                ];

                $this->load->library('upload', $config);

                if (!file_exists($upload_path)) { //check if series dir exists
                    mkdir($upload_path, 0777, true); // create dir if !exist
                    $num = 1; //init
                } else {
                    $num = $this->db->where('series', $series)->count_all_results('photos') + 1;
                };

                if (!$this->upload->do_upload()) {
                    $error = array('error' => $this->upload->display_errors());
                    $this->load->view('admin/photos/upload', $error);
                } else {
                    $file = $this->upload->data();    
                    $caption = $_POST['caption'];
                    $description = $_POST['description'];

                    $data = [
                        'filename'      => $file['file_name'],
                        'series'        => $series,
                        'num'           => $num,
                        'caption'       => $caption,
                        'description'   => $description
                    ];

                    $this->Admin_photos_model->upload($data);

                    $this->load->library('image_moo'); //create thumbnail, upload

                    $file_raw_name = $this->upload->data('raw_name');
                    $file_ext = $this->upload->data('file_ext');
                    $file_width = $this->upload->data('image_width');
                    $file_height = $this->upload->data('image_height');
                    $file_uploaded = $upload_path.$data['filename']; //$field_info->upload_path.'/'.$uploader_response[0]->name; 

                    if ($file_width > 1024 && $file_height > 720) {
                        $this->image_moo->load($file_uploaded)
                        ->resize_crop(1024,720)->save($upload_path.$file_raw_name.'_thumb_xl'.$file_ext)
                        ->resize_crop(800,562)->save($upload_path.$file_raw_name.'_thumb_lg'.$file_ext)
                        ->resize_crop(640,450)->save($upload_path.$file_raw_name.'_thumb_med'.$file_ext)
                        ->resize_crop(450,316)->save($upload_path.$file_raw_name.'_thumb_sm'.$file_ext)
                        ->resize_crop(222,156)->save($upload_path.$file_raw_name.'_thumb_xs'.$file_ext);

                        $data = [
                            'has_thumb_xl'          => 1,
                            'has_thumb_lg'          => 1,
                            'has_thumb_med'         => 1,
                            'has_thumb_sm'          => 1,
                            'has_thumb_xs'          => 1,
                            'thumb_xl_filename'     => $file_raw_name.'_thumb_xl'.$file_ext,
                            'thumb_lg_filename'     => $file_raw_name.'_thumb_lg'.$file_ext,
                            'thumb_med_filename'    => $file_raw_name.'_thumb_med'.$file_ext,
                            'thumb_sm_filename'     => $file_raw_name.'_thumb_sm'.$file_ext,
                            'thumb_xs_filename'     => $file_raw_name.'_thumb_xs'.$file_ext
                        ];
                    };

                    if ($this->image_moo->error) {
                        print $this->image_moo->display_errors();
                    };

                    $this->Admin_photos_model->thumbnails($data);

                    $this->session->set_flashdata('message','file uploaded: '.$file_uploaded.'New image has been added..'.'series dir: '.$series.'last num of series: '.$num.'thumb:'.$file_raw_name.'_thumb_xl'.$file_ext.'errors: '.$this->image_moo->display_errors());
                    redirect('admin/photos');
                };

Admin_photos_model

<?php
    class Admin_photos_model extends CI_Model {
        public function __construct(){
            $this->load->database();
        }
        public function upload($data) {
            try {
                $this->db->insert('photos', $data);
                return true;
           } catch (Exception $e) {
                echo $e->getMessage();
           };
        }
        public function thumbnails($data) {
            try {
                $this->db->insert('photos', $data);
                return true;
            } catch (Exception $e) {
                echo $e->getMessage();
           };
        }
    }

Attempting to generate thumbnails, I'm separating the photos by series. If the series hasn't started, a new dir is created. Ideally, uploading 'waterfall.jpg' with series 'nature' would yield:

app
...
public_html/
    img/
        photos/
            nature/
                waterfall.jpg
                waterfall_thumb_xl.jpg
                waterfall_thumb_lg.jpg
                waterfall_thumb_med.jpg
                waterfall_thumb_sm.jpg
                waterfall_thumb_xs.jpg

Any help would be appreciated.

1

There are 1 answers

0
Robert Lovelett On

Reading the image_moo documentation, the save function needs to have an overwrite=FALSE/TRUE. Doing so seemed to fix it.

"save($x,$overwrite=FALSE) - Saved the manipulated image (if applicable) to file $x - JPG, PNG, GIF supported. If overwrite is not set file write may fail. The file saved will be dependant on processing done to the image, or a simple copy if nothing has been done."