Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating a gallery plugin
#1
Hi guys!

I can't get the hang of how to code a plug-in.

This is what I have today.
1. It's a tiny script that scans given folder for JPG files and returns an array.
2. If the thumbnail folder is empty or has fewer thumbnails than the image array, it creates new thumbs.
3. After that it creates a index.php file with a UL list of all the images. (thumbs and links)
4. When the script is inluded at the end, a javascript loads the gallery CSS, JS and so on into the <head>.

On my old CMS system adding <?php nQaG("gallerydir","test"); ?> works fine .

But in Get-Simple, nothing happens even if I tried multiple solutions.
Doesn't work: <?php nQaG("gallerydir","test"); ?>
Doesn't work: (% nQaG( gallerydir , test ) %)

Here's the main script:
Code:
<?php
/*
Plugin Name: nQa Gallery
Description: Simple but nice Gallery function
Version: 1.1
Author: Dialup
Author URI: www.example.com
*/

# get correct id for plugin
$thisfile=basename(__FILE__, ".php");

# register plugin
register_plugin(
    $thisfile,
    'nQa Gallery',    
    '1.1',        
    'Dialup',
    'www.example.com',
    'Simple but nice Gallery function',
    'theme',
    ''  
);

# activate filter
add_filter('content','nQaG()');

function insert_javascript_and_css () {
$lines = file("plugins/nQa_Gallery/meta.php");
foreach ($lines as $line_num => $line)
{echo $line;}
}

function nQaG($gallery_dir,$folder){

$alldir = $gallery_dir."/".$folder."/";
$tndir = $gallery_dir."/".$folder."/tn/";

if (!is_dir($tndir)) {mkdir($tndir);}

//$images = glob("/tmp/*.{jpeg,gif,png}", GLOB_BRACE);

$img_count = count(glob($alldir."*.{jpg,JPG,jpeg,JPEG}",GLOB_BRACE));
$tn_count = count(glob($tndir."*.{jpg,JPG,jpeg,JPEG}",GLOB_BRACE));

$img_dir_list = glob($alldir."*.{jpg,JPG,jpeg,JPEG}",GLOB_BRACE);
sort($img_dir_list);

if ($tn_count < $img_count)
{
    //create thumbnails
    $dir_handle = opendir($alldir);
    foreach($img_dir_list as $imagefile)
    {
        //if (!file_exists($tndir.$imagefile)){
            $onlyfile = str_replace($alldir, "", $imagefile);
            $img = imagecreatefromjpeg($alldir.$onlyfile);
            $width = imagesx($img);
            $height = imagesy($img);
            $tn_width = 200;
            $new_height = ceil($tn_width * ($height / $width));
            $tmp = imagecreatetruecolor($tn_width, $new_height);
            imagecopyresized($tmp,$img,0,0,0,0,$tn_width,$new_height,$width,$height);
            //imagejpeg($tmp,"$tndir".$tnfile,100);print_r ($tmp."<br />");
            imagejpeg($tmp, $tndir.$onlyfile);

        //}
    }
}
    // Create and Open the new php file
        $createIndex = "$alldir/index.php";
        $fh = fopen($createIndex, 'w') or die("can't open file");

    // Get the content from the template
        $stringData = "<div id=\"gallery\">\n<ul>\n";
        fwrite($fh, $stringData);

        foreach($img_dir_list as $imglink){
        $links = str_replace($alldir, "", $imglink);
        fwrite($fh,"<li><a title=\"$gallery_dir\" href=\"$alldir$links\" ><img class=\"shadows\" src=\"$tndir$links\" alt=\"$gallery_dir\" /></a></li>\n");
    }
        $stringData = "</ul>\n</div>";
        fwrite($fh, $stringData);
        fclose($fh);

include ($alldir."index.php");
}
?>
Reply
#2
It doesn't look like you really need a plugin. You could just add this code to your theme functions.php and then call it in the page directly.

-RobA>
Reply
#3
Looks like that won't work either.

