PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/trash/photo.php

https://github.com/inoves/ModInoves
PHP | 67 lines | 56 code | 8 blank | 3 comment | 9 complexity | 2407af1d47fa55f5224c13385d517692 MD5 | raw file
  1. <?php
  2. class Photo extends ActiveRecord\Model {
  3. static $versions = array(
  4. 'quad/' => ' -resize 60x60^ -gravity center -extent 60x60 ',
  5. 'thumbs/'=> ' -resize 70x80\> ',
  6. 'small/'=> ' -resize 150x150\> ',
  7. 'medium/'=> ' -resize 300x300\> ',
  8. 'screen/'=> ' -resize 480x600\> '
  9. );
  10. public function url_photo($version){
  11. return Helper::url_photo($this, $version, 'name', 'photos');
  12. }
  13. public function upload($page_id, $file, $desc='')
  14. {
  15. if (!is_uploaded_file($file['tmp_name']))
  16. throw new Exception('Not file uploaded.');
  17. $photo = new Photo();
  18. $photo->page_id = $page_id;
  19. $photo->description = $desc;
  20. list($name, $ext)=explode('.',$file['name']);
  21. $photo->name = uniqid().'.'.$ext;
  22. $photo->save();
  23. $dir = Config::$path_photos . $photo->id.'/';
  24. foreach( Photo::$versions as $version => $value ){
  25. @mkdir( $dir.$version, 0777, TRUE );
  26. //error_log( $dir.$version , 4);
  27. $str_exec = "convert ".$file['tmp_name'] . $value . $dir.$version.$photo->name;
  28. //error_log( $str_exec , 4);
  29. exec( $str_exec );
  30. }
  31. move_uploaded_file($file['tmp_name'], $dir.$photo->name);
  32. //exit;
  33. }
  34. public function del_photo()
  35. {
  36. $dir = Config::$path_photos.$this->id.'/';
  37. $this->rmdirr($dir);
  38. $this->destroy();
  39. }
  40. private function rmdirr($dirname)
  41. {
  42. if (!file_exists($dirname)):
  43. return false;
  44. endif;
  45. if (is_file($dirname) || is_link($dirname)):
  46. return unlink($dirname);
  47. endif;
  48. $dir = dir($dirname);
  49. while (false !== $entry = $dir->read()):
  50. if ($entry == '.' || $entry == '..'):
  51. continue;
  52. endif;
  53. self::rmdirr($dirname . DIRECTORY_SEPARATOR . $entry);
  54. endwhile;
  55. $dir->close();
  56. return rmdir($dirname);
  57. }
  58. }
  59. ?>