Advertisement
Guest User

Untitled

a guest
May 19th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.37 KB | None | 0 0
  1. <?php
  2. //MarkoZz 0.5.1
  3. class galerija_dao
  4. {
  5. const defPicTypes = 'jpg,jpeg,gif,png';
  6. const defArchiveTypes = 'zip,rar';
  7. public $makethumbs = false;
  8. public $forceDelete = false;
  9. private $defAlbum = false;
  10. private $defThumb = false;
  11. private $height = 80;
  12. private $width = 80;
  13. private $scale = 1;
  14. private $data;
  15.  
  16. protected function getFiles($path = 'pictures')
  17. {
  18. $dir = opendir($path);
  19. $i = 0;
  20. $data = array();
  21.  
  22. while (false !== ($actualfile = readdir($dir))) {
  23. $i++;
  24. if ($i > 2) //dir structure
  25. {
  26. $parts = explode('.', $actualfile);
  27. $file = $path.'/'.$actualfile;
  28. //create more attributes to pass >> $temp['size'] = filesize($file); <<
  29.  
  30. $temp = array( 'ctime' => filectime($file),
  31. 'size' => filesize($file),
  32. 'input' => $path,
  33. 'path' => $file,
  34. 'ext' => $parts[sizeof($parts)-1]
  35. );
  36.  
  37. unset($parts[sizeof($parts)-1]);
  38.  
  39. foreach ($parts as $part)
  40. $s .= $part;
  41.  
  42. $temp['name'] = $s;
  43.  
  44. if ($this->makethumbs)
  45. if ($thumb = $this->makeThumb($temp))
  46. $temp['thumb'] = $thumb;
  47.  
  48. $data[] = $temp;
  49. }
  50. $parts = '';
  51. $temp = '';
  52. $s = '';
  53. }
  54. //exit(var_dump($data));
  55. $this->makethumbs = false;
  56. closedir($dir);
  57. return $data;
  58. }
  59.  
  60. protected function getFile($path)
  61. {
  62. if(!file_exists($path))
  63. exit('File not found: "'.$path.'"');
  64. $data = array();
  65. $i = 0;
  66. $parts = explode('.', $path);
  67.  
  68. $tmp = explode('/', $path);
  69. $name = $tmp[sizeof($tmp)-1];
  70. unset($tmp[sizeof($tmp)-1]);
  71. foreach($tmp as $str)
  72. {
  73. if($i > 0)
  74. $input .= '/'.$str;
  75. else
  76. $input .= $str;
  77. $i++;
  78. }
  79. $file = $path;
  80. //create more attributes to pass >> $temp['size'] = filesize($file); <<
  81.  
  82. $temp = array( 'ctime' => filectime($file),
  83. 'size' => filesize($file),
  84. 'input' => $input,
  85. 'path' => $file,
  86. 'ext' => $parts[sizeof($parts)-1]
  87. );
  88.  
  89. $tmpName = explode('.', $name);
  90. unset($tmpName[sizeof($tmpName)-1]);
  91. foreach($tmpName as $out)
  92. $s .= $out;
  93.  
  94. $temp['name'] = $s;
  95.  
  96. if($this->makethumbs)
  97. if($thumb = $this->makeThumb($temp))
  98. $temp['thumb'] = $thumb;
  99.  
  100. $data[] = $temp;
  101.  
  102. $this->makethumbs = false;
  103.  
  104. return $data;
  105. }
  106.  
  107. private function checkForUpdate($data)
  108. {
  109. $valid = false;
  110.  
  111. list($width, $height) = getimagesize($data['savepath']);
  112.  
  113. if($height != $this->height && $width != $this->width)
  114. $valid = true;
  115. if(($data['ctime'] - time()) > 54000)
  116. $valid = true;
  117. if($this->forceDelete)
  118. $valid = true;
  119.  
  120. return $valid;
  121. }
  122.  
  123. private function makeThumb($data)
  124. {
  125. if (!is_dir($data['input'].'/thumbs'))
  126. mkdir($data['input'].'/thumbs');
  127.  
  128. $data['savepath'] = $data['input'].'/thumbs/'.$data['name'].'.'.$data['ext'];
  129.  
  130. if (file_exists($data['savepath'])) {
  131. if ($this->checkForUpdate($data)) {
  132. $this->removeThumb($data);
  133. $this->writeLog($data, false, true);
  134. } else {
  135. $this->writeLog($data, false);
  136. return $data['savepath'];
  137. }
  138. }
  139.  
  140. $param['path'] = $data['path'];
  141.  
  142. if ($data['ext'] == 'jpg' || $data['ext'] == 'jpeg') {
  143. if ($image = imagecreatefromjpeg($data['path'])) {
  144. $param['image'] = $image;
  145. imagejpeg($this->resizePic($param),$data['savepath']);
  146. $this->writeLog($data, true);
  147. return $data['savepath'];
  148. } else {
  149. return false;
  150. }
  151. } elseif( $data['ext'] == 'gif') {
  152. if($image = imagecreatefromjgif($data['path'])) {
  153. $param['image'] = $image;
  154. imagegif($this->resizePic($param),$data['savepath']);
  155. $this->writeLog($data, true);
  156. return $data['savepath'];
  157. } else{
  158. return false;
  159. }
  160. } elseif ($data['ext'] == 'png') {
  161. if ($image = imagecreatefrompng($data['path'])) {
  162. $param['image'] = $image;
  163. imagepng($this->resizePic($param), $data['savepath']);
  164. $this->writeLog($data, true);
  165. return $data['savepath'];
  166. } else {
  167. return false;
  168. }
  169. } else {
  170. return false;
  171. }
  172. }
  173.  
  174. private function writeLog($data, $success, $update=false, $move=false)
  175. {
  176. if(!is_dir($data['input'].'/log'))
  177. mkdir($data['input'].'/log');
  178.  
  179. if(!file_exists($data['input'].'/log/gallery.txt'))
  180. fopen($data['input'].'/log/gallery.txt', 'w');
  181.  
  182. $string = ($success ? "made new|*|\t" : "file found, skipped|*|");
  183.  
  184. if($update)
  185. $string = "file removed|*|\t";
  186. if($move)
  187. $string = "file move[from|to]|*|";
  188.  
  189. file_put_contents($data['input'].'/log/gallery.txt', date('h:i:s')."->".time()."|*|".$string."\t".$data['savepath']."\r\n", FILE_APPEND);
  190. }
  191.  
  192. public function setThumbSize($height, $width)
  193. {
  194. $this->height = $height;
  195. $this->width = $width;
  196. }
  197.  
  198. public function setThumbScale($scale)
  199. {
  200. $this->scale = $scale;
  201. }
  202.  
  203. public function setHomeDirs($album,$thumbs)
  204. {
  205. $this->defAlbum = $album;
  206. if(!is_dir($album))
  207. mkdir($album);
  208. $this->defThumb = $thumbs;
  209. if(!is_dir($thumbs))
  210. mkdir($thumbs);
  211. }
  212.  
  213. public function cleanUp()
  214. {
  215. if(($this->defAlbum && $this->defThumb))
  216. {
  217. //echo var_dump($this->data);
  218. if(is_array($this->data[0]))
  219. {
  220. $valid = rmdir($this->data[0]['input'].'/thumbs');
  221. foreach($this->data as $data)
  222. {
  223. unlink($data['thumb']);
  224. unlink($data['savepath']);
  225. $this->writeLog($data, false, true);
  226. }
  227. //$valid = $valid & rmdir($this->defAlbum);
  228. $valid = $valid & rmdir($this->defThumb);
  229. if(!$valid)
  230. exit("Couldn't delete everything.");
  231. else
  232. exit("Should be clean.");
  233. }
  234. else
  235. {
  236. $valid = rmdir($this->data['input'].'/thumbs');
  237. unlink($this->data['thumb']);
  238. unlink($this->data['savepath']);
  239. $this->writeLog($data, false, true);
  240. //$valid = $valid & rmdir($this->defAlbum);
  241. $valid = $valid & rmdir($this->defThumb);
  242. if(!$valid)
  243. exit("Couldn't delete everything.");
  244. else
  245. exit("Should be clean.");
  246. }
  247. }
  248.  
  249. }
  250.  
  251. private function repairLinks($data) //can't use same dir, file names don't change
  252. {
  253. if(is_array($data[0]))
  254. {
  255. foreach($data as $pic)
  256. {
  257. $newData = $pic;
  258.  
  259. $new['path'] = $this->defAlbum.'/'.$pic['name'].'.'.$pic['ext'];
  260. $new['thumb'] = $this->defThumb.'/'.$pic['name'].'.'.$pic['ext'];
  261. $pic['savepath'] = $pic['path'];
  262. $this->writeLog($pic, true, false, true);
  263.  
  264. if(rename($pic['path'], $new['path']) && rename($pic['thumb'], $new['thumb']))
  265. {
  266. $newData['path'] = $new['path'];
  267. $newData['thumb'] = $new['thumb'];
  268. $newData['savepath'] = $new['path'];
  269. $this->writeLog($newData, true, false, true);
  270. }
  271. else
  272. exit('Moving files to home directory failed! Check permissions.');
  273.  
  274. $out[] = $newData;
  275. }
  276. $data = $out;
  277. }
  278. else
  279. {
  280. $new['path'] = $this->defAlbum.'/'.$data['name'].'.'.$data['ext'];
  281. $new['thumb'] = $this->defThumb.'/'.$data['name'].'.'.$data['ext'];
  282. $data['savepath'] = $data['path'];
  283. $this->writeLog($data, true, false, true);
  284.  
  285. if(rename($data['path'], $new['path']) && rename($data['thumb'], $new['thumb']))
  286. {
  287. $data['path'] = $new['path'];
  288. $data['thumb'] = $new['thumb'];
  289. $data['savepath'] = $new['path'];
  290. $this->writeLog($data, true, false, true);
  291. }
  292. else
  293. exit('Moving files to home directory failed! Check permissions.');
  294. }
  295.  
  296. return $data;
  297. }
  298.  
  299. private function removeThumb($data)
  300. {
  301. if(is_dir($data['input'].'/thumbs'))
  302. {
  303. if(file_exists($data['input'].'/thumbs/'.$data['name'].'.'.$data['ext']))
  304. unlink($data['input'].'/thumbs/'.$data['name'].'.'.$data['ext']);
  305. else
  306. return false;
  307. }
  308. else
  309. exit('No thumbs directory found!');
  310. }
  311.  
  312. private function resizePic($param)
  313. {
  314. list($width, $height) = getimagesize($param['path']);
  315.  
  316. $thumb = imagecreatetruecolor($this->width*$this->scale, $this->height*$this->scale);
  317.  
  318. imagecopyresized($thumb, $param['image'], 0, 0, 0, 0, $this->width*$this->scale, $this->height*$this->scale, $width, $height);
  319.  
  320. return $thumb;
  321. }
  322.  
  323. private function filterFiles($path, $ext)
  324. {
  325. $input = $this->getFiles($path);
  326. $internalExt = explode(',', $ext);
  327. $data = array();
  328.  
  329. foreach($input as $part)
  330. {
  331. foreach($internalExt as $intExt)
  332. {
  333. if($part['ext'] == $intExt)
  334. $data[] = $part;
  335. }
  336. }
  337. if($this->defAlbum && $this->defThumb)
  338. $data = $this->repairLinks($data);
  339.  
  340. $this->data = $data;
  341.  
  342. return $data;
  343. }
  344.  
  345. private function filterFile($path, $ext)
  346. {
  347. $input = $this->getFile($path);
  348. $internalExt = explode(',', $ext);
  349. $data = false;
  350.  
  351. foreach($internalExt as $intExt)
  352. {
  353. if($input[0]['ext'] == $intExt)
  354. $data = $input[0];
  355. }
  356. if(!$data)
  357. exit('File type not supported!');
  358.  
  359. if($this->defAlbum && $this->defThumb)
  360. $data = $this->repairLinks($data);
  361.  
  362. $this->data = $data;
  363.  
  364. return $data;
  365. }
  366.  
  367. public function getPictures($path, $ext = self::defPicTypes)
  368. {
  369. return $this->filterFiles($path, $ext);
  370. }
  371.  
  372. public function getPicture($path, $ext = self::defPicTypes)
  373. {
  374. return $this->filterFile($path, $ext);
  375. }
  376. }
  377.  
  378. $gal = new galerija_dao();
  379. $gal->makethumbs = true;
  380. //$gal->forceDelete = true;
  381. $gal->setThumbSize(200, 200);
  382. $gal->setHomeDirs('../../../pic', '../../../pic/thu');
  383. //$gal->setThumbScale(0.75);
  384. //$gal->makethumbs = true;*/
  385.  
  386. //$gal->setThumbSize()
  387. echo '<pre>';
  388. //var_dump($gal->getPicture('../../../picture/Tree.jpg'));
  389. var_dump($gal->getPictures('../../../pictures'));
  390. //$gal->cleanUp();
  391. echo '</pre>';
  392.  
  393. //$gal->makeNewThumbs();
  394.  
  395. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement