Image.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. if (!file_exists(ApplicationSettings::GetSetting("images", "upload_location")))
  6. mkdir(ApplicationSettings::GetSetting("images", "upload_location"));
  7. class Image extends DBObjectAutoCreate {
  8. public $Path,$ThumbnailPath;
  9. private $Filename,$TempFile;
  10. public static function IsValidType($ext){
  11. // use a switch because it's quicker than a loop
  12. switch (strtolower($ext)){
  13. case "bmp":
  14. case "dib":
  15. case "jpeg":
  16. case "jpg":
  17. case "jpe":
  18. case "jfif":
  19. case "gif":
  20. case "tif":
  21. case "tiff":
  22. case "png":
  23. case "ico":
  24. return true;
  25. default:
  26. return false;
  27. }
  28. }
  29. public static function GetImagesByAlbum($albumId) {
  30. self::CreateTable("images");
  31. $images=array();
  32. $prep=self::$PDO->prepare("SELECT image_id FROM images WHERE album_id=? AND image_deleted=0");
  33. $prep->execute(array($albumId));
  34. while ($imageId=$prep->fetchColumn())
  35. $images[]=new Image($imageId);
  36. return $images;
  37. }
  38. public function __construct($id=0,$tempFile="") {
  39. if (!is_numeric($id)){
  40. $this->Filename=$id;
  41. $this->TempFile=$tempFile;
  42. $id=0;
  43. }
  44. parent::__construct("images", "image_id", $id);
  45. if ($this->ImageId)
  46. $this->CalculatePaths();
  47. }
  48. private function CalculatePaths(){
  49. $dir=ApplicationSettings::GetSetting("images", "upload_location");
  50. $info=pathinfo($this->ImageFilename);
  51. $this->Path=$dir.'/'.$this->ImageFilename;
  52. $this->ThumbnailPath=$dir.'/'.$info['filename'].'.thumbnail.'.$info['extension'];
  53. }
  54. private function MakeFileName() {
  55. return $this->ImageId."_".Utils::MakeStringUrlSafe($this->Filename);
  56. }
  57. public function Save() {
  58. $newSave=!$this->ImageId;
  59. parent::Save();
  60. if ($newSave && $this->ImageId){
  61. $this->ImageFilename=$this->MakeFileName();
  62. $this->CalculatePaths();
  63. if(!move_uploaded_file($this->TempFile, $this->Path))
  64. throw new Exception("Unable to move uploaded file: ".$this->Path.', '.$this->TempFile);
  65. if (class_exists("Imagick")){
  66. $thumb=new Imagick($this->Path);
  67. $height=ApplicationSettings::GetSetting("images", "thumbnail_height");
  68. $width=ApplicationSettings::GetSetting("images", "thumbnail_width");
  69. $thumb->scaleImage(0,$height);
  70. $thumb->scaleImage($width,0);
  71. $thumb->writeImage($this->ThumbnailPath);
  72. $thumb->clear();
  73. $thumb->destroy();
  74. } else
  75. copy($this->Path, $this->ThumbnailPath);
  76. parent::Save();
  77. }
  78. }
  79. }