Image.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. ApplicationSettings::RegisterDefaultSetting("images", "upload_location", "images/uploads");
  3. ApplicationSettings::RegisterDefaultSetting("images", "thumbnail_width", "480");
  4. ApplicationSettings::RegisterDefaultSetting("images", "thumbnail_height", "360");
  5. class Image extends DBObjectAutoCreate {
  6. public $Path,$ThumbnailPath;
  7. private $Filename,$TempFile;
  8. public static function IsValidType($ext){
  9. // use a switch because it's quicker than a loop
  10. switch (strtolower($ext)){
  11. case "bmp":
  12. case "dib":
  13. case "jpeg":
  14. case "jpg":
  15. case "jpe":
  16. case "jfif":
  17. case "gif":
  18. case "tif":
  19. case "tiff":
  20. case "png":
  21. case "ico":
  22. return true;
  23. default:
  24. return false;
  25. }
  26. }
  27. public static function GetImagesByAlbum($albumId) {
  28. self::CreateTable("images");
  29. $images=array();
  30. $prep=self::$PDO->prepare("SELECT image_id FROM images WHERE album_id=? AND image_deleted=0");
  31. $prep->execute(array($albumId));
  32. while ($imageId=$prep->fetchColumn())
  33. $images[]=new Image($imageId);
  34. return $images;
  35. }
  36. public function __construct($id=0,$tempFile="") {
  37. if (!is_numeric($id)){
  38. $this->Filename=$id;
  39. $this->TempFile=$tempFile;
  40. $id=0;
  41. }
  42. parent::__construct("images", "image_id", $id);
  43. if ($this->ImageId){
  44. $dir=ApplicationSettings::GetSetting("images", "upload_location");
  45. $info=pathinfo($this->ImageFilename);
  46. $this->Path=$dir.'/'.$this->ImageFilename;
  47. $this->ThumbnailPath=$dir.'/'.$info['filename'].'.thumbnail.'.$info['extension'];
  48. }
  49. }
  50. private function MakeFileName() {
  51. return $this->ImageId."_".Helper::MakeStringUrlSafe($this->Filename);
  52. }
  53. public function Save() {
  54. $newSave=!$this->ImageId;
  55. parent::Save();
  56. if ($newSave && $this->ImageId){
  57. $this->ImageFilename=$this->MakeFileName();
  58. $imageDir=ApplicationSettings::GetSetting("images", "upload_location");
  59. if(!move_uploaded_file($this->TempFile, $imageDir.'/'.$this->ImageFilename))
  60. throw new Exception("Unable to move uploaded file: ".$imageDir.'/'.$this->ImageFilename.', '.$this->TempFile);
  61. parent::Save();
  62. }
  63. }
  64. }