I added the script to: functions.php

I wrote <?php nQaG("gallerydir","test"); ?> in the gallery-test.xml manually.
But that does not work.

Changing it to (% <?php nQaG("gallerydir","test"); ?> %) in the gallery-test.xml.
Does not work.

I get no error messages in the php_error.log,
the script does not create a "tn" folder, nor thumbnails and no index.php file.

Any tricks or fixes ?
Reply
#4
you could add the source code zipped as attachment
so that helpful souls can read what you modified ;=)
|--

Das deutschsprachige GetSimple-(Unter-)Forum:   http://get-simple.info/forums/forumdisplay.php?fid=18
Reply
#5
I copied the Innovation theme folder and used the files as a template.


See attached files.
Reply
#6
try downloading the Exec-PHP plugin which allows you to use PHP within your content.

http://get-simple.info/extend/plugin/exec-php/17/

otherwise hardcode

Code:
<?php nQaG("gallerydir","test"); ?>

in your template.php file
My Github Repos: Github
Website: DigiMute
Reply
#7
n00dles that did the trick, thanks.

I can't hardcode it into the template, then it won't be flexible.

The thought is that you just upload a folder full of images into the main gallery folder.
After that you call the function on any page.
You can call the function multiple times on the same page or multiple times on multiple different pages.

Same page, with multiple folders:
<?php nQaG("gallery","January"); ?><br />
<?php nQaG("gallery","February"); ?><br />
<?php nQaG("gallery",">March"); ?><br />

That way you can sort the gallery, add/remove images, add/remove folders and the script automatically updates the page(s).
Reply
#8
OK, got it working perfectly.

Code:
<?php if(!defined('IN_GS')){ die('you cannot load this page directly.'); }
/*
* @File:        functions.php
* @Action:        SG theme
*/

add_action('theme-header','insert_javascript_and_css');

function insert_javascript_and_css () {
$lines = file("theme/Sg/files/meta.php");
foreach ($lines as $line_num => $line)
{echo $line;}
}

function nQaG($gallery_dir,$folder){

$alldir = $gallery_dir."/".$folder."/";
$tndir = $gallery_dir."/".$folder."/tn/";

if (!is_dir($tndir)) {mkdir($tndir);}

//$images = glob("/tmp/*.{jpeg,gif,png}", GLOB_BRACE);

$img_count = count(glob($alldir."*.{jpg,JPG,jpeg,JPEG}",GLOB_BRACE));
$tn_count = count(glob($tndir."*.{jpg,JPG,jpeg,JPEG}",GLOB_BRACE));

$img_dir_list = glob($alldir."*.{jpg,JPG,jpeg,JPEG}",GLOB_BRACE);
sort($img_dir_list);

if ($tn_count < $img_count)
{
    //create thumbnails
    $dir_handle = opendir($alldir);
    foreach($img_dir_list as $imagefile)
    {
        //if (!file_exists($tndir.$imagefile)){
            $onlyfile = str_replace($alldir, "", $imagefile);
            $img = imagecreatefromjpeg($alldir.$onlyfile);
            $width = imagesx($img);
            $height = imagesy($img);
            $tn_width = 200;
            $new_height = ceil($tn_width * ($height / $width));
            $tmp = imagecreatetruecolor($tn_width, $new_height);
            imagecopyresized($tmp,$img,0,0,0,0,$tn_width,$new_height,$width,$height);
            //imagejpeg($tmp,"$tndir".$tnfile,100);print_r ($tmp."<br />");
            imagejpeg($tmp, $tndir.$onlyfile);
        //}
    }
}
    // Create and Open the new php file
        $createIndex = "$alldir"."index.php";
        $fh = fopen($createIndex, 'w') or die("can't open file");

    // Get the content from the template
        $stringData = "<div id=\"gallery\">\n<ul>\n";
        fwrite($fh, $stringData);

        foreach($img_dir_list as $imglink){
        $links = str_replace($alldir, "", $imglink);
        fwrite($fh,"<li><a title=\"$gallery_dir\" href=\"../$alldir$links\" ><img class=\"shadows\" src=\"../$tndir$links\" alt=\"$gallery_dir\" /></a></li>\n");
    }
        $stringData = "</ul>\n</div>";
        fwrite($fh, $stringData);
        fclose($fh);

include ($alldir."index.php");
}
?>

