Location: PHPKode > projects > mediaIndex > classes/handler.image.php
<?php
/**
 * $Id: handler.image.php,v 1.22 2004/11/29 22:04:34 openface Exp $      
 *
 *               _ _        _         _                 
 *  _ __  ___ __| (_)__ _  (_)_ _  __| |_____ _____ _ _ 
 * | '  \/ -_) _` | / _` | | | ' \/ _` / -_) \ / -_) '_|
 * |_|_|_\___\__,_|_\__,_| |_|_||_\__,_\___/_\_\___|_|  
 *                                                     
 *     Standalone Indexer Script for Media Files
 *        jason hines, <hide@address.com>
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
 * General Public License for more details.
 */

/* thumbnail directory */
define("_THUMBPATH",_CACHEPATH."/thumbnails");
define("_THUMBURL",_CACHEURL."/thumbnails");

/* cache smaller versions if original is too large */
define("_MAXIMAGESIZE","800x600");

/* location for the cached max images */
define("_MAXIMAGEPATH",_CACHEPATH."/reducedpics");
define("_MAXIMAGEURL",_CACHEURL."/reducedpics");

/**
 * Group handler class for images
 *
 * @version     $Revision: 1.22 $
 * @package     mediaIndexer
 * @subpackage  handlers
 * @author      jason hines, <hide@address.com>
 */

class imageHandler extends defaultHandler {

    public $iconName = "image.gif";

    public function imageHandler() {
        if (!is_executable(_CONVERT)) {
            _addMessage("WARNING! ". _CONVERT . " not found. Is ImageMagick installed?");
        }
    }

    public function printMedia(file $file) {
        $size = getimagesize(_MEDIAPATH."/{$file->path}");
        $parts = explode("x",_MAXIMAGESIZE);
        if ($size[0] < $parts[0] || $size[1] < $parts[1]) {
            $imgurl = _MEDIAURL.$file->path;
        } else {
            // use a smaller image instead of original
            $ext = _getExtension($file->path);
            $tname = $file->hash . "." . $ext;
            if (!file_exists(_MAXIMAGEPATH."/".$tname)) {
                if (!is_writable(_MAXIMAGEPATH)) {
                    mkdir(_MAXIMAGEPATH);
                }

                // smaller image doesn't exist, generate it
                _addMessage("Generating reduced image for  ".basename($file->path)." ...");
                $cmd = _CONVERT ." -geometry "._MAXIMAGESIZE." "._MEDIAPATH.escapeshellarg($file->path)." "._MAXIMAGEPATH."/{$tname}";
                exec($cmd,$out,$ret);
                if ($ret != 0)
                    _addMessage("Unable to generate reduced image for ".basename($file->path).".");
            }
            $imgurl = _MAXIMAGEURL."/".$tname;
        }

        echo "<img src=\"{$imgurl}\" alt=\"{$file->path}\" />\n";
    }

    public function printListing(file $file) {
        $size = getimagesize(_MEDIAPATH."/".$file->path);
        $parts = explode("x",_THUMBSIZE);
        if ($size[0] < $parts[0] || $size[1] < $parts[1]) {
            // original size is smaller than desired thumbnail size, use original
            $imgurl = _MEDIAURL.$file->path;
        } else {
            // use a thumbnail image instead of original
            $ext = _getExtension($file->path);
            $tname = $file->hash . "." . $ext;
            if (!file_exists(_THUMBPATH."/".$tname)) {
                if (!is_writable(_THUMBPATH)) {
                    mkdir(_THUMBPATH);
                }

                // thumnail doesn't exist, generate it
                _addMessage("Generating thumbnail ".basename($file->path)." ...");
                $cmd = _CONVERT ." -geometry "._THUMBSIZE." "._MEDIAPATH.escapeshellarg($file->path)." "._THUMBPATH."/{$tname}";
                exec($cmd,$out,$ret);
                if ($ret != 0)
                    _addMessage("Unable to generate thumbnail ".basename($file->path).".");
            }
            $imgurl = _THUMBURL."/".$tname;
        }
        echo "<div class=\"imageblock\"><a href=\"?file="._encodeuri($file->path)."\">";
        echo "<img src=\"{$imgurl}\" alt=\"{$file->path} Thumbnail\" border=\"0\" /></a><br />";
        echo "<small>" . basename($file->path) . "</small>\n";
        if (!empty($file->desc)) {
            echo "<div class=\"description\">" . _parseText($file->desc) . "</div>\n";
        }
        echo "</div>\n";

    }

}

?>
Return current item: mediaIndex