Location: PHPKode > projects > FreeGallery > freegallery-2.0/includes/functions.php
<?php
########################################################################
#
# Project: FreeGallery
# URL: http://www.nabber.org/projects/
# E-mail: hide@address.com
#
# Copyright: (C) 2003-2010, Neil McNab
# License: GNU General Public License Version 2
#   (http://www.gnu.org/copyleft/gpl.html)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Filename: $URL: https://freegallery.svn.sourceforge.net/svnroot/freegallery/releases/freegallery-2.0/includes/functions.php $
# Author(s): Neil McNab
# Last Updated: $Date: 2010-05-21 16:53:57 -0700 (Fri, 21 May 2010) $
#
# Description:
#   This file contains common functions needed in the FreeGallery project.
#
########################################################################

include_once("config.defaults.php");

function parse_params($html) {
    /*** a new dom object ***/
    $dom = new domDocument;

    /*** load the html into the object ***/
    if (!$dom->loadHTML($html)) {
        return FALSE;
    }

    $params = $dom->getElementsByTagName('param');

    /*** loop over the params ***/
    $retval = array();
    foreach ($params as $param)
    {
        $name = $param->attributes->getNamedItem('name')->textContent;
        $retval[$name] = $param->attributes->getNamedItem('value')->textContent;
    }
    return $retval;
}

function magick_resize($file, $width, $height, $thumb='') {
    global $CONVERT;
    $cmd = sprintf("%s -resize %sx%s %s jpeg:", $CONVERT, $width, $height, escapeshellarg($file));
    header ("Content-type: image/jpeg");
    if ($thumb == '') {
        passthru($cmd . "-", $retval);
    } else {
        exec($cmd . $thumb, $output, $retval);
    }
    return $retval;
}

function ffmpeg_resize($file, $width, $height, $thumb='') {
    global $FFMPEG, $VID_THUMB_TIME;

    // try real php library method first
    if (class_exists('ffmpeg_movie') AND function_exists('gd_info')) {
        $mov = new ffmpeg_movie($file);
        $frnum = $mov->getFrameRate() * $VID_THUMB_TIME;
        if ($frnum <= 0) {
            $frnum = 1;
        }
        $frame = $mov->getFrame($frnum);
        if ($frame) {
            gd_obj_resize($frame->toGDImage(), $width, $height, $frame->getWidth(), $frame->getHeight(), $thumb);
            return 0;
        }
    }

    //failback to ffmpeg system call
    $cmd = sprintf("%s -i %s -vcodec png -vframes 1 -an -f rawvideo -ss %s -s %sx%s ", $FFMPEG, escapeshellarg($file), $VID_THUMB_TIME, $width, $height);
    if ($thumb == '') {
        header ("Content-type: image/png");
        passthru($cmd . "-", $retval);
    } else {
        exec($cmd . $file . ".thumb.png", $output, $retval);
        list($org_width, $org_height, $type) = get_info($file . ".thumb.png");
        gd_resize($file . ".thumb.png", IMAGETYPE_PNG, $width, $height, $org_width, $org_height, $thumb);
        unlink($file . ".thumb.png");
    }
    return $retval;
}

function format_album_name($name) {
    $name = str_replace("_", " ", $name);
    return $name;
}

function lsdir($dir) {
    global $TYPES;
    $list = array();
    if ($dh = opendir($dir))
    {
      while (($file = readdir($dh)) !== false)
      {
        # Determine file extension
        $ext = strtolower(strrchr("$dir/$file","."));
        # If an image file
        # Ignore extra gallery files
        if ((in_array($ext, $TYPES) || is_dir("$dir/$file")) && ($file[0] != "." && !stristr($file, ".sized.") && !stristr($file, ".thumb.") && !stristr($file, ".highlight."))) {
            $list[] = $file;
        }
      }
      closedir($dh);
    }
    asort($list);
    return $list;
}

function parse_time($desc) {
    if (preg_match("/(january|february|march|april|may|june|july|august|september|october|november|december) ([0-9]*)[0-9-, ]*([0-9]{4})/i", $desc, $result)) {
        $desc = $result[1] . " " . $result[2] . " " . $result[3];
    }
    $desctime = strtotime($desc);
    if ($desctime == FALSE) {
        $desctime = -1;
    }
    return $desctime;
}

function size($file)
{
  $size = filesize ("$file");
  $unit = 'B';
  $amount = 1024;
  
  if ($size >= $amount)
  {
    $size = round ($size / $amount, 0);
    $unit = 'kB';
  }
  if ($size >= $amount)
  {
    $size = round ($size / $amount, 1);
    $unit = 'MB';
  }
  return $size . " " . $unit;
}


function exif_ignore($tag)
{
  $ignore[] = 'UndefinedTag';
  $ignore[] = 'ImageInfo';
  $ignore[] = 'ModeArray';

  foreach ($ignore as $key => $value)
  {
    if (stristr($tag,$value))
    {
      return 1;
    }
  }
  return 0;
}

