PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/include/class.image.php

https://gitlab.com/vectorci/Collabtive
PHP | 338 lines | 240 code | 52 blank | 46 comment | 32 complexity | fdbb755420eb1826790bcf7b02c6f77f MD5 | raw file
  1. <?PHP
  2. #require("hft_image_errors.php"); // here you can include your own language error file
  3. //this class works with image
  4. class hft_image {
  5. var $image_original;
  6. var $file_original;
  7. var $image_original_width;
  8. var $image_original_height;
  9. var $image_original_type_code;
  10. var $image_original_type_abbr;
  11. var $image_original_html_sizes;
  12. var $image_resized;
  13. var $file_resized;
  14. var $image_resized_width;
  15. var $image_resized_height;
  16. var $image_resized_type_code;
  17. var $image_resized_type_abbr;
  18. var $image_resized_html_sizes;
  19. //some settings
  20. var $jpeg_quality;
  21. var $use_gd2;
  22. function hft_image($file_original){
  23. //constructor of the class
  24. //it takes given file and creates image out of it
  25. global $ERR;
  26. $this->clear(); // clear all.
  27. if(file_exists($file_original)) {
  28. $this->file_original = $file_original;
  29. $this->image_original = $this->imagecreatefromfile($file_original);
  30. if(!$this->image_original){
  31. $this->error($ERR["IMAGE_NOT_CREATED_FROM_FILE"]." file=$file_original");
  32. return false;
  33. }
  34. } else {
  35. $this->error($ERR["FILE_DOESNOT_EXSIT"]." file=$file_original");
  36. }
  37. }
  38. function clear() {
  39. // clear all the class member varaibles
  40. $this->image_original = 0;
  41. $this->file_original = "";
  42. $this->image_original_width = 0;
  43. $this->image_original_height = 0;
  44. $this->image_original_type_code = 0;
  45. $this->image_original_type_abbr = "";
  46. $this->image_original_html_sizes= "";
  47. $this->image_resized = 0;
  48. $this->file_resized = "";
  49. $this->image_resized_width = 0;
  50. $this->image_resized_height = 0;
  51. $this->image_resized_type_code = -1;
  52. $this->image_resized_type_abbr = "";
  53. $this->image_resized_html_sizes = "";
  54. $this->set_parameters();
  55. }
  56. function set_parameters($jpeg_quality="55", $use_gd2=true) {
  57. $this->jpeg_quality=$jpeg_quality;
  58. $this->use_gd2=$use_gd2;
  59. }
  60. function error($msg){
  61. //error messages and debug info:
  62. // here you can implement your own error handling
  63. echo("<hr color='red'><font color='red'><b>$msg</b></font><br> file=<b>".__FILE__."</b><hr color='red'>");
  64. }
  65. function imagecreatefromfile($img_file){
  66. global $ERR;
  67. $img=0;
  68. $img_sz = getimagesize( $img_file ); ## returns array with some properties like dimensions and type;
  69. ####### Now create original image from uploaded file. Be carefull! GIF is often not supported, as far as I remember from GD 1.6
  70. switch( $img_sz[2] ){
  71. case 1:
  72. $img = $this->_imagecheckandcreate("ImageCreateFromGif", $img_file);
  73. $img_type = "GIF";
  74. break;
  75. case 2:
  76. $img = $this->_imagecheckandcreate("ImageCreateFromJpeg", $img_file);
  77. $img_type = "JPG";
  78. break;
  79. case 3:
  80. $img = $this->_imagecheckandcreate("ImageCreateFromPng", $img_file);
  81. $img_type = "PNG";
  82. break;
  83. // would be nice if this function will be finally supported
  84. case 4:
  85. $img = $this->_imagecheckandcreate("ImageCreateFromSwf", $img_file);
  86. $img_type = "SWF";
  87. break;
  88. default:
  89. $img = 0;
  90. $img_type = "UNKNOWN";
  91. $this->error($ERR["IMG_NOT_SUPPORTED"]." $img_file");
  92. break;
  93. }//case
  94. if($img){
  95. $this->image_original_width=$img_sz[0];
  96. $this->image_original_height=$img_sz[1];
  97. $this->image_original_type_code=$img_sz[2];
  98. $this->image_original_type_abbr=$img_type;
  99. $this->image_original_html_sizes=$img_sz[3];
  100. }else {
  101. $this->clear();
  102. }
  103. return $img;
  104. }
  105. function _imagecheckandcreate($function, $img_file) {
  106. //inner function used from imagecreatefromfile().
  107. //Checks if the function exists and returns
  108. //created image or false
  109. global $ERR;
  110. if(function_exists($function)) {
  111. $img = $function($img_file);
  112. }else{
  113. $img = false;
  114. $this->error($ERR["FUNCTION_DOESNOT_EXIST"]." ".$function);
  115. }
  116. return $img;
  117. }
  118. function resize($desired_width, $desired_height, $mode="-"){
  119. //this is core function--it resizes created image
  120. //if any of parameters == "*" then no resizing on this parameter
  121. //>> mode = "+" then image is resized to cover the region specified by desired_width, _height
  122. //>> mode = "-" then image is resized to fit into the region specified by desired_width, _height
  123. // width-to-height ratio is all the time the same
  124. //>>mode=0 then image will be exactly resized to $desired_width _height.
  125. //geometrical distortion can occur in this case.
  126. // say u have picture 400x300 and there is circle on the picture
  127. //now u resized in mode=0 to 800x300 -- circle shape will be distorted and will look like ellipse.
  128. //GD2 provides much better quality but is not everywhere installed
  129. global $ERR;
  130. if($desired_width == "*" && $desired_height == "*"){
  131. $this->image_resized = $this->image_original;
  132. Return true;
  133. }
  134. switch($mode) {
  135. case "-":
  136. case '+':
  137. //multipliers
  138. if($desired_width != "*") $mult_x = $desired_width / $this->image_original_width;
  139. if($desired_height != "*") $mult_y = $desired_height / $this->image_original_height;
  140. $ratio = $this->image_original_width / $this->image_original_height;
  141. if($desired_width == "*"){
  142. $new_height = $desired_height;
  143. $new_width = $ratio * $desired_height;
  144. }elseif($desired_height == "*"){
  145. $new_height = $desired_width / $ratio;
  146. $new_width = $desired_width;
  147. }else{
  148. if($mode=="-"){
  149. if( $this->image_original_height * $mult_x < $desired_height ){
  150. //image must be smaller than given $desired_ region
  151. //test which multiplier gives us best result
  152. //$mult_x does the job
  153. $new_width = $desired_width;
  154. $new_height = $this->image_original_height * $mult_x;
  155. }else{
  156. //$mult_y does the job
  157. $new_width = $this->image_original_width * $mult_y;
  158. $new_height = $desired_height;
  159. }
  160. }else{
  161. //mode == "+"
  162. // cover the region
  163. //image must be bigger than given $desired_ region
  164. //test which multiplier gives us best result
  165. if( $this->image_original_height * $mult_x > $desired_height ){
  166. //$mult_x does the job
  167. $new_width = $desired_width;
  168. $new_height = $this->image_original_height * $mult_x;
  169. }else{
  170. //$mult_y does the job
  171. $new_width = $this->image_original_width * $mult_y;
  172. $new_height = $desired_height;
  173. }
  174. }
  175. }
  176. break;
  177. case '0':
  178. //fit the region exactly.
  179. if($desired_width == "*") $desired_width = $this->image_original_width;
  180. if($desired_height == "*") $desired_height = $this->image_original_height;
  181. $new_width = $desired_width;
  182. $new_height = $desired_height;
  183. break;
  184. default:
  185. $this->error($ERR["UNKNOWN_RESIZE_MODE"]." $mode");
  186. break;
  187. }
  188. // OK here we have $new_width _height
  189. //create destination image checking for GD2 functions:
  190. if( $this->use_gd2 ){
  191. if( function_exists("imagecreatetruecolor")){
  192. $this->image_resized = imagecreatetruecolor($new_width, $new_height) or $this->error($ERR["GD2_NOT_CREATED"]);
  193. }else {
  194. $this->error($ERR["GD2_UNAVALABLE"]." ImageCreateTruecolor()");
  195. }
  196. } else {
  197. $this->image_resized = imagecreate($new_width, $new_height) or $this->error($ERR["IMG_NOT_CREATED"]);
  198. }
  199. //Resize
  200. if( $this->use_gd2 ){
  201. if( function_exists("imagecopyresampled")){
  202. $res = imagecopyresampled($this->image_resized,
  203. $this->image_original,
  204. 0, 0, //dest coord
  205. 0, 0, //source coord
  206. $new_width, $new_height, //dest sizes
  207. $this->image_original_width, $this->image_original_height // src sizes
  208. ) or $this->error($ERR["GD2_NOT_RESIZED"]);
  209. }else {
  210. $this->error($ERR["GD2_UNAVALABLE"]." ImageCopyResampled()");
  211. }
  212. } else {
  213. $res = imagecopyresized($this->image_resized,
  214. $this->image_original,
  215. 0, 0, //dest coord
  216. 0, 0, //source coord
  217. $new_width, $new_height, //dest sizes
  218. $this->image_original_width, $this->image_original_height // src sizes
  219. ) or $this->error($ERR["IMG_NOT_RESIZED"]);
  220. }
  221. }
  222. function output_original($destination_file, $image_type="JPG") {
  223. //outputs original image
  224. //if destination file is empty image will be output to browser
  225. // right now $image_type can be JPG or PNG
  226. return _output_image($destination_file, $image_type, $this->image_original);
  227. }
  228. function output_resized($destination_file, $image_type="JPG") {
  229. //if destination file is empty image will be output to browser
  230. // right now $image_type can be JPG or PNG
  231. $res = $this->_output_image($destination_file, $image_type, $this->image_resized);
  232. if(trim($destination_file)){
  233. $sz=getimagesize($destination_file);
  234. $this->file_resized = $destination_file;
  235. $this->image_resized_width = $sz[0];
  236. $this->image_resized_height = $sz[1];
  237. $this->image_resized_type_code=$sz[2];
  238. $this->image_resized_html_sizes=$sz[3];
  239. //only jpeg and png are really supported, but I'd like to think of future
  240. switch($this->image_resized_html_sizes){
  241. case 0:
  242. $this->image_resized_type_abbr = "GIF";
  243. break;
  244. case 1:
  245. $this->image_resized_type_abbr = "JPG";
  246. break;
  247. case 2:
  248. $this->image_resized_type_abbr = "PNG";
  249. break;
  250. case 3:
  251. $this->image_resized_type_abbr = "SWF";
  252. break;
  253. default:
  254. $this->image_resized_type_abbr = "UNKNOWN";
  255. break;
  256. }
  257. }
  258. return $res;
  259. }
  260. function _output_image($destination_file, $image_type, $image){
  261. //if destination file is empty image will be output to browser
  262. // right now $image_type can be JPEG or PNG
  263. global $ERR;
  264. $destination_file = trim($destination_file);
  265. $res = false;
  266. if($image){
  267. switch($image_type) {
  268. case 'JPEG':
  269. case 'JPG':
  270. $res = ImageJpeg($image, $destination_file, $this->jpeg_quality);
  271. break;
  272. case 'PNG':
  273. $res = Imagepng($image, $destination_file);
  274. break;
  275. default:
  276. $this->error($ERR["UNKNOWN_OUTPUT_FORMAT"]." $image_type");
  277. break;
  278. }
  279. }else{
  280. $this->error($ERR["NO_IMAGE_FOR_OUTPUT"]);
  281. }
  282. if(!$res) $this->error($ERR["UNABLE_TO_OUTPUT"]." $destination_file");
  283. return $res;
  284. }
  285. }
  286. ?>