1

I'm currently trying to upload images to a page and also have them saved within 2 folders. What's supposed to happen is I upload an image and it is saved in my gallery and thumbs folder.

The image saved in the thumbs folder will then be displayed on the screen and when the image is clicked on, it will be displayed in its proper size, as it was saved in the gallery folder. At the moment the image is saved to the gallery database and to my gallery folder but the thumb image does not display on the screen.

I think is because it's not saved to the thumbs folder. Why is it not saved to the folder?

Controller:

class Gallery extends CI_Controller {

   function __construct() {

      // Call the parent construct
      parent::__construct();
      $this->load->model("profiles");
      $this->load->model("gal_model");
      $this->load->helper(array('form', 'url'));
      $this->gallery_path = 'web-project-jb/assets/gallery/';
      $this->gallery_path_url = base_url().'web-project-jb/assets/gallery/';
   }

   function upload() {
      $config = array(
         'allowed_types' =>'gif|jpg|jpeg|png',
         'upload_path' => $this->gallery_path,
         'max_size' => 10000,
         'max_width' => 1024,
         'max_height' => 768);

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

      $image_data = $this->upload->data();

      $config = array(
         'source_image' => $image_data["full_path"],
         'new_image' => $this->gallery_path. '/thumbs',
         'maintain_ration' => true,
         'width' => 150,
         'height' => 100
      );

      $this->load->library("image_lib", $config);

      $this->image_lib->resize();

      $username = $this->session->userdata('username');

      if ( ! $this->upload->do_upload()) {

         $error = array('error' => $this->upload->display_errors());
         $username = $this->session->userdata('username');
         $viewData['username'] = $username;

         $this->load->view('shared/header');
         $this->load->view('gallery/galtitle', $viewData);
         $this->load->view('shared/nav');
         $this->load->view('gallery/galview', $error, $viewData, array('error' => ' '));
         $this->load->view('shared/footer');

      } else {

         $file_data  = $this->upload->data();
         $image = $this->gallery_path.$file_data['file_name'];
         $data['image'] = $this->gallery_path.$file_data['file_name'];
         $this->username = $this->session->userdata('username');
         $images = $this->session->userdata('images');
         $data['images'] = $images;
         $this->gal_model->putGalleryImage($username, $image);
         $this->session->set_userdata($image);
         $viewData['username'] = $username;
         $data['gal_model'] = $this->gal_model->get_images($username);
         var_dump($image);

         $username = $this->session->userdata('username');

         $this->load->view('shared/header');
         $this->load->view('gallery/galtitle', $viewData);
         $this->load->view('shared/nav');
         $this->load->view('gallery/galview', $data, $viewData);
         $this->load->view('shared/footer');
      }
   }

   function index() {
      $username = $this->session->userdata('username');
      $images = $this->session->userdata('images');
      $this->load->library('upload');
      $data['gal_model'] = $this->gal_model->get_images($username);
      $file_data = $this->upload->data();
      $file_data['file_name'] = $this->gal_model->get_images($username);
      $image = $this->gallery_path.$file_data['file_name'];
      $data['image'] = $file_data['file_name'];
      $data['images'] = $images;
      $viewData['username'] = $username;

      $this->load->view('shared/header');
      $this->load->view('gallery/galtitle', $viewData);
      $this->load->view('shared/nav');
      $this->load->view('gallery/galview', $viewData, $data, array('error' => ' '));
      $this->load->view('shared/footer');
   }
}

Gal model:

class Gal_model extends CI_Model {
  var $gallery_path;
  var $gallery_path_url;
  function Gal_model() {
    parent::__construct();
    $this->gallery_path = 'web-project-jb/assets/gallery/';
    $this->gallery_path_url = base_url().'web-project-jb/assets/gallery/';
  }
  function exists($username) {
    $this->db->select('*')->from("gallery")->where('user', $username);
    $query = $this->db->get();
    if ($query->num_rows() > 0) {
      return true;
    } else {
      return false;
    }
  }
  function putGalleryImage($username, $image) {
    $record = array('user' => $username, 'galleryimage' => $image);
    $this->session->set_userdata($image);
    if ($this->exists($username)) {
      $this->db->where('user', $username)->insert('gallery', $record);
    }
  }
  function get_images($username) {
    $this->db->select('*')->from('gal_model')->where('user', $username);
    $files = scandir($this->gallery_path);
    $files = array_diff($files, array('.', '..', 'thumbs'));
    $images = array();
    foreach ($files as $file) {
      $images[] = array(
        'url' => $this->gallery_path_url.$file,
        'thumb_url' => $this->gallery_path_url.'thumbs/'.$file
      );
    }
    return $images;
  }
}

Gallery view:

<? if (isset($images) && is_array($images)): foreach($images as $image):?>
<a href="<?php echo $image['url']; ?>">
<img src ="<?php echo $image['thumb_url']; ?>"width='150' height='150'/>
<?= endforeach; else:  ?>
<div id = "blank_gallery">Please upload an Image</div>
<? endif; ?>
artless noise
  • 21,212
  • 6
  • 68
  • 105
Frank_g
  • 21
  • 1
  • 1
  • 5
  • *"I think is because its not saving to the thumbs folder. "* surely you can look in the folder and know for sure if this is the case –  Dec 18 '12 at 23:18
  • For the record, Ive checked the thumbs folder and no images have saved there. Have any idea why this could be? – Frank_g Dec 18 '12 at 23:20
  • Do you have proper write access on the folder? – random Dec 18 '12 at 23:28
  • Yes - all files were saving correctly before I added the database functionality to it – Frank_g Dec 18 '12 at 23:31
  • This foreach in my getImages function is supposed to save the images in the thumbs folder also: foreach ($files as $file){ $images[] = array( 'url' => $this->gallery_path_url.$file, 'thumb_url' => $this->gallery_path_url.'thumbs/'.$file ); – Frank_g Dec 18 '12 at 23:55
  • Are you trying to upload the same file to two separate locations? You only need to upload the file once (to the gallery directory), then use the image_lib to resize that file with the `new_image` property to save the output to the thumbs directory. – Jeemusu Dec 19 '12 at 01:40
  • Did you just create a new account? Is this not roughly the same code: http://stackoverflow.com/questions/13924849/why-is-my-gallery-view-page-throwing-up-an-error/13924895#comment19221835_13924895 .... uncanny, huh? – Kai Qing Dec 19 '12 at 01:47
  • @Jeemusu -yes that what is what Im trying to do, but what's stopping it from doing this? – Frank_g Dec 19 '12 at 01:57
  • @Frank_g Well, I would say your logic is wrong. Instead of uploading the same image to two locations, then resizing one of them. Upload the image once, to the gallery folder. Then resize that image to create a new image which is saved in the thumbs folder. The codeigniter documentation for the image_lib is pretty decent, give it a shot. – Jeemusu Dec 19 '12 at 02:05
  • i have put the image lib that i have so far in the else part of my upload statement - $config = array( 'source_image' => $image_data["full_path"], 'new_image' => $this->gallery_path. '/thumbs', 'maintain_ration' => true, 'width' => 150, 'height' => 100 ); $this->load->library("image_lib", $config); $this->image_lib->resize(); still no luck – Frank_g Dec 19 '12 at 10:55
  • One thing that I notice is that you gallery path is `'web-project-jb/assets/gallery/';` and then when specify your thumb path you have `$this->gallery_path. '/thumbs'` so there are two `//` at your path. This could be a reason to work localy but not on-line. – Alex Jan 25 '13 at 16:39

0 Answers0