HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/PvdBoogaard/indoorski.nl/backup/oude-site/cms/api/class.imagedir.php
<?php

/**
 * This file contains the iwp_admin_imagemanager class
 *
 * @version $Id$
 * 
 *
 * @package IWP
 * @subpackage IWP_Admin
 */

/**
 * Image Manager
 * This class is used to manage all the images within the /images/ directory of this website
 *
 * @package IWP
 * @subpackage IWP_Admin
 */

class iwp_imagedir {

	protected $imageDirectory = 'images';
	protected $dirObject = null;
	public $start = null;
	public $finish = null;
	public $sortField = 'name';
	public $sortDirection = 'asc';
	protected $dirItems = array();

	/**
	 * Instance
	 * This static variable holds the current instance of this object being loaded.
	 * So using the getInstance function anywhere will return the very same instance.
	 *
	 * @var object Instance
	 */
	public static $Instance;

	/**
	 * getInstance
	 * This is a static function that sets up the class instance and stores it to the static variable. It will then return that instantiation in the future.
	 *
	 * @return object Returns the instantiated object
	 **/
	public static function getInstance(){
		if(!isset(self::$Instance)){
			self::$Instance = new self();
		}
		return self::$Instance;
	}

	public function __construct($sortDirection='asc', $sortField='name'){
		if($sortDirection == 'desc'){
			$this->sortDirection = 'desc';
		}

		if($sortField == 'size' || $sortField == 'modified'){
			$this->sortField = $sortField;
		}

		$this->dirObject = new DirectoryIterator($this->GetImagePath());
		foreach($this->dirObject as $dirItem){
			if($this->IsImageFile($dirItem)){
				list($width, $height) = @getimagesize($dirItem->getPathname());
				$width = max((int)$width, 10);
				$height = max((int)$height, 10);
				$origWidth = $width;
				$origHeight = $height;

				if($width > 200){
					$height = (200/$width) * $height;
					$width = 200;
				}

				if($height > 150){
					$width = (150/$height) * $width;
					$height = 150;
				}

				$this->dirItems[] = array(
										'id'=>substr(md5((string)$dirItem->getFilename()), 0 , 10),
										'url' => $this->GetImageDir() . '/' . (string)$dirItem->getFilename(),
										'url_absolute' => $this->GetImageDir(false) .'/'. (string)$dirItem->getFilename(),
										'name'=>(string)$dirItem->getFilename(),
									/*	'displayname'=>$this->GetDisplayName((string)$dirItem->getFilename()),*/
										'size'=>(string)filesize($dirItem->getPathname()),
										'modified'=>(string)filemtime($dirItem->getPathname()),
										'height'=>(string)$height,
										'origheight'=>(string)$origHeight,
										'width'=>(string)$width,
										'origwidth'=>(string)$origWidth,
									);
			}
		}
		if($sortField == 'size' || $sortField == 'modified'){
			usort($this->dirItems, array($this, 'iwp_imgcmpint'));
		}else{
			usort($this->dirItems, array($this, 'iwp_imgcmpstr'));
		}
	}

	public function GetDisplayName($name){
		if(strlen($name) < 25){
			return $name;
		}

		$first = substr($name, 0, 12);
		$last = substr($name, -12);
		return $first. '...'.$last;
	}

	public function CountDirItems(){
		return count($this->dirItems);
	}

	public function GetImageDirFiles(){
		if(is_null($this->start) ||is_null($this->finish)){
			return $this->dirItems;
		}

		$returnItems = array();

		for($i=$this->start;$i<$this->finish;++$i){
			if(isset( $this->dirItems[$i])){
				$returnItems[] = $this->dirItems[$i];
			}
		}

		return $returnItems;
	}

	public function GetImagePath(){
		return IWP_BASE_PATH . '/'  . $this->imageDirectory;
	}

	public function GetImageDir($includeSiteUrl = true){
		if ($includeSiteUrl) {
			return iwp_config::Get('siteURL') . '/'   . $this->imageDirectory;
		} else {
			return iwp_urls::getInstance()->ForceSlash(iwp_config::Get('appPath'), true, true) . $this->imageDirectory;
		}
	}

	public function IsImageFile($fileName){
		if($fileName->isDir() || $fileName->isDot()){
			return false;
		}

		$validImages = array('png' , 'jpg', 'gif', 'jpeg' ,'tiff', 'bmp');
		foreach($validImages as $image){
			if(strtolower(substr($fileName, neg(strlen($image))-1)) == '.' . $image){
				return true;
			}
		}
		return false;
	}

	public function iwp_imgcmpstr($a, $b)
	{
		$return = strnatcmp(iwp_strtolower($a[$this->sortField]), iwp_strtolower($b[$this->sortField]));
		if($return === -1){
			$return = false;
		}else{
			$return = true;
		}
		if($this->sortDirection == 'desc'){
			return !$return;
		}
		return $return;
	}

	public function iwp_imgcmpint($a, $b)
	{
		$return = false;
		if($a[$this->sortField] >= $b[$this->sortField]){
			$return = true;
		}

		if($this->sortDirection == 'desc'){
			return !$return;
		}
		return $return;
	}

}