PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/engine/core_modules/img.manipulation.php

https://gitlab.com/Nightprince/Warcry-CMS
PHP | 287 lines | 144 code | 26 blank | 117 comment | 32 complexity | 6d3c8fa125ed4dc910b8d5f0c16d8bb0 MD5 | raw file
  1. <?php
  2. if (!defined('init_engine'))
  3. {
  4. header('HTTP/1.0 404 not found');
  5. exit;
  6. }
  7. /**
  8. * ImageManipulation
  9. *
  10. * For more information on this file and how to use the class please visit
  11. * http://www.talkincode.com/
  12. *
  13. * @author Tech @ Talk In Code
  14. * @version 1.0
  15. * @copyright 2009 Talk In Code
  16. */
  17. /**
  18. * Image Resizer and Cropper. This class takes a reference to an image and
  19. * allows resizing and cropping of that image. This class is designed to
  20. * work independently of other classes.
  21. * You must have GD installed to utilise this class.
  22. *
  23. * @package ImageManipulation
  24. */
  25. class ImageManipulation {
  26. /**
  27. * An array to hold the settings for the image. Default values for
  28. * images are set here.
  29. *
  30. * @var array
  31. */
  32. public $image = array('targetx'=>0,
  33. 'targety'=>0,
  34. 'quality'=>75);
  35. /**
  36. * A boolean value to detect if an image has not been created. This
  37. * can be used to validate that an image is viable before trying
  38. * resize or crop.
  39. *
  40. * @var boolean
  41. */
  42. public $imageok = false;
  43. /**
  44. * Contructor method. Will create a new image from the target file.
  45. * Accepts an image filename as a string. Method also works out how
  46. * big the image is and stores this in the $image array.
  47. *
  48. * @param string $imgFile The image filename.
  49. */
  50. public function ImageManipulation($imgfile)
  51. {
  52. //detect image format
  53. $this->image["format"] = strtolower(substr(strrchr($imgfile, '.'), 1));
  54. $this->image["format"] = strtoupper($this->image["format"]);
  55. // convert image into usable format.
  56. if ( $this->image["format"] == "JPG" || $this->image["format"] == "JPEG" ) {
  57. //JPEG
  58. $this->image["format"] = "JPEG";
  59. $this->image["src"] = ImageCreateFromJPEG($imgfile);
  60. } elseif( $this->image["format"] == "PNG" ){
  61. //PNG
  62. $this->image["format"] = "PNG";
  63. $this->image["src"] = imagecreatefrompng($imgfile);
  64. } elseif( $this->image["format"] == "GIF" ){
  65. //GIF
  66. $this->image["format"] = "GIF";
  67. $this->image["src"] = ImageCreateFromGif($imgfile);
  68. } elseif ( $this->image["format"] == "WBMP" ){
  69. //WBMP
  70. $this->image["format"] = "WBMP";
  71. $this->image["src"] = ImageCreateFromWBMP($imgfile);
  72. } else {
  73. //DEFAULT
  74. return false;
  75. }
  76. // Image is ok
  77. $this->imageok = true;
  78. // Work out image size
  79. $this->image["sizex"] = imagesx($this->image["src"]);
  80. $this->image["sizey"] = imagesy($this->image["src"]);
  81. }
  82. /**
  83. * Sets the height of the image to be created. The width of the image
  84. * is worked out depending on the value of the height.
  85. *
  86. * @param int $height The height of the image.
  87. */
  88. public function setImageHeight($height=100)
  89. {
  90. //height
  91. $this->image["sizey_thumb"] = $height;
  92. $this->image["sizex_thumb"] = ($this->image["sizey_thumb"]/$this->image["sizey"])*$this->image["sizex"];
  93. }
  94. /**
  95. * Sets the width of the image to be created. The height of the image
  96. * is worked out depending on the value of the width.
  97. *
  98. * @param int $size The width of the image.
  99. */
  100. public function setImageWidth($width=100)
  101. {
  102. //width
  103. $this->image["sizex_thumb"] = $width;
  104. $this->image["sizey_thumb"] = ($this->image["sizex_thumb"]/$this->image["sizex"])*$this->image["sizey"];
  105. }
  106. /**
  107. * This method automatically sets the width and height depending
  108. * on the dimensions of the image up to a maximum value.
  109. *
  110. * @param int $size The maximum size of the image.
  111. */
  112. public function resize($size=100)
  113. {
  114. if ( $this->image["sizex"] >= $this->image["sizey"] ) {
  115. $this->image["sizex_thumb"] = $size;
  116. $this->image["sizey_thumb"] = ($this->image["sizex_thumb"]/$this->image["sizex"])*$this->image["sizey"];
  117. } else {
  118. $this->image["sizey_thumb"] = $size;
  119. $this->image["sizex_thumb"] = ($this->image["sizey_thumb"]/$this->image["sizey"])*$this->image["sizex"];
  120. }
  121. }
  122. public function resizeProper($max_width=100, $max_height=100)
  123. {
  124. $original_width = $this->image["sizex"];
  125. $original_height = $this->image["sizey"];
  126. if (($original_width > $max_width) or ($original_height > $max_height))
  127. {
  128. //original width exceeds, so reduce the original width to maximum limit.
  129. //calculate the height according to the maximum width.
  130. if(($original_width > $max_width) and ($original_height <= $max_height))
  131. {
  132. $percent = $max_width/$original_width;
  133. $new_width = $max_width;
  134. $new_height = round ($original_height * $percent);
  135. }
  136. //image height exceeds, recudece the height to maxmimum limit.
  137. //calculate the width according to the maximum height limit.
  138. if(($original_width <= $max_width) and ($original_height > $max_height))
  139. {
  140. $percent = $max_height/$original_height;
  141. $new_height = $max_height;
  142. $new_width = round ($original_width * $percent);
  143. }
  144. //both height and width exceeds.
  145. //but image can be vertical or horizontal.
  146. if(($original_width > $max_width) AND ($original_height > $max_height))
  147. {
  148. //if image has more width than height
  149. //resize width to maximum width.
  150. if ($original_width > $original_height)
  151. {
  152. $percent = $max_width/$original_width;
  153. $new_width = $max_width;
  154. $new_height = round ($original_height * $percent );
  155. }
  156. //image is vertical or square. More height than width.
  157. //resize height to maximum height.
  158. else
  159. {
  160. $new_height = $max_height;
  161. $percent = $max_height/$original_height;
  162. $new_height = $max_height;
  163. $new_width = round ($original_width * $percent);
  164. }
  165. }
  166. }
  167. //save
  168. $this->image["sizey_thumb"] = $new_height;
  169. $this->image["sizex_thumb"] = $new_width;
  170. }
  171. /**
  172. * This method sets the cropping values of the image. Be sure
  173. * to set the height and with of the image if you want the
  174. * image to be a certain size after cropping.
  175. *
  176. * @param int $x The x coordinates to start cropping from.
  177. * @param int $y The y coordinates to start cropping from.
  178. * @param int $w The width of the crop from the x and y coordinates.
  179. * @param int $h The height of the crop from the x and y coordinates.
  180. */
  181. public function setCrop($x, $y, $w, $h)
  182. {
  183. $this->image["targetx"] = $x;
  184. $this->image["targety"] = $y;
  185. $this->image["sizex"] = $w;
  186. $this->image["sizey"] = $h;
  187. }
  188. /**
  189. * Sets the JPEG output quality.
  190. *
  191. * @param int $quality The quality of the JPEG image.
  192. */
  193. public function setJpegQuality($quality=75)
  194. {
  195. //jpeg quality
  196. $this->image["quality"] = $quality;
  197. }
  198. /**
  199. * Shows the image to a browser. Sets the correct image format in a header.
  200. */
  201. public function show()
  202. {
  203. //show thumb
  204. header("Content-Type: image/".$this->image["format"]);
  205. $this->createResampledImage();
  206. if ( $this->image["format"]=="JPG" || $this->image["format"]=="JPEG" ) {
  207. //JPEG
  208. imageJPEG($this->image["des"], "", $this->image["quality"]);
  209. } elseif ( $this->image["format"] == "PNG" ) {
  210. //PNG
  211. imagePNG($this->image["des"]);
  212. } elseif ( $this->image["format"] == "GIF" ) {
  213. //GIF
  214. imageGIF($this->image["des"]);
  215. } elseif ( $this->image["format"] == "WBMP" ) {
  216. //WBMP
  217. imageWBMP($this->image["des"]);
  218. }
  219. }
  220. /**
  221. * Private method to run the imagecopyresampled() function with the parameters that have been set up.
  222. * This method is used by the save() and show() methods.
  223. */
  224. private function createResampledImage()
  225. {
  226. /* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/
  227. if ( isset($this->image["sizex_thumb"]) && isset($this->image["sizey_thumb"]) ) {
  228. $this->image["des"] = ImageCreateTrueColor($this->image["sizex_thumb"], $this->image["sizey_thumb"]);
  229. imagecopyresampled($this->image["des"], $this->image["src"], 0, 0, $this->image["targetx"], $this->image["targety"], $this->image["sizex_thumb"], $this->image["sizey_thumb"], $this->image["sizex"], $this->image["sizey"]);
  230. } else {
  231. $this->image["des"] = ImageCreateTrueColor($this->image["sizex"], $this->image["sizey"]);
  232. imagecopyresampled($this->image["des"], $this->image["src"], 0, 0, $this->image["targetx"], $this->image["targety"], $this->image["sizex"], $this->image["sizey"], $this->image["sizex"], $this->image["sizey"]);
  233. }
  234. }
  235. /**
  236. * Saves the image to a given filename, if no filename is given then a default is created.
  237. *
  238. * @param string $save The new image filename.
  239. */
  240. public function save($save="")
  241. {
  242. //save thumb
  243. if ( empty($save) ) {
  244. $save = strtolower("./thumb.".$this->image["format"]);
  245. }
  246. $this->createResampledImage();
  247. if ( $this->image["format"] == "JPG" || $this->image["format"] == "JPEG" ) {
  248. //JPEG
  249. imageJPEG($this->image["des"], $save, $this->image["quality"]);
  250. } elseif ( $this->image["format"] == "PNG" ) {
  251. //PNG
  252. imagePNG($this->image["des"], $save);
  253. } elseif ( $this->image["format"] == "GIF" ) {
  254. //GIF
  255. imageGIF($this->image["des"], $save);
  256. } elseif ( $this->image["format"] == "WBMP" ) {
  257. //WBMP
  258. imageWBMP($this->image["des"], $save);
  259. }
  260. }
  261. }