GalleryFieldImage.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. class GalleryFieldImage extends \ElggFile{
  3. const SUBTYPE = "gallery_field_image";
  4. protected function initializeAttributes() {
  5. parent::initializeAttributes();
  6. $this->attributes['subtype'] = self::SUBTYPE;
  7. }
  8. /**
  9. * Save the image
  10. *
  11. * @warning container_guid must be set first
  12. *
  13. * @param array $data
  14. * @return bool
  15. */
  16. public function save($data = null) {
  17. if (!parent::save()) {
  18. return false;
  19. }
  20. /*if ($data) {
  21. // new image
  22. $this->simpletype = "image";
  23. $this->saveImageFile($data);
  24. $this->saveThumbnails();
  25. $this->extractExifData();
  26. }*/
  27. return true;
  28. }
  29. /**
  30. * Delete image
  31. *
  32. * @return bool
  33. */
  34. public function delete() {
  35. $thumb_image = get_entity($this->thumb_file_guid);
  36. if($thumb_image)
  37. {
  38. $thumb_image->delete();
  39. }
  40. return parent::delete();
  41. }
  42. /**
  43. * Get the URL of this image
  44. *
  45. * @return string
  46. */
  47. public function getURL($size = 'small') {
  48. return elgg_normalize_url("gallery_field/image/{$this->guid}/{$size}");
  49. }
  50. /**
  51. * Save the uploaded image
  52. *
  53. * @param array $data
  54. */
  55. protected function saveImageFile($data) {
  56. $imginfo = getimagesize($data['tmp_name']);
  57. // move the uploaded file into album directory
  58. $this->setOriginalFilename($data['name']);
  59. $filename = $this->getFilenameOnFilestore();
  60. $result = move_uploaded_file($data['tmp_name'], $filename);
  61. if (!$result) {
  62. return false;
  63. }
  64. return true;
  65. }
  66. protected static function genGUID()
  67. {
  68. if (function_exists('com_create_guid')){
  69. return com_create_guid();
  70. }else{
  71. //mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
  72. $charid = strtoupper(md5(uniqid(rand(), true)));
  73. $hyphen = chr(45);// "-"
  74. $uuid = chr(123)// "{"
  75. .substr($charid, 0, 8).$hyphen
  76. .substr($charid, 8, 4).$hyphen
  77. .substr($charid,12, 4).$hyphen
  78. .substr($charid,16, 4).$hyphen
  79. .substr($charid,20,12)
  80. .chr(125);// "}"
  81. return $uuid;
  82. }
  83. }
  84. /**
  85. *
  86. * @param string $tmp_name path to file
  87. * @param string $type mime type
  88. * @return self
  89. * @throws Exception
  90. */
  91. public static function createFromFile($tmp_name, $type)
  92. {
  93. $mime_type = ElggFile::detectMimeType($tmp_name, $type);
  94. if(false == in_array($mime_type, array("image/jpeg","image/jpg")))
  95. {
  96. register_error(elgg_echo("gallery_field:only_jpg"));
  97. return null;
  98. }
  99. $ext = "jpg";
  100. $file = new self();
  101. $thumb_file = new ElggFile();
  102. $random_guid = self::genGUID();
  103. $file->setFilename($random_guid.".".$ext);
  104. $thumb_file->setFilename($random_guid."_thumb.".$ext);
  105. $file->setMimeType($mime_type);
  106. $thumb_file->setMimeType($mime_type);
  107. $imgsizearray = getimagesize($tmp_name);
  108. if ($imgsizearray == false) {
  109. register_error("bad file");
  110. return null;
  111. }
  112. $width = $imgsizearray[0];
  113. $height = $imgsizearray[1];
  114. $file->open("write");
  115. $file->write(self::cropImage($tmp_name, $width, $height, 760, 580));
  116. $file->close();
  117. $thumb_file->open("write");
  118. $thumb_file->write(self::cropImage($tmp_name, $width, $height, 200, 140));
  119. $thumb_file->close();
  120. $thumb_file->save();
  121. $file->thumb_file_guid = $thumb_file->guid;
  122. $file->save();
  123. return $file;
  124. }
  125. protected static function cropImage($tmp_name, $orig_width, $orig_height, $new_width, $new_height)
  126. {
  127. $crop = array();
  128. if($new_height / $new_width > $orig_height / $orig_width)
  129. {
  130. $scaled_new_width = $new_width * $orig_height / $new_height;
  131. $crop = array(
  132. 'x1' => round(($orig_width - $scaled_new_width) / 2),
  133. 'y1' => 0,
  134. 'x2' => round(($orig_width + $scaled_new_width) / 2),
  135. 'y2' => $orig_height
  136. );
  137. }
  138. else
  139. {
  140. $scaled_new_height = $new_height * $orig_width / $new_width;
  141. $crop = array(
  142. 'x1' => 0,
  143. 'y1' => round(($orig_height - $scaled_new_height) / 2),
  144. 'x2' => $orig_width,
  145. 'y2' => round(($orig_height + $scaled_new_height) / 2)
  146. );
  147. }
  148. /*echo "<pre>";
  149. var_dump(
  150. $orig_width,
  151. $orig_height,
  152. $scaled_new_width,
  153. $tmp_name,
  154. $new_width,
  155. $new_height,
  156. $crop);*/
  157. return get_resized_image_from_existing_file(
  158. $tmp_name,
  159. $new_width,
  160. $new_height,
  161. false,
  162. $crop['x1'],
  163. $crop['y1'],
  164. $crop['x2'],
  165. $crop['y2'],
  166. true);
  167. }
  168. }