function is_type($file, $types) {
  if (in_array(strtolower(strrchr($file,".")), $types)) {
    return true;
  }
  return false;
}

function is_movie($file) {
  global $MOVIES;
  return is_type($file, $MOVIES);
}

function is_audio($file) {
  global $AUDIO;
  return is_type($file, $AUDIO);
}

function is_pdf($file) {
  return is_type($file, array(".pdf"));
}

function get_info($file) {
    global $EXTRACT, $IDENTIFY;
    $org_width = 0;
    $org_height = 0;
    $type = "";
    if (is_movie($file)) {
        if (class_exists('ffmpeg_movie')) {
            $mov = new ffmpeg_movie($file);
            $org_width = $mov->getFrameWidth();
            $org_height = $mov->getFrameHeight();
        }
        if (!empty($EXTRACT) AND $org_width == 0 AND $org_height == 0) {
          exec("$EXTRACT $file", $output);
          foreach ($output as $line) {
            if ((stripos($line, "size") === 0) OR (stripos($line, "format") === 0)) {
                $result = preg_match("/([0-9]+)x([0-9]+)/", $line, $matches);
                if ($result) {
                    $org_width = $matches[1];
                    $org_height = $matches[2];
                }
            }
          }
        }
    } elseif (is_pdf($file)) {
        if (!empty($IDENTIFY)) {
            exec("$IDENTIFY $file" . "[0]", $output);
            foreach ($output as $line) {
                $result = preg_match("/([0-9]+)x([0-9]+)/", $line, $matches);
                if ($result) {
                    $org_width = $matches[1];
                    $org_height = $matches[2];
                }
            }
        }
    } else {
        list($org_width, $org_height, $type, $attr) = getimagesize($file);
    }
    return array($org_width, $org_height, $type);
}

function image_resize($file, $base, $thumb='') {
#get image attributes

list($org_width, $org_height, $type) = get_info($file);

if (($org_width == 0 and $org_height == 0)) {
    header ("Content-type: image/gif");
    if (is_movie($file) AND !class_exists('ffmpeg_frame')) {
        readfile("sys-images/movie.gif");
    } elseif (is_audio($file)) {
        readfile("sys-images/audio.gif");
    } elseif (is_dir($file)) {
        readfile("sys-images/folder.gif");
    } else {
        readfile("sys-images/noimage.gif");
    }
    return;
}

list($height, $width) = base2hw($base, $org_height, $org_width);

##image generation section

if (is_movie($file)) {
    if (ffmpeg_resize($file, $width, $height, $thumb)) {
        header ("Content-type: image/gif");
        readfile("sys-images/movie.gif");
    }
} elseif(is_pdf($file)) {
    if (magick_resize($file . "[0]", $width, $height, $thumb)) {
        header ("Content-type: image/gif");
        readfile("sys-images/noimage.gif");
    }
} else {
    gd_resize($file, $type, $width, $height, $org_width, $org_height, $thumb);
}
}

function gd_resize($file, $type, $width, $height, $org_width, $org_height, $thumb='') {

#begin http image output
header ("Content-type: image/jpeg");

#open orginal image
if ($type == IMAGETYPE_JPEG)
{
  $imagein = imagecreatefromjpeg ($file)
     or die ("Cannot Initialize new GD image stream");
}
elseif ($type == IMAGETYPE_GIF)
{
  $imagein = imagecreatefromgif ($file)
     or die ("Cannot Initialize new GD image stream");
}
elseif ($type == IMAGETYPE_PNG)
{
  $imagein = imagecreatefrompng ($file)
     or die ("Cannot Initialize new GD image stream");
}

return gd_obj_resize($imagein, $width, $height, $org_width, $org_height, $thumb);

}

function gd_obj_resize($imagein, $width, $height, $org_width, $org_height, $thumb){

global $THUMB_QUALITY;

#create a pallet for the resized image
$imageout = @imagecreatetruecolor ($width, $height)
     or die ("Cannot Initialize new GD image stream");
/*
#low quality, but faster - only option if gd is < 2.0
$imageout = @imagecreate ($width, $height)
     or die ("Cannot Initialize new GD image stream");
*/

#do the resizing
/*
imagecopyresized ($imageout, $imagein, 0, 0, 0, 0, $width, $height, $org_width, $org_height);
*/
#alternative method, slower
imagecopyresampled ($imageout, $imagein, 0, 0, 0, 0, $width, $height, $org_width, $org_height);

#finish writing the image
imagejpeg ($imageout, $thumb, $THUMB_QUALITY);
imagedestroy ($imageout);
}

function base2hw($base, $org_height, $org_width) {

#determine resize based on height or width
if ($org_width < $org_height)
{
  $height = $base;
  $width = round($base * $org_width/$org_height, 0);
}
else
{
  $width = $base;
  if ($org_width == 0) {
      $height = round($width * 3/4, 0);
  } else {
      $height = round($base * $org_height/$org_width, 0);
  }
}

return array($height, $width);
}

?>
Return current item: FreeGallery