PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/12cropimageinc.php

https://github.com/aravindc/pixelpost
PHP | 210 lines | 134 code | 33 blank | 43 comment | 27 complexity | e3cdacc0d4b70aa7233bb87574bc4ad2 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////////////////////////////
  3. ////////////////////////// //////////////////////////
  4. ////////////////////////// 1 2 CROP IMAGE //////////////////////////
  5. ////////////////////////// //////////////////////////
  6. ////////////////////////// (c) 2002 Roel Meurders //////////////////////////
  7. ////////////////////////// mail: scripts@roelmeurders.com //////////////////////////
  8. ////////////////////////// version 0.2 //////////////////////////
  9. ////////////////////////// //////////////////////////
  10. ////////////////////////////////////////////////////////////////////////////////////////////////////
  11. ///// CREDITS: Most Javascript is taken from DHTMLCentral.com and is made by Thomas Brattli. ///////
  12. ////////////////////////////////////////////////////////////////////////////////////////////////////
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////
  14. ///// Modified by: Ramin Mehran @ March 2005
  15. ///// Modifications: Modified substantially to make it
  16. ///// compatible with Pixelpost v1.4 (Pixelpost.org) and the original php
  17. ///// file has been broken into seperated parts.
  18. ///// Updated in release of Pixelpost 1.5 for PHP 4.4.1 and PHP 5.1.1 compatibility.
  19. /////
  20. ///// SVN file version:
  21. ///// $Id$
  22. /////
  23. ////////////////////////////////////////////////////////////////////////////////////////////////////
  24. // set the 12cropimage file path (in the include directory)
  25. // You do not need to access this if not logged in - securing it
  26. if(!isset($_SESSION["pixelpost_admin"]) || $cfgrow['password'] != $_SESSION["pixelpost_admin"] || isset($_GET["_SESSION"]["pixelpost_admin"]) AND $_GET["_SESSION"]["pixelpost_admin"] == $_SESSION["pixelpost_admin"] || isset($_POST["_SESSION"]["pixelpost_admin"]) AND $_POST["_SESSION"]["pixelpost_admin"] == $_SESSION["pixelpost_admin"] || isset($_COOKIE["_SESSION"]["pixelpost_admin"]) AND $_COOKIE["_SESSION"]["pixelpost_admin"] == $_SESSION["pixelpost_admin"])
  27. {
  28. die("Try another day!!");
  29. }
  30. $spacer = "../includes/spacer.gif"; //Relactive path tpo spacer.gif, transparent image
  31. $javafile = "../includes/12cropimage.js"; //Relative path to js-file
  32. // set global variables
  33. global $txt, $imgW,$imgH, $imgProp, $spacer, $redirect, $javafile, $crw, $crh, $org, $res;
  34. /* start up mysql */
  35. mysql_error();
  36. // if the user selected '12c' as the cropping tool
  37. if(isset($_GET['x']) AND $_GET['x'] == '12cropthumb')
  38. {
  39. $w = $_GET['sw'];
  40. $h = $_GET['sh'];
  41. $dw = $_GET['dw'];
  42. $dh = $_GET['dh'];
  43. $file = $_GET['filename'];
  44. if(createthumbnail_12crop($w,$h,$dw,$dh,$file))
  45. {
  46. header("Location: index.php?view=images");// end if 12cropthumb
  47. }
  48. else
  49. {
  50. echo 'Some error in 12crop: thumb is not created!';
  51. }
  52. }
  53. // get specified thumbnail width and height
  54. $cfgquery = mysql_query("SELECT * FROM `".$pixelpost_db_prefix."config`");
  55. $cfgrow = mysql_fetch_array($cfgquery,MYSQL_ASSOC);
  56. $max_width = $cfgrow['thumbwidth'];
  57. $max_height = $cfgrow['thumbheight'];
  58. // renaming the variable
  59. $crw = $max_width;
  60. $crh = $max_height;
  61. // initialize the variables with some values
  62. $imgH = 22;
  63. $imgW = 22;
  64. $imgProp = .4;
  65. // save required messages
  66. $txt['cropimage'] = "crop";
  67. $txt['preview'] = "preview";
  68. $txt['bigger'] = "+";
  69. $txt['smaller'] = "-";
  70. $txt['closewindow'] = "close window";
  71. $txt['selectioninpicture'] = "The selection has to be completely on the image.";
  72. /////////////////////////
  73. // Functions
  74. // prepare the cropdiv
  75. function setsize_cropdiv($file)
  76. {
  77. global $pixelpost_db_prefix, $crw, $crh, $spacer;
  78. $cfgquery = mysql_query("SELECT * FROM `".$pixelpost_db_prefix."config`");
  79. $cfgrow = mysql_fetch_array($cfgquery,MYSQL_ASSOC);
  80. // credit to codewalkers.com - there is 90% a tutorial there
  81. $max_width = $cfgrow['thumbwidth'];
  82. $max_height = $cfgrow['thumbheight'];
  83. define('IMAGE_BASE', rtrim($cfgrow['imagepath'],"/"));
  84. $image_path = IMAGE_BASE."/".$file;
  85. $img = null;
  86. $ext_exp = explode('.', $image_path);
  87. $ext_end = end($ext_exp);
  88. $ext = strtolower($ext_end);
  89. if ($ext == 'jpg' || $ext == 'jpeg')
  90. {
  91. $img = @imagecreatefromjpeg($image_path);
  92. }
  93. elseif($ext == 'png')
  94. {
  95. $img = @imagecreatefrompng($image_path);
  96. }
  97. elseif($ext == 'gif')
  98. {
  99. $img = @imagecreatefromgif($image_path);
  100. }
  101. if($img)
  102. {
  103. $imgW = imagesx($img);
  104. $imgH = imagesy($img);
  105. $divsize = $imgH+50;
  106. $imgProp = .8* min($imgW/$max_width, $imgH/$max_height);
  107. $imgProp1 = $crw*$imgProp;
  108. $imgProp2 = $crh*$imgProp;
  109. echo "
  110. <style type=\"text/css\">
  111. #cropdiv { position:absolute; width:".$imgProp1."px; height:".$imgProp2."px; z-index:2; background-image:url(".$spacer."); }
  112. #editthumbnail{ position:relative; height:".$divsize."px; visibility: hidden; }
  113. </style>";
  114. imagedestroy($img);
  115. return true;
  116. }
  117. else
  118. {
  119. imagedestroy($img);
  120. return false;
  121. }
  122. }
  123. // special createthumbnail which uses 12cropimage
  124. function createthumbnail_12crop($w,$h,$dw,$dh,$file)
  125. {
  126. global $pixelpost_db_prefix;
  127. $cfgquery = mysql_query("SELECT * FROM `".$pixelpost_db_prefix."config`");
  128. $cfgrow = mysql_fetch_array($cfgquery,MYSQL_ASSOC);
  129. // credit to codewalkers.com - there is 90% a tutorial there
  130. $max_width = $cfgrow['thumbwidth'];
  131. $max_height = $cfgrow['thumbheight'];
  132. $tmp_img = imagecreatetruecolor($max_width,$max_height);
  133. define('IMAGE_BASE', rtrim($cfgrow['imagepath'],"/"));
  134. $image_path = IMAGE_BASE."/".$file;
  135. $img = null;
  136. $ext_exp = explode('.', $image_path);
  137. $ext_end = end($ext_exp);
  138. $ext = strtolower($ext_end);
  139. if ($ext == 'jpg' || $ext == 'jpeg')
  140. {
  141. $img = @imagecreatefromjpeg($image_path);
  142. }
  143. elseif($ext == 'png')
  144. {
  145. $img = @imagecreatefrompng($image_path);
  146. }
  147. elseif($ext == 'gif')
  148. {
  149. $img = @imagecreatefromgif($image_path);
  150. }
  151. if($img)
  152. {
  153. $width = imagesx($img);
  154. $height = imagesy($img);
  155. // gd 2.0.1 or later: imagecopyresampled
  156. // gd less than 2.0: imagecopyresized
  157. if(function_exists('imagecopyresampled'))
  158. {
  159. imagecopyresampled($tmp_img, $img, 0,0,$w,$h,$max_width,$max_height,$dw,$dh);
  160. }
  161. else
  162. {
  163. imagecopyresized($tmp_img, $img, 0,0,$w,$h,$max_width ,$max_height,$dw,$dh);
  164. }
  165. imagedestroy($img);
  166. // now save the thumbnail
  167. touch($cfgrow['thumbnailpath']."thumb_$file");
  168. imagejpeg($tmp_img, $cfgrow['thumbnailpath']."thumb_$file",$cfgrow['compression']);
  169. $thumbimage = $cfgrow['thumbnailpath']."thumb_$file";
  170. chmod($thumbimage,0644);
  171. imagedestroy($tmp_img);
  172. return true;
  173. }// if $img
  174. // if you are here, something's wrong
  175. return false;
  176. }
  177. ?>