The function "insert_javascript_and_css" does what it says.
It copies the content of the meta.php file and adds it into the <head></head>.
(jquery lightbox and css)

The code validates and the page looks exactly like I wanted it to look Smile

Thanks guys! Smile
Reply
#9
I updated the function, moving the JS/CSS script.
That way it only loads the extra JS/CSS files on the pages that has a gallery.

I also attached all the files to this post.
To get everything working you have to edit the path in these files:
jquery.lightbox-0.5.js
jquery.lightbox-0.5.css
meta.php
ex.: ../theme/YOUR-THEME/files/lightbox-ico-loading.gif
Code:
<?php if(!defined('IN_GS')){ die('you cannot load this page directly.'); }
/*
* @File:        functions.php
* @Action:        SG theme

To call the function,
edit the page and add
<?php nQaG("main gallery folder","subfolder with photos"); ?>
in the source.
*/


function nQaG($gallery_dir,$folder){

        // Loads the JS files and CS files into the head
add_action('theme-header','insert_javascript_and_css');
$lines = file("theme/Sg/files/meta.php");
foreach ($lines as $line_num => $line)
{echo $line;}

        // Shortening the address to the gallery folders
$alldir = $gallery_dir."/".$folder."/";
$tndir = $gallery_dir."/".$folder."/tn/";

        // Creating the thumbnail folder if it doesn't exist
if (!is_dir($tndir)) {mkdir($tndir);}

        // Counting all the images in both folders
$img_count = count(glob($alldir."*.{jpg,JPG,jpeg,JPEG}",GLOB_BRACE));
$tn_count = count(glob($tndir."*.{jpg,JPG,jpeg,JPEG}",GLOB_BRACE));

        // Sorting the imagelist
$img_dir_list = glob($alldir."*.{jpg,JPG,jpeg,JPEG}",GLOB_BRACE);
sort($img_dir_list);

        // If there are more images than thumbnails
if ($tn_count < $img_count)
{
        //create thumbnails
    $dir_handle = opendir($alldir);
    foreach($img_dir_list as $imagefile)
    {
        if (!file_exists($tndir.$imagefile)){
            $onlyfile = str_replace($alldir, "", $imagefile);
            $img = imagecreatefromjpeg($alldir.$onlyfile);
            $width = imagesx($img);
            $height = imagesy($img);
            $tn_width = 200;
            $new_height = ceil($tn_width * ($height / $width));
            $tmp = imagecreatetruecolor($tn_width, $new_height);
            imagecopyresized($tmp,$img,0,0,0,0,$tn_width,$new_height,$width,$height);
            imagejpeg($tmp, $tndir.$onlyfile);
        }
    }
}
    // Create and Open the file that holds the gallery content
        $createIndex = "$alldir"."index.php";
        $fh = fopen($createIndex, 'w') or die("can't open file");

    // Write the content
        $stringData = "<div id=\"gallery\">\n<ul>\n";
        fwrite($fh, $stringData);

        foreach($img_dir_list as $imglink){
        $links = str_replace($alldir, "", $imglink);
        fwrite($fh,"<li><a title=\"$gallery_dir\" href=\"../$alldir$links\" ><img class=\"shadows\" src=\"../$tndir$links\" alt=\"$gallery_dir\" /></a></li>\n");
    }
        $stringData = "</ul>\n</div>";
        fwrite($fh, $stringData);
        fclose($fh);
        
    // Include the gallery content in the page
include ($alldir."index.php");
}

?>
Reply




Users browsing this thread: 1 Guest(s)