Image.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 DBObject {
  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. $images=array();
  31. $prep=self::$PDO->prepare("SELECT image_id FROM images WHERE album_id=? AND image_deleted=0");
  32. $prep->execute(array($albumId));
  33. while ($imageId=$prep->fetchColumn())
  34. $images[]=new Image($imageId);
  35. return $images;
  36. }
  37. public function __construct($id=0,$tempFile="") {
  38. if (!is_numeric($id)){
  39. $this->Filename=$id;
  40. $this->TempFile=$tempFile;
  41. $id=0;
  42. }
  43. parent::__construct("images", "image_id", $id);
  44. if ($this->ImageId)
  45. $this->CalculatePaths();
  46. }
  47. private function CalculatePaths(){
  48. $dir=ApplicationSettings::GetSetting("images", "upload_location");
  49. $info=pathinfo($this->ImageFilename);
  50. $this->Path=$dir.'/'.$this->ImageFilename;
  51. $this->ThumbnailPath=$dir.'/'.$info['filename'].'.thumbnail.'.$info['extension'];
  52. }
  53. private function MakeFileName() {
  54. return $this->ImageId."_".Utils::MakeStringUrlSafe($this->Filename);
  55. }
  56. public function Save() {
  57. $newSave=!$this->ImageId;
  58. parent::Save();
  59. if ($newSave && $this->ImageId){
  60. $this->ImageFilename=$this->MakeFileName();
  61. $this->CalculatePaths();
  62. if(!move_uploaded_file($this->TempFile, $this->Path))
  63. throw new Exception("Unable to move uploaded file: ".$this->Path.', '.$this->TempFile);
  64. if (class_exists("Imagick")){
  65. $thumb=new Imagick($this->Path);
  66. $height=ApplicationSettings::GetSetting("images", "thumbnail_height");
  67. $width=ApplicationSettings::GetSetting("images", "thumbnail_width");
  68. $thumb->scaleImage(0,$height);
  69. $thumb->scaleImage($width,0);
  70. $thumb->writeImage($this->ThumbnailPath);
  71. $thumb->clear();
  72. $thumb->destroy();
  73. } else
  74. copy($this->Path, $this->ThumbnailPath);
  75. parent::Save();
  76. }
  77. }
  78. }