PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/ext/handle_pixel/main.php

https://gitlab.com/dali99/shimmie2-Material-Theme
PHP | 223 lines | 135 code | 30 blank | 58 comment | 12 complexity | 5187697dfcef42ef9fea7971cc9b6018 MD5 | raw file
  1. <?php
  2. /**
  3. * Name: Handle Pixel
  4. * Author: Shish <webmaster@shishnet.org>
  5. * Link: http://code.shishnet.org/shimmie2/
  6. * Description: Handle JPEG, PNG, GIF, etc files
  7. */
  8. class PixelFileHandler extends DataHandlerExtension {
  9. /**
  10. * @param string $ext
  11. * @return bool
  12. */
  13. protected function supported_ext($ext) {
  14. $exts = array("jpg", "jpeg", "gif", "png");
  15. $ext = (($pos = strpos($ext,'?')) !== false) ? substr($ext,0,$pos) : $ext;
  16. return in_array(strtolower($ext), $exts);
  17. }
  18. /**
  19. * @param string $filename
  20. * @param array $metadata
  21. * @return Image|null
  22. */
  23. protected function create_image_from_data(/*string*/ $filename, /*array*/ $metadata) {
  24. $image = new Image();
  25. $info = getimagesize($filename);
  26. if(!$info) return null;
  27. $image->width = $info[0];
  28. $image->height = $info[1];
  29. $image->filesize = $metadata['size'];
  30. $image->hash = $metadata['hash'];
  31. $image->filename = (($pos = strpos($metadata['filename'],'?')) !== false) ? substr($metadata['filename'],0,$pos) : $metadata['filename'];
  32. $image->ext = (($pos = strpos($metadata['extension'],'?')) !== false) ? substr($metadata['extension'],0,$pos) : $metadata['extension'];
  33. $image->tag_array = Tag::explode($metadata['tags']);
  34. $image->source = $metadata['source'];
  35. return $image;
  36. }
  37. /**
  38. * @param string $file
  39. * @return bool
  40. */
  41. protected function check_contents(/*string*/ $file) {
  42. $valid = Array(IMAGETYPE_PNG, IMAGETYPE_GIF, IMAGETYPE_JPEG);
  43. if(!file_exists($file)) return false;
  44. $info = getimagesize($file);
  45. if(is_null($info)) return false;
  46. if(in_array($info[2], $valid)) return true;
  47. return false;
  48. }
  49. /**
  50. * @param string $hash
  51. * @return bool
  52. */
  53. protected function create_thumb(/*string*/ $hash) {
  54. $outname = warehouse_path("thumbs", $hash);
  55. if(file_exists($outname)) {
  56. return true;
  57. }
  58. return $this->create_thumb_force($hash);
  59. }
  60. /**
  61. * @param string $hash
  62. * @return bool
  63. */
  64. protected function create_thumb_force(/*string*/ $hash) {
  65. global $config;
  66. $inname = warehouse_path("images", $hash);
  67. $outname = warehouse_path("thumbs", $hash);
  68. $ok = false;
  69. switch($config->get_string("thumb_engine")) {
  70. default:
  71. case 'gd':
  72. $ok = $this->make_thumb_gd($inname, $outname);
  73. break;
  74. case 'convert':
  75. $ok = $this->make_thumb_convert($inname, $outname);
  76. break;
  77. }
  78. return $ok;
  79. }
  80. public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event) {
  81. $event->add_part("
  82. <form>
  83. <select class='shm-zoomer'>
  84. <option value='full'>Full Size</option>
  85. <option value='width'>Fit Width</option>
  86. <option value='height'>Fit Height</option>
  87. <option value='both'>Fit Both</option>
  88. </select>
  89. </form>
  90. ", 20);
  91. $u_ilink = $event->image->get_image_link();
  92. $nu_enabled = (strpos($u_ilink, '?') !== false ? "<input type='hidden' name='q' value='image/{$event->image->id}.{$event->image->ext}' />" : "");
  93. $event->add_part("
  94. <form action='{$u_ilink}'>
  95. $nu_enabled
  96. <input type='submit' value='Image Only'>
  97. </form>
  98. ", 21);
  99. }
  100. // IM thumber {{{
  101. /**
  102. * @param string $inname
  103. * @param string $outname
  104. * @return bool
  105. */
  106. private function make_thumb_convert(/*string*/ $inname, /*string*/ $outname) {
  107. global $config;
  108. $w = $config->get_int("thumb_width");
  109. $h = $config->get_int("thumb_height");
  110. $q = $config->get_int("thumb_quality");
  111. $convert = $config->get_string("thumb_convert_path");
  112. // ffff imagemagic fails sometimes, not sure why
  113. //$format = "'%s' '%s[0]' -format '%%[fx:w] %%[fx:h]' info:";
  114. //$cmd = sprintf($format, $convert, $inname);
  115. //$size = shell_exec($cmd);
  116. //$size = explode(" ", trim($size));
  117. $size = getimagesize($inname);
  118. if($size[0] > $size[1]*5) $size[0] = $size[1]*5;
  119. if($size[1] > $size[0]*5) $size[1] = $size[0]*5;
  120. // running the call with cmd.exe requires quoting for our paths
  121. $format = '"%s" "%s[0]" -extent %ux%u -flatten -strip -thumbnail %ux%u -quality %u jpg:"%s"';
  122. $cmd = sprintf($format, $convert, $inname, $size[0], $size[1], $w, $h, $q, $outname);
  123. $cmd = str_replace("\"convert\"", "convert", $cmd); // quotes are only needed if the path to convert contains a space; some other times, quotes break things, see github bug #27
  124. exec($cmd, $output, $ret);
  125. log_debug('handle_pixel', "Generating thumnail with command `$cmd`, returns $ret");
  126. if($config->get_bool("thumb_optim", false)) {
  127. exec("jpegoptim $outname", $output, $ret);
  128. }
  129. return true;
  130. }
  131. // }}}
  132. // epeg thumber {{{
  133. /**
  134. * @param string $inname
  135. * @param string $outname
  136. * @return bool
  137. */
  138. private function make_thumb_epeg(/*string*/ $inname, /*string*/ $outname) {
  139. global $config;
  140. $w = $config->get_int("thumb_width");
  141. exec("epeg $inname -c 'Created by EPEG' --max $w $outname");
  142. return true;
  143. }
  144. // }}}
  145. // GD thumber {{{
  146. /**
  147. * @param string $inname
  148. * @param string $outname
  149. * @return bool
  150. */
  151. private function make_thumb_gd(/*string*/ $inname, /*string*/ $outname) {
  152. global $config;
  153. $thumb = $this->get_thumb($inname);
  154. $ok = imagejpeg($thumb, $outname, $config->get_int('thumb_quality'));
  155. imagedestroy($thumb);
  156. return $ok;
  157. }
  158. /**
  159. * @param string $tmpname
  160. * @return resource
  161. */
  162. private function get_thumb(/*string*/ $tmpname) {
  163. global $config;
  164. $info = getimagesize($tmpname);
  165. $width = $info[0];
  166. $height = $info[1];
  167. $memory_use = (filesize($tmpname)*2) + ($width*$height*4) + (4*1024*1024);
  168. $memory_limit = get_memory_limit();
  169. if($memory_use > $memory_limit) {
  170. $w = $config->get_int('thumb_width');
  171. $h = $config->get_int('thumb_height');
  172. $thumb = imagecreatetruecolor($w, min($h, 64));
  173. $white = imagecolorallocate($thumb, 255, 255, 255);
  174. $black = imagecolorallocate($thumb, 0, 0, 0);
  175. imagefill($thumb, 0, 0, $white);
  176. imagestring($thumb, 5, 10, 24, "Image Too Large :(", $black);
  177. return $thumb;
  178. }
  179. else {
  180. if($width > $height*5) $width = $height*5;
  181. if($height > $width*5) $height = $width*5;
  182. $image = imagecreatefromstring(file_get_contents($tmpname));
  183. $tsize = get_thumbnail_size($width, $height);
  184. $thumb = imagecreatetruecolor($tsize[0], $tsize[1]);
  185. imagecopyresampled(
  186. $thumb, $image, 0, 0, 0, 0,
  187. $tsize[0], $tsize[1], $width, $height
  188. );
  189. return $thumb;
  190. }
  191. }
  192. // }}}
  193. }