PHP thumbnail class need !!!

Discussion in 'PHP' started by The_Studio, May 25, 2010.

  1. #1
    hey guys i have a thumbnail class, which out puts all images in a given directory,
    but i want it to exclude any images called thumb.jpg
    does any body know how i could go about doing that ?

    here is the class that i am using
    class Thumbs
    {
    	var $dir = "";
    	var $thumbDir = "";
    	var $tPrefix = "";
    	var $maxH = 0;
    	var $maxW = 0;
    	var $fix = 0;
    
        // initialize the class
    	function Thumbs($dir, $tPrefix = "", $maxW = 0, $maxH = 0, $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))  { mkdir($this->thumbDir); }
    		//removed below and created above
    		//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...");
    	
    	}
    	
    	// find images in given directory
    	function getImageNames()
    	{
    		$files = false;
    		if($resDir = opendir($this->dir))
    		{
    			// check all files in $dir - add images to array 'files'
    			$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;
    	}
    
        // check whether a thumb was allready created
    	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;
    		}
    	}
    
        // create a new thumb to given image
    	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;
    		//$srcW = imagesx($srcImage);
    		//$srcH = imagesy($srcImage);
    		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, "85");
    		return true;
    	}
    
        // collect all images, creates thumbs and return all as an array: return[0] = images_array, return[1] = thumbs_array
    	function getImages()
    	{
    		@set_time_limit(300);
    		$images = "";
    		$thumbs = "";
    		$cpos = 0;
    		$imageList = $this->getImageNames();
    
    		//echo "<div id=\"wait\" style=\"display: \" align=\"center\"><font color=\"#FF0000\">Please wait...</font></div>\n";
    		//flush();
    		//ob_flush();
    		foreach($imageList as $image) {
    			$thumb = false;
    			$thumb = $this->checkThumb($image);
    			if(!$thumb) {
    				$thumb = $this->createThumb($image);
    				//if(!$thumb) echo "$image is not a valid image<br>";
    			}
    			if($thumb) {
    				$images[$cpos] = $this->dir."/".$image;
    				$thumbs[$cpos] = $this->thumbDir."/".$this->tPrefix.$image;
    				$cpos++;
    			}
    		}
    		//echo "<script type=\"text/javascript\">document.all.wait.style.display = 'none';</script>\n";
    
    		return array($images, $thumbs);
    	}
    
        // rebuild ALL thumbs - may be usefull for Admins, if the content of an image has changed
    	function rebuildThumbs()
    	{
    		@set_time_limit(300);
    		$imageList = $this->getImageNames();
    		foreach($imageList as $image) {
    			$this->createThumb($image);
    		}
    	}
    }
    PHP:

     
    Last edited: May 25, 2010
    The_Studio, May 25, 2010 IP
  2. Ludatha

    Ludatha Peon

    Messages:
    35
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Can you show me the class or send it to me.

    The class will loop out the images so you just have to add an if statement, something like this:


    Ill do it for you.
     
    Ludatha, May 25, 2010 IP
  3. The_Studio

    The_Studio Peon

    Messages:
    97
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    class has been added above::

    i want the class to exclude any file in the directory called thumb.jpg thanks
     
    The_Studio, May 25, 2010 IP
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    Replace this

     if($file[0] != "_" && $file != "." && $file != ".." && !is_dir($this->dir . "/" . $file))
    Code (markup):
    with this

     if($file[0] != "_" && $file != "." && $file != ".." && !is_dir($this->dir . "/" . $file) && strtolower($file) != 'thumb.jpg')
    Code (markup):
     
    joebert, May 25, 2010 IP
  5. The_Studio

    The_Studio Peon

    Messages:
    97
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Joebert,
    Thanks alot that work a treat
     
    The_Studio, May 25, 2010 IP