Location: PHPKode > projects > Nukequiz > html/modules/Nukequiz/admin/NQACreateThumbs.php
<?php
/*****************************************************************/
/* Nukequiz (tm)    v 3.1.0                                		 */
/* By: Kissoftware (hide@address.com)      		     */
/* http://www.kissoftware.org.uk                           		 */
/* http://www.kissoftware.co.uk                       		     */
/* Copyright © 2003-2008 by Kissoftware         				 */
/* See Docs For Information & Credits                 			 */
/* For More information Visit Forums Or The Online Help Manual	 */
/*****************************************************************/
class Thumbs
{
var $dir = "";
var $thumbDir = "";
var $tPrefix = "";
var $maxH = 0;
var $maxW = 0;
var $fix = 0;

function Thumbs($dir, $tPrefix = "", $maxW = 250, $maxH = 250, $fix = 0)
{
if(substr($dir, -1) == "/") $dir = substr($dir, 0, -1);
$this->dir = $dir;
$this->tPrefix = $tPrefix;
$this->thumbDir = $dir."/thumbs";
$this->maxH = $maxH;
$this->maxW = $maxW;
$this->fix = $fix;
//if(!file_exists($this->dir)) die("path \"".$this->dir."\" doesn't exist! please set the right path to your picture folder.");
//if(!file_exists($this->thumbDir)) die("path \"".$this->thumbDir."\" doesn't exist! please create the subdir \"/thumbs\" in your picture folder.");
//if(!is_writable($this->thumbDir)) die("path \"".$this->thumbDir."\" has no write rights! please set the rights to 777 via CHMOD first...");
}

function getImageNames()
{
$files = false;
if($resDir = opendir($this->dir))
{
$cpos = 0;
while($file = readdir($resDir))
{
if($file[0] != "_" && $file != "." && $file != ".." && !is_dir($this->dir . "/" . $file))
{
$ext = substr($file, -3);
$ext = strtolower($ext);
if($ext == 'jpg' OR $ext == 'gif' OR $ext == 'png') {
$files[$cpos] = $file;
$cpos++;
}
}
}
closedir($resDir);
}
if($files) sort($files);
return $files;
}

function checkThumb($image)
{
$thumbFile = $this->thumbDir."/".$this->tPrefix.$image;
if(file_exists($thumbFile)) {
list($srcW, $srcH, $srcType, $html_attr) = getimagesize($thumbFile);
if($this->fix == 1) {
if($this->maxW != $srcW) {
return false;
}
} elseif($this->fix == 2) {
if($this->maxH != $srcH) {
return false;
}
} else {
if($srcH > $this->maxH OR ($srcW != $this->maxW && $srcH != $this->maxH)) {
return false;
}
}
//echo "thumb of $image exists<br>";
return true;
} else {
//echo "thumb of $image doesn't exist!!!<br>";
return false;
}
}

function createThumb($image)
{
$srcFile = $this->dir."/".$image;
list($srcW, $srcH, $srcType, $html_attr) = getimagesize($srcFile);
$ext = substr($image, -3);
$ext = strtolower($ext);
if($ext == 'jpg') {
$srcImage = @imagecreatefromjpeg($srcFile);
} elseif($ext == 'gif') {
$srcImage = @imagecreatefromgif($srcFile);
} elseif($ext == 'png') {
$srcImage = @imagecreatefrompng($srcFile);
}
if(!$srcImage) return false;
if($this->fix == 0) {
if($srcW / $this->maxW > $srcH / $this->maxH) {
$factor = $this->maxW / $srcW;
} else {
$factor = $this->maxH / $srcH;
}
} elseif($this->fix == 1) {
$factor = $this->maxW / $srcW;
} elseif($this->fix == 2) {
$factor = $this->maxH / $srcH;
}
$newH = (int) round($srcH * $factor);
$newW = (int) round($srcW * $factor);
$newImage = imagecreatetruecolor($newW, $newH);
imagecopyresampled($newImage,$srcImage,0,0,0,0,$newW,$newH,$srcW,$srcH);
$newFile = $this->thumbDir."/".$this->tPrefix.$image;
imagejpeg($newImage, $newFile, "90");
return true;
}

function getImages()
{
@set_time_limit(300);
$images = "";
$thumbs = "";
$cpos = 0;
$imageList = $this->getImageNames();
foreach($imageList as $image) {
$thumb = false;
$thumb = $this->checkThumb($image);
if(!$thumb) {
$thumb = $this->createThumb($image);
}
if($thumb) {
$images[$cpos] = $this->dir."/".$image;
$thumbs[$cpos] = $this->thumbDir."/".$this->tPrefix.$image;
$cpos++;
}
}
return array($images, $thumbs, $image);
}

function rebuildThumbs()
{
@set_time_limit(300);
$imageList = $this->getImageNames();
foreach($imageList as $image) {
$this->createThumb($image);
}
}
}
?>
Return current item: Nukequiz