PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/api/1/public/image.php

https://github.com/dreamhackcrew/API
PHP | 203 lines | 138 code | 60 blank | 5 comment | 40 complexity | d2d386c49f4423a95df9d0b962cfa01b MD5 | raw file
  1. <?php
  2. if ( !isset($request[0]) ) {
  3. response(array(
  4. 'error'=>'No image defined!',
  5. ));
  6. }
  7. //$request[0] = explode('.',$request[0]);
  8. $image = $request[0];
  9. $width = 100;
  10. $height = 100;
  11. if ( isset($_GET['width']) && is_numeric($_GET['width']) )
  12. $width = min($_GET['width'],1000);
  13. if ( isset($_GET['height']) && is_numeric($_GET['height']) )
  14. $height = min($_GET['height'],1000);
  15. image::showThumb($image,$height,$width);
  16. class image {
  17. static function storage() {
  18. return '/var/www/crew.dreamhack.se/storage/images/';
  19. }
  20. static function getId( $id ) {
  21. if ( is_file( self::storage().$id ) )
  22. return $id;
  23. return false;
  24. return $id;
  25. }
  26. static function showThumb( $ident , $height, $width, $radius = 0 ) {
  27. if ( ($data = db()->fetchSingle("SELECT * FROM images WHERE ident='%s' ORDER BY id DESC LIMIT 1",$ident)) == '' )
  28. return !trigger_error('File identification not found in database!');
  29. if ( !is_numeric($height) || !is_numeric($width) )
  30. return !trigger_error('Syntax error');
  31. if ( !is_file( self::storage().$ident ) )
  32. return !trigger_error('Sourcefile "'.$ident.'" not found!');
  33. if ( !is_file( self::storage().$ident.'.'.$height.'.'.$width ) )
  34. if ( !self::makeThumb( $ident , $height, $width ) )
  35. return !trigger_error('Failed to make thumbnail',E_USER_WARNING);
  36. $file = self::storage().$ident.'.'.$height.'.'.$width;
  37. header('Content-type: '.$data['type']);
  38. die(file_get_contents($file));
  39. }
  40. static function makeThumb( $id , $height, $width, $radius = 0 ) {
  41. if ( !is_numeric($height) || !is_numeric($width) )
  42. return !trigger_error('Syntax error');
  43. if ( !is_file( self::storage().$id ) )
  44. return !trigger_error('Sourcefile with id "'.$id.'" not found!');
  45. if ( !$file = image::getStats($id) )
  46. return !trigger_error('File identification not found in database!');
  47. if ( !$im = self::createFromMime( self::storage().$file['file'], $file['type'] ) )
  48. return !trigger_error('Failed read file!');
  49. $sh = imagesy($im);
  50. $sw = imagesx($im);
  51. if ( $height < 1 )
  52. $heightd = $sh * ($width / $sw);
  53. else
  54. $heightd = $height;
  55. if ( $width < 1 )
  56. $widthd = $sw * ($heightd / $sh);
  57. else
  58. $widthd = $width;
  59. $thumb = imagecreatetruecolor( $widthd, $heightd );
  60. if ( $file['BoxW'] != 0 ) { // Om det finns en definierad yta som ska visas
  61. if ( $file['BoxH'] < $file['BoxW'] ) {
  62. $dw = ( $file['BoxW'] * $sw / 100 );
  63. $dh = ( $heightd / $widthd ) * $dw;
  64. } else {
  65. $dh = ( $file['BoxH'] * $sh / 100 ) ;
  66. $dw = ( $widthd / $heightd ) * $dh;
  67. }
  68. $dx = ( $file['BoxX'] * $sw / 100 ) - ($dw / 2) + ( $file['BoxW'] * $sw / 200 );
  69. $dy = ( $file['BoxY'] * $sh / 100 ) - ($dh / 2) + ( $file['BoxH'] * $sh / 200 );
  70. } else {
  71. $ratioSrc = ($sh / $sw);
  72. $ratioDst = ($heightd / $widthd);
  73. if ( $ratioSrc > $ratioDst ) {
  74. $dw = $sw;
  75. $dh = ( $heightd / $widthd ) * $dw;
  76. } else {
  77. $dh = $sh;
  78. $dw = ( $widthd / $heightd ) * $dh;
  79. }
  80. $dx = ( $sw / 2 ) - ( $dw / 2 );
  81. $dy = ( $sh / 2 ) - ( $dh / 2 );
  82. }
  83. //p("dh:$dh dw:$dw dx$dx dy$dy");
  84. if ( !imagecopyresampled( $thumb, $im, 0, 0, $dx, $dy, $widthd, $heightd, $dw, $dh ) )
  85. return !trigger_error('Failed to resize image!');
  86. //$textcolor = imagecolorallocate($thumb, 255, 255, 255);
  87. //imagestring($thumb, 1, 0, 0, "dh:$dh dw:$dw", $textcolor);
  88. //imagestring($thumb, 1, 0, 10,"dx$dx dy$dy", $textcolor);
  89. $filename = self::storage().$file['file'].'.'.$height.'.'.$width;
  90. if ( !$radius ) {
  91. if ( !self::saveFileFromMime( $thumb, $filename, 'image/jpeg') )
  92. return !trigger_error('Failed to save thumbnail');
  93. } else {
  94. if ( !self::roundCorners( $thumb, $radius ) )
  95. return !trigger_error('Failed to make round corners on thumbnail');
  96. if ( !self::saveFileFromMime( $thumb, $filename.'.'.$radius, 'image/png') )
  97. return !trigger_error('Failed to save thumbnail');
  98. }
  99. return is_file($filename);
  100. }
  101. function getStats( $ident ) {
  102. if ( !$id = self::getId( $ident) )
  103. return @!trigger_error('File identification not found in database!');
  104. if ( !is_file( self::storage().$id ) )
  105. return !trigger_error('Sourcefile with id "'.$id.'" not found!');
  106. if ( !$data = db()->fetchSingle("SELECT * FROM images WHERE file='%s'",$id) )
  107. return !trigger_error('File identification not found in database!');
  108. if ( !$im = self::createFromMime( self::storage().$data['file'], $data['type'] ) )
  109. return !trigger_error('Failed read file!');
  110. $data['width'] = imagesx($im);
  111. $data['height'] = imagesy($im);
  112. return $data;
  113. }
  114. static function createFromMime( $file, $mime ) {
  115. if ( !is_file($file) )
  116. return !trigger_error('File "'.$file.'" not found!');
  117. switch ($mime) {
  118. case 'image/gif':
  119. return imagecreatefromgif( $file );
  120. case 'image/jpeg':
  121. case 'image/pjpeg':
  122. return imagecreatefromjpeg( $file );
  123. case 'image/png':
  124. return imagecreatefrompng( $file );
  125. default:
  126. return !trigger_error('Mime type "'.$mime.'" not supported!');
  127. }
  128. }
  129. static function saveFileFromMime( $image, $file, $mime ) {
  130. switch ($mime) {
  131. case 'image/gif':
  132. return imagegif( $image,$file );
  133. case 'image/jpeg':
  134. case 'image/pjpeg':
  135. return imagejpeg( $image,$file );
  136. case 'image/png':
  137. return imagepng( $image,$file );
  138. default:
  139. return !trigger_error('Mime type "'.$mime.'" not supported!');
  140. }
  141. }
  142. }
  143. ?>