Welcome Guest, Not a member yet? Register   Sign In
Simple Image gallery Library
#11

[eluser]Jelmer[/eluser]
An improved version, posted as code since the forums didn't accept a php file, gave a MIME (?!) warning about txt files and said it contained wrong content for a word document when I tried that...

Look at the first post for instructions...

UPDATE 10 oktober 2008: fixed bug that ignored uppercase file-extentions.

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/****************************************
|
| class            Image Gallery
| type            Library
| author        Jelmer Schreuder
| version        0.1
| based on         http://www.phptoys.com/e107_plugins/content/content.php?content.54
|
*****************************************/

class Image_gallery
{
    var $thmb_width = 120;
    var $thmb_height = 80;
    var $rootdir = './images';
    
    function Image_gallery()
    {
        $this->ci =& get_instance();

        $this->ci->load->library('image_lib');
        $this->ci->load->helper('directory');
    }
    
    function generateThumbnails($dir) {
        $dir = ereg_replace('[/]+\.\.', '', $dir);
        $dir = ereg_replace('./images', '', $dir);
        $dir = $this->rootdir . $dir;
        
        // Open the actual directory
        if ($handle = directory_map($dir, TRUE)) {
            // Read all file from the actual directory
            foreach ($handle as $key => $file) {
                // Check whether tha actual item is a valid file
                if (is_file($dir.'/'.$file)) {
                    // Check whether the actual image is a thumbnail
                    if (eregi('_th\.(jp(e){0,1}g$|gif$|png$)', $file)){
                        $isThumb = true;
                    } else {
                        $isThumb = false;
                    }
                    
                    if (!$isThumb) {
                        // Check if the actual file is a jpeg, gif or png image
                        if (eregi("\.(jp(e){0,1}g$|gif$|png$)", $file)) {
                            // If a thumbnail doesn't exist => create a new one
                            $thmbFile = $dir.eregi_replace("\.(jp(e){0,1}g$|gif$|png$)", "_th.\\1", $file);
                            if (!file_exists($thmbFile)){
                                $config['source_image'] = $dir.$file;
                                $config['create_thumb'] = true;
                                $config['thumb_marker'] = '_th';
                                $config['maintain_ratio'] = true;
                                $config['width'] = $this->thmb_width;
                                $config['height'] = $this->thmb_height;
                                
                                $this->ci->image_lib->initialize($config);
                                if ( ! $this->ci->image_lib->resize())
                                {
                                    echo $this->ci->image_lib->display_errors();
                                }
                                $this->ci->image_lib->clear();
                            }
                        }
                    }
                }
            }
        }
    }
    
    function getNormalImage($file) {
        if (eregi("_th\.(jp(e){0,1}g$|gif$|png$)", $file))
            return base64_encode(eregi_replace('^\./', '', eregi_replace("_th\.(jp(e){0,1}g$|gif$|png$)", ".\\1", $file)));
        else return "";
    }
    
    function displayImages($dir, $static = true) {
        $dir = ereg_replace('[/]+\.\.', '', $dir);
        $dir = ereg_replace('./images/', '', $dir);
        $dir = $this->rootdir . $dir;
        
        if (!$static) $this->generateThumbnails($dir);
        if ($handle = directory_map($dir, TRUE)) {
            natcasesort($handle);
            // Read all file from the actual directory
            foreach ($handle as $key => $file) {
                // Check whether tha actual item is a valid file
                if (is_file($dir.$file)) {
                    // Check whether the actual image is a thumbnail
                    if (eregi("_th\.(jp(e){0,1}g$|gif$|png$)", $file)) {
                        $file_non_th = eregi_replace("_th\.(jp(e){0,1}g$|gif$|png$)", ".\\1", $file);
                        $output['images'][] = array('path' => $dir, 'thumb' => $file, 'image' => $file_non_th);
                    }
                } elseif (is_dir($dir.$file)) {
                    $output['dirs'][] = array('path' => $dir, 'dir' => $file);
                }
            }
            if ($output['images'] == '') $output['images'] = '<em>(no images)</em>';
            return $output;
        } elseif (!file_exists($dir.$file)) return array('images'=>'<em>(directory doesn\'t exist)</em>');
          else return array('images'=>'<em>(no images in directory)</em>');
    }
    
    function getImage($encoded_url) {
        return base64_decode($encoded_url);
    }
}

?&gt;
#12

[eluser]Tony Nash[/eluser]
@Jelmer very cool.. Thanks, Do you have any idea of having image categories and image captions? A category can be a folder and gallery page will show a folder icon for categories.

for your ref.

http://www.sitepoint.com/print/php-galle...m-minutes/
http://www.php-mysql-tutorial.com/image-gallery/
#13

[eluser]Jelmer[/eluser]
@tony Nash

I've only just started work on the front-end, I wrote (and will use) it mostly for my admin to manage images. I still haven't decided whether I want it as easy as possible (because most of my users are inexperienced internet users) or with more options.

The easy option: use only the filesystem, directory based with the foldername as the album name and the filename as the image caption (also simply taking the first picture in each dir as the album icon).

More advanced option: as you suggested I think, on a quick glance over the urls you provided. Using a database to append the filesystem, but still with the filesystem as the basis. I kinda like the filesystem approach. Using the path and filename in an images table it would be easy to add whatever information you want to every image. And an albums table for every path to add information to the albums.

I'm still undecided, but I'll probably decide for the first option. Ease of setup and only options you really need is the best for my system as my users really don't know much about the internet in general. Were I writing it for myself I'd probably use the second option to add categories and labels.

Whatever I decide I'll add the important code to this topic.
#14

[eluser]Jelmer[/eluser]
Not sure if anybody is using this but there was a bug that ignored images with uppercase or mixedcase file-extentions. It's fixed in the post with the library.
#15

[eluser]ghprod[/eluser]
Thanks a lot for share your code Jelmer Big Grin

maybe you can create more complex with feature upload ang integrate with database Smile

regards
#16

[eluser]Jelmer[/eluser]
@ghprod

I ended up not extending the library a lot beyond what it currently offers. I use the filenames for the image title and the folders in which they are placed as the albums.

Maybe I'll come back to this sometime in the future but don't wait for me, I have no intention of writing a full image gallery implementation anytime soon.
#17

[eluser]emcgfx[/eluser]
Can some one show or create gallery based on EasyGallery for CodeIgniter 2.0 please ? Thank you.




Theme © iAndrew 2016 - Forum software by © MyBB