Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //MarkoZz 0.5.1
- class galerija_dao
- {
- const defPicTypes = 'jpg,jpeg,gif,png';
- const defArchiveTypes = 'zip,rar';
- public $makethumbs = false;
- public $forceDelete = false;
- private $defAlbum = false;
- private $defThumb = false;
- private $height = 80;
- private $width = 80;
- private $scale = 1;
- private $data;
- protected function getFiles($path = 'pictures')
- {
- $dir = opendir($path);
- $i = 0;
- $data = array();
- while (false !== ($actualfile = readdir($dir))) {
- $i++;
- if ($i > 2) //dir structure
- {
- $parts = explode('.', $actualfile);
- $file = $path.'/'.$actualfile;
- //create more attributes to pass >> $temp['size'] = filesize($file); <<
- $temp = array( 'ctime' => filectime($file),
- 'size' => filesize($file),
- 'input' => $path,
- 'path' => $file,
- 'ext' => $parts[sizeof($parts)-1]
- );
- unset($parts[sizeof($parts)-1]);
- foreach ($parts as $part)
- $s .= $part;
- $temp['name'] = $s;
- if ($this->makethumbs)
- if ($thumb = $this->makeThumb($temp))
- $temp['thumb'] = $thumb;
- $data[] = $temp;
- }
- $parts = '';
- $temp = '';
- $s = '';
- }
- //exit(var_dump($data));
- $this->makethumbs = false;
- closedir($dir);
- return $data;
- }
- protected function getFile($path)
- {
- if(!file_exists($path))
- exit('File not found: "'.$path.'"');
- $data = array();
- $i = 0;
- $parts = explode('.', $path);
- $tmp = explode('/', $path);
- $name = $tmp[sizeof($tmp)-1];
- unset($tmp[sizeof($tmp)-1]);
- foreach($tmp as $str)
- {
- if($i > 0)
- $input .= '/'.$str;
- else
- $input .= $str;
- $i++;
- }
- $file = $path;
- //create more attributes to pass >> $temp['size'] = filesize($file); <<
- $temp = array( 'ctime' => filectime($file),
- 'size' => filesize($file),
- 'input' => $input,
- 'path' => $file,
- 'ext' => $parts[sizeof($parts)-1]
- );
- $tmpName = explode('.', $name);
- unset($tmpName[sizeof($tmpName)-1]);
- foreach($tmpName as $out)
- $s .= $out;
- $temp['name'] = $s;
- if($this->makethumbs)
- if($thumb = $this->makeThumb($temp))
- $temp['thumb'] = $thumb;
- $data[] = $temp;
- $this->makethumbs = false;
- return $data;
- }
- private function checkForUpdate($data)
- {
- $valid = false;
- list($width, $height) = getimagesize($data['savepath']);
- if($height != $this->height && $width != $this->width)
- $valid = true;
- if(($data['ctime'] - time()) > 54000)
- $valid = true;
- if($this->forceDelete)
- $valid = true;
- return $valid;
- }
- private function makeThumb($data)
- {
- if (!is_dir($data['input'].'/thumbs'))
- mkdir($data['input'].'/thumbs');
- $data['savepath'] = $data['input'].'/thumbs/'.$data['name'].'.'.$data['ext'];
- if (file_exists($data['savepath'])) {
- if ($this->checkForUpdate($data)) {
- $this->removeThumb($data);
- $this->writeLog($data, false, true);
- } else {
- $this->writeLog($data, false);
- return $data['savepath'];
- }
- }
- $param['path'] = $data['path'];
- if ($data['ext'] == 'jpg' || $data['ext'] == 'jpeg') {
- if ($image = imagecreatefromjpeg($data['path'])) {
- $param['image'] = $image;
- imagejpeg($this->resizePic($param),$data['savepath']);
- $this->writeLog($data, true);
- return $data['savepath'];
- } else {
- return false;
- }
- } elseif( $data['ext'] == 'gif') {
- if($image = imagecreatefromjgif($data['path'])) {
- $param['image'] = $image;
- imagegif($this->resizePic($param),$data['savepath']);
- $this->writeLog($data, true);
- return $data['savepath'];
- } else{
- return false;
- }
- } elseif ($data['ext'] == 'png') {
- if ($image = imagecreatefrompng($data['path'])) {
- $param['image'] = $image;
- imagepng($this->resizePic($param), $data['savepath']);
- $this->writeLog($data, true);
- return $data['savepath'];
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- private function writeLog($data, $success, $update=false, $move=false)
- {
- if(!is_dir($data['input'].'/log'))
- mkdir($data['input'].'/log');
- if(!file_exists($data['input'].'/log/gallery.txt'))
- fopen($data['input'].'/log/gallery.txt', 'w');
- $string = ($success ? "made new|*|\t" : "file found, skipped|*|");
- if($update)
- $string = "file removed|*|\t";
- if($move)
- $string = "file move[from|to]|*|";
- file_put_contents($data['input'].'/log/gallery.txt', date('h:i:s')."->".time()."|*|".$string."\t".$data['savepath']."\r\n", FILE_APPEND);
- }
- public function setThumbSize($height, $width)
- {
- $this->height = $height;
- $this->width = $width;
- }
- public function setThumbScale($scale)
- {
- $this->scale = $scale;
- }
- public function setHomeDirs($album,$thumbs)
- {
- $this->defAlbum = $album;
- if(!is_dir($album))
- mkdir($album);
- $this->defThumb = $thumbs;
- if(!is_dir($thumbs))
- mkdir($thumbs);
- }
- public function cleanUp()
- {
- if(($this->defAlbum && $this->defThumb))
- {
- //echo var_dump($this->data);
- if(is_array($this->data[0]))
- {
- $valid = rmdir($this->data[0]['input'].'/thumbs');
- foreach($this->data as $data)
- {
- unlink($data['thumb']);
- unlink($data['savepath']);
- $this->writeLog($data, false, true);
- }
- //$valid = $valid & rmdir($this->defAlbum);
- $valid = $valid & rmdir($this->defThumb);
- if(!$valid)
- exit("Couldn't delete everything.");
- else
- exit("Should be clean.");
- }
- else
- {
- $valid = rmdir($this->data['input'].'/thumbs');
- unlink($this->data['thumb']);
- unlink($this->data['savepath']);
- $this->writeLog($data, false, true);
- //$valid = $valid & rmdir($this->defAlbum);
- $valid = $valid & rmdir($this->defThumb);
- if(!$valid)
- exit("Couldn't delete everything.");
- else
- exit("Should be clean.");
- }
- }
- }
- private function repairLinks($data) //can't use same dir, file names don't change
- {
- if(is_array($data[0]))
- {
- foreach($data as $pic)
- {
- $newData = $pic;
- $new['path'] = $this->defAlbum.'/'.$pic['name'].'.'.$pic['ext'];
- $new['thumb'] = $this->defThumb.'/'.$pic['name'].'.'.$pic['ext'];
- $pic['savepath'] = $pic['path'];
- $this->writeLog($pic, true, false, true);
- if(rename($pic['path'], $new['path']) && rename($pic['thumb'], $new['thumb']))
- {
- $newData['path'] = $new['path'];
- $newData['thumb'] = $new['thumb'];
- $newData['savepath'] = $new['path'];
- $this->writeLog($newData, true, false, true);
- }
- else
- exit('Moving files to home directory failed! Check permissions.');
- $out[] = $newData;
- }
- $data = $out;
- }
- else
- {
- $new['path'] = $this->defAlbum.'/'.$data['name'].'.'.$data['ext'];
- $new['thumb'] = $this->defThumb.'/'.$data['name'].'.'.$data['ext'];
- $data['savepath'] = $data['path'];
- $this->writeLog($data, true, false, true);
- if(rename($data['path'], $new['path']) && rename($data['thumb'], $new['thumb']))
- {
- $data['path'] = $new['path'];
- $data['thumb'] = $new['thumb'];
- $data['savepath'] = $new['path'];
- $this->writeLog($data, true, false, true);
- }
- else
- exit('Moving files to home directory failed! Check permissions.');
- }
- return $data;
- }
- private function removeThumb($data)
- {
- if(is_dir($data['input'].'/thumbs'))
- {
- if(file_exists($data['input'].'/thumbs/'.$data['name'].'.'.$data['ext']))
- unlink($data['input'].'/thumbs/'.$data['name'].'.'.$data['ext']);
- else
- return false;
- }
- else
- exit('No thumbs directory found!');
- }
- private function resizePic($param)
- {
- list($width, $height) = getimagesize($param['path']);
- $thumb = imagecreatetruecolor($this->width*$this->scale, $this->height*$this->scale);
- imagecopyresized($thumb, $param['image'], 0, 0, 0, 0, $this->width*$this->scale, $this->height*$this->scale, $width, $height);
- return $thumb;
- }
- private function filterFiles($path, $ext)
- {
- $input = $this->getFiles($path);
- $internalExt = explode(',', $ext);
- $data = array();
- foreach($input as $part)
- {
- foreach($internalExt as $intExt)
- {
- if($part['ext'] == $intExt)
- $data[] = $part;
- }
- }
- if($this->defAlbum && $this->defThumb)
- $data = $this->repairLinks($data);
- $this->data = $data;
- return $data;
- }
- private function filterFile($path, $ext)
- {
- $input = $this->getFile($path);
- $internalExt = explode(',', $ext);
- $data = false;
- foreach($internalExt as $intExt)
- {
- if($input[0]['ext'] == $intExt)
- $data = $input[0];
- }
- if(!$data)
- exit('File type not supported!');
- if($this->defAlbum && $this->defThumb)
- $data = $this->repairLinks($data);
- $this->data = $data;
- return $data;
- }
- public function getPictures($path, $ext = self::defPicTypes)
- {
- return $this->filterFiles($path, $ext);
- }
- public function getPicture($path, $ext = self::defPicTypes)
- {
- return $this->filterFile($path, $ext);
- }
- }
- $gal = new galerija_dao();
- $gal->makethumbs = true;
- //$gal->forceDelete = true;
- $gal->setThumbSize(200, 200);
- $gal->setHomeDirs('../../../pic', '../../../pic/thu');
- //$gal->setThumbScale(0.75);
- //$gal->makethumbs = true;*/
- //$gal->setThumbSize()
- echo '<pre>';
- //var_dump($gal->getPicture('../../../picture/Tree.jpg'));
- var_dump($gal->getPictures('../../../pictures'));
- //$gal->cleanUp();
- echo '</pre>';
- //$gal->makeNewThumbs();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement