0

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
    Perhaps start small, try and upload one image independent of any other code using `image_moo` and see if you can get it to work. If not, then you can modify your question here to show your simple test. This way the question is more inline with stack's simple and verifiable ideology for questions. As a side note, I'm not sure why you bother with a individual field like `has_thumb` if your code doesn't (seemingly) have options for not having a thumb. Further it would be important to know where the failure is... does the image upload and just not resize? or not upload at all? – Alex Jan 12 '18 at 08:14
  • I isolated the problem as you said. Thank you. 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." – Robert Lovelett Jan 12 '18 at 19:22
  • Ah so there were already images there of the same name to begin with? – Alex Jan 12 '18 at 21:13

1 Answers1

1

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."