Location: PHPKode > projects > PhpBlock > A9.8/script/imagemanager.class.php
<?php

class ImageManager {
    private function __construct() {
        $this->listBlockId = array();
        $this->listZoneId = array();
        $this->listBlockRockId = array();
        $this->listZoneRockId = array();
        $this->generateImage = true;
    }
    private static $instance;

    public static $ROCK_IMAGE_LETTER = "r";

    function getInstance() {
        if(!ImageManager::$instance) {
            ImageManager::$instance = new ImageManager();
        }
        return ImageManager::$instance;
    }

    private function getImagePath($parBlockId, $parZoneId, $parUniqueString) {
        return Constante::getPathToImageCache()."/$parBlockId/$parZoneId-$parUniqueString.png";
    }

    function getImage($parBlockId, $parZoneId, &$parUniqueString, $parExtraString="n") {
        Log::debug("ImageManager::getImage($parBlockId, $parZoneId, $parUniqueString, $parExtraString)");
        $locImagePath = $this->getImagePath($parBlockId, $parZoneId, "$parExtraString$parUniqueString");
        Log::debug("ImageManager::getImage|locImagePath=$locImagePath");

        if(!is_file($locImagePath)) {
            return False;
        }
        $locImage = imagecreatefrompng($locImagePath);
        return $locImage;
    }
    function getRockImage($parBlockId, $parZoneId, &$parUniqueString) {
        return $this->getImage($parBlockId, $parZoneId, $parUniqueString, ImageManager::$ROCK_IMAGE_LETTER);
    }
    
    function setZoneImage(&$parImage, $parBlockId, $parZoneId, &$parUniqueString, $parExtraString="n") {
        Log::debug("ImageManager::setZoneImage(image, $parBlockId, $parZoneId, $parUniqueString)");
        $locImagePath = $this->getImagePath($parBlockId, $parZoneId, "$parExtraString$parUniqueString");
        Log::debug("ImageManager::setZoneImage|locImagePath=$locImagePath");
        imagepng($parImage, $locImagePath);
    }

    function setRockZoneImage(&$parImage, $parBlockId, $parZoneId, &$parUniqueString) {
        Log::debug("ImageManager::setRockZoneImage(image, $parBlockId, $parZoneId, $parUniqueString)");
        $this->setZoneImage($parImage, $parBlockId, $parZoneId, $parUniqueString, ImageManager::$ROCK_IMAGE_LETTER);
    }

    function setBlockImage(&$parImage, &$parBlock) {
        $this->deleteBlockImage($parBlock);

        $locImagePath = "0-n".rand()."-".($parBlock->modifierImageCount+1);
        $parBlock->setImagePath($locImagePath);
        $locDirPath=Constante::getPathToImageCache()."/".$parBlock->blockId;
        imagepng($parImage, "$locDirPath/$locImagePath.png");
    }

    function setRockBlockImage(&$parImage, &$parBlock) {
        $this->deleteBlockImage($parBlock, ImageManager::$ROCK_IMAGE_LETTER);
        $locImagePathNum = rand();
        $parBlock->setRockImagePathNum($locImagePathNum);
        $locImagePath = "0-".ImageManager::$ROCK_IMAGE_LETTER."$locImagePathNum-".($parBlock->getRockModifierImageCount()+1);
        Log::debug("ImageManager::setRockBlockImage create ".$parBlock->blockId."/$locImagePath.png");
        $locDirPath=Constante::getPathToImageCache()."/".$parBlock->blockId;
        imagepng($parImage, "$locDirPath/$locImagePath.png");
    }

/*
    function deleteImage($parBlockId, $parZoneId) {
        system("rm ".Constante::getPathToImageCache()."/$parBlockId/$parZoneId-*.png");
    }
*/
    function deleteBlockImage(&$parBlock, $parBlockLetter="n") {
        $locDirPath=Constante::getPathToImageCache()."/".$parBlock->blockId;
        $locImagePathNum = $parBlock->getRockImagePathNum();
        $locImagePath = "0-".ImageManager::$ROCK_IMAGE_LETTER."$locImagePathNum-".$parBlock->getRockModifierImageCount();
        if($locImagePath != "") {
            $locDir = getDirCreate_IFP($locDirPath);
            while (false !== ($file = readdir($locDir))) {
                if(strncmp($file, "0-".$parBlockLetter, 3) == 0) {
                    unlink("$locDirPath/$file");
                }
            }
        }
    }

    function deleteAllBlockImages_CreateIFP($parBlockId) {
        $locDirPath=Constante::getPathToImageCache()."/$parBlockId";
        $locDir = getDirCreate_IFP($locDirPath);
        while (false !== ($file = readdir($locDir))) {
            if(is_file($file)) {
                unlink("$locDirPath/$file");
            }
        }
    }

    function createDirectories($parBlockId, $parZoneId) {
        $this->deleteAllBlockImages_CreateIFP($parBlockId);
        fopen(Constante::getPathToImageCache()."/$parBlockId/index.html", "w+");
    }

    function addImageBlockToGenerate($parBlockId) {
        if(!in_array($parBlockId, $this->listBlockId)) {
            array_push($this->listBlockId, $parBlockId);
        }
        if(count($this->listBlockId) > 1000) {
            $this->generateImageToGenerate();
        }
    }

    function addImageZoneToGenerate($parZoneId) {
        if(!in_array($parZoneId, $this->listZoneId)) {
            array_push($this->listZoneId, $parZoneId);
        }
        if(count($this->listZoneId) > 1000) {
            $this->generateImageToGenerate();
        }
    }

    function addImageBlockRockToGenerate($parBlockId) {
        if(!in_array($parBlockId, $this->listBlockRockId)) {
            array_push($this->listBlockRockId, $parBlockId);
        }
    }

    function addImageZoneRockToGenerate($parZoneId) {
        if(!in_array($parZoneId, $this->listZoneRockId)) {
            array_push($this->listZoneRockId, $parZoneId);
        }
    }
    
    function generateImageToGenerate() {
        if($this->generateImage) {
            uasort($this->listZoneId, "compareZoneIdWithZ");
            uasort($this->listBlockId, "compareZoneIdWithZ");

            global $gloObjectManager;
            foreach($this->listZoneId as $locZoneId) {
                //get lock($locZoneId)
                $locZone = $gloObjectManager->getZone($locZoneId);
                $locZone->createImage(Constante::$ZONE_IMAGE_SIZE);
            }

            if(count($this->listBlockId) > 0) {
                $locListBlockId=array();
                foreach($this->listBlockId as $locBlockId) {
                    $locBlock = $gloObjectManager->getBlock($locBlockId);
                    $locBlock->createImage(Constante::$BLOCK_IMAGE_SIZE);
                    array_push($locListBlockId, $locBlock->blockId);
                }
                $locListBlockIdWhereClause = implode(",", $locListBlockId);
                
                executer("UPDATE block SET modifierImageCount=modifierImageCount+1, timestamp=".time()." WHERE blockId IN ($locListBlockIdWhereClause)");
            }
            foreach($this->listZoneId as $locZoneId) {
                //release lock($locZoneId)
            }

/*
            foreach($this->listZoneRockId as $locZoneId) {
                //get lock($locZoneId)
                $locZone = $gloObjectManager->getZone($locZoneId);
                $locZone->createRockImage(Constante::$ZONE_IMAGE_SIZE);
            }

            if(count($this->listBlockRockId) > 0) {
                $locListBlockId=array();
                foreach($this->listBlockRockId as $locBlockId) {
                    $locBlock = $gloObjectManager->getBlock($locBlockId);
                    $locBlock->createRockImage(Constante::$BLOCK_IMAGE_SIZE);
                    array_push($locListBlockId, $locBlock->blockId);
                }
                $locListBlockIdWhereClause = implode(",", $locListBlockId);

                executer("UPDATE blockInfo SET value=value+1
                    WHERE blockId IN ($locListBlockIdWhereClause)
                AND type1=".DungeonConstante::$BLOCK_INFO_TYPE1_ROCK_IMAGE.
                " AND type2=".DungeonConstante::$BLOCK_INFO_TYPE2_ROCK_IMAGE_MODIFIER_COUNT);
                executer("UPDATE blockInfo SET value=".time().
                    " WHERE blockId IN ($locListBlockIdWhereClause)
                AND type1=".DungeonConstante::$BLOCK_INFO_TYPE1_ROCK_IMAGE.
                " AND type2=".DungeonConstante::$BLOCK_INFO_TYPE2_ROCK_IMAGE_TIMESTAMP);
            }
            foreach($this->listZoneRockId as $locZoneId) {
                //release lock($locZoneId)
            }
*/
        } else {
            if(count($this->listZoneId) > 0) {
                $locListZoneIdWhereClause = implode(",", $this->listZoneId);
                executer("UPDATE zone SET modified=1 WHERE zoneId IN ($locListZoneIdWhereClause)");
            }
            if(count($this->listBlockId) > 0) {
                $locListBlockIdWhereClause = implode(",", $this->listBlockId);
                executer("UPDATE block SET modified=1 WHERE blockId IN ($locListBlockIdWhereClause)");
            }
        }
        unset($this->listZoneId);
        unset($this->listBlockId);
        $this->listBlockId = array();
        $this->listZoneId = array();
    }
}
?>
Return current item: PhpBlock