PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/shop/libraries/Libupload.php

https://github.com/ekoisa/pyrocms-shop
PHP | 256 lines | 171 code | 36 blank | 49 comment | 49 complexity | ed55185f2f15bc1c48a6b84576f22f00 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Name: Libupload
  4. *
  5. * Author: Eko muhammad isa
  6. * ekoisa@gmail.com
  7. * @eko_isa
  8. *
  9. * Location: http://www.eNotes.web.id/
  10. *
  11. * Created: 30 September 2011
  12. *
  13. * Description: Alternative upload system PyroCMS
  14. *
  15. */
  16. class Libupload
  17. {
  18. /*
  19. *
  20. * $param = array(
  21. * 'file_post', // name file input
  22. * 'file_target', // list file target contains array
  23. * )
  24. *
  25. *
  26. * file_target = (array(
  27. * 'new_name', // name file input
  28. * 'path', // path upload file
  29. * 'width', // target width
  30. * 'height', // target height
  31. * ), array(
  32. * 'new_name',
  33. * 'path',
  34. * 'width',
  35. * 'height',
  36. * ));
  37. * */
  38. public function upload_img($param = array()){
  39. if(!isset($param)){
  40. return;
  41. }
  42. if(!isset($param['file_post'])){
  43. return;
  44. }
  45. $max_size = 1500;
  46. $file_post = $param['file_post'];
  47. $file_target = $param['file_target'];
  48. $file_result = array();
  49. $errors=0;
  50. $dbg_show = '';
  51. $file_result[0] = false;
  52. $file_result[1] = $dbg_show;
  53. if(isset($_FILES[$file_post])){
  54. $image =$_FILES[$file_post]['name'];
  55. $uploadedfile = $_FILES[$file_post]['tmp_name'];
  56. if ($image){
  57. $filename = stripslashes($_FILES[$file_post]['name']);
  58. $extension = $this->getExtension($filename);
  59. $extension = strtolower($extension);
  60. if (($extension != "jpg") && ($extension != "jpeg")
  61. && ($extension != "png") && ($extension != "gif"))
  62. {
  63. $dbg_show .= ' Unknown Image extension <br/>';
  64. $errors=1;
  65. }
  66. else
  67. {
  68. $size=filesize($_FILES[$file_post]['tmp_name']);
  69. if ($size > $max_size*1024)
  70. {
  71. $dbg_show .= 'You have exceeded the size limit <br/>';
  72. $errors=1;
  73. }
  74. if($extension=="jpg" || $extension=="jpeg" )
  75. {
  76. $uploadedfile = $_FILES[$file_post]['tmp_name'];
  77. $src = imagecreatefromjpeg($uploadedfile);
  78. }
  79. else if($extension=="png")
  80. {
  81. $uploadedfile = $_FILES[$file_post]['tmp_name'];
  82. $src = imagecreatefrompng($uploadedfile);
  83. }
  84. else
  85. {
  86. $src = imagecreatefromgif($uploadedfile);
  87. }
  88. list($width,$height)=getimagesize($uploadedfile);
  89. $startk = 2;
  90. foreach($file_target as $vl){
  91. $TargetWidth=(intval($vl['width']) > 0) ? intval($vl['width']): false;
  92. $TargetHeight=(intval($vl['height']) > 0) ? intval($vl['height']): false;
  93. if(!$TargetWidth and !$TargetHeight){
  94. $newwidth = $width;
  95. $newheight = $height;
  96. }else{
  97. if(!$TargetHeight and $TargetWidth > 0){
  98. // echo $TargetWidth . " |cek a| " . $width . "<br/>";
  99. if($TargetWidth < $width){
  100. $newwidth = $TargetWidth;
  101. $newheight = ($TargetWidth * $height) / $width;
  102. $newheight = ceil($newheight);
  103. //echo $newwidth . " |cek a rs| " . $newheight . "<br/>";
  104. }else{
  105. $newwidth = $width;
  106. $newheight = $height;
  107. //echo $newwidth . " |cek xxx rs| " . $newheight . "<br/>";
  108. }
  109. //echo $newwidth . " w v0s h " . $newheight . "<br/>";
  110. }elseif(!$TargetWidth and $TargetHeight > 0){
  111. //echo $TargetHeight . " |cek b| " . $height . "<br/>";
  112. if($TargetHeight < $height){
  113. $newheight = $TargetHeight;
  114. $newwidth = ($TargetHeight * $width) / $height;
  115. $newwidth = ceil($newwidth);
  116. }else{
  117. $newwidth = $width;
  118. $newheight = $height;
  119. }
  120. //echo $newwidth . " w v1s h " . $newheight . "<br/>";
  121. }else{
  122. //echo "cek cek cek<br/>";
  123. if($width < $height and $TargetHeight < $height){
  124. // echo $TargetHeight . " |cek c| " . $height . "<br/>";
  125. $newwidth = ($TargetHeight * $width) / $height;
  126. $newwidth = ceil($newwidth);
  127. $newheight = $TargetHeight;
  128. }elseif($width >= $height and $TargetWidth < $width){
  129. // echo $TargetWidth . " |cek d| " . $width . "<br/>";
  130. $newheight = ($TargetWidth * $height) / $width;
  131. $newheight = ceil($newheight);
  132. $newwidth = $TargetWidth;
  133. }else{
  134. $newwidth = $width;
  135. $newheight = $height;
  136. }
  137. }
  138. //echo $newwidth . " w v2s h " . $newheight . "<br/>";
  139. }
  140. $tmp=imagecreatetruecolor($newwidth,$newheight);
  141. imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight, $width,$height);
  142. $new_filename = $vl['new_name'].'.'.$extension;
  143. $filename = $vl['path']. $new_filename;
  144. $original_name = $_FILES[$file_post]['name'];
  145. $this->createDir($vl['path']);
  146. if($extension=="jpg" || $extension=="jpeg" )
  147. {
  148. imagejpeg($tmp,$filename,90);
  149. }
  150. else if($extension=="png")
  151. {
  152. imagepng($tmp,$filename);
  153. }
  154. else
  155. {
  156. imagegif($tmp,$filename);
  157. }
  158. imagedestroy($tmp);
  159. $file_result[$startk] = array('file_new_name'=>$new_filename, 'file_orig_name'=>$original_name);
  160. $startk++;
  161. }
  162. imagedestroy($src);
  163. }
  164. }
  165. }else{
  166. $file_result[0] = false;
  167. $dbg_show .= 'No File Uploaded.<br/>';
  168. }
  169. if(strlen(trim($dbg_show)) > 0){
  170. $file_result[0] = false;
  171. }else{
  172. $file_result[0] = true;
  173. }
  174. $file_result[1] = $dbg_show;
  175. return $file_result;
  176. }
  177. function getExtension($str) {
  178. $i = strrpos($str,".");
  179. if (!$i) { return ""; }
  180. $l = strlen($str) - $i;
  181. $ext = substr($str,$i+1,$l);
  182. return $ext;
  183. }
  184. function createDir($path) {
  185. if (!is_file($path) && !is_dir($path)) {
  186. mkdir($path); //create the directory
  187. chmod($path, 0777); //make it writable
  188. }
  189. }
  190. function RemoveFile($TheTarget){
  191. if(file_exists($TheTarget)){
  192. unlink($TheTarget);
  193. }
  194. }
  195. function UploadFile($TheFile, $TargetPath, $TargetName = ""){
  196. $path_parts = pathinfo($TheFile['name']);
  197. $extn = strtolower($path_parts['extension']);
  198. if(($extn != "rar") && ($extn != "zip") && ($extn != "doc") && ($extn != "xls") && ($extn != "docx") && ($extn != "xlsx") && ($extn != "pdf"))
  199. {
  200. $errors="<br/> Unknown File extension ";
  201. return "-1";
  202. }else{
  203. $size=filesize($TheFile['tmp_name']);
  204. if ($size > 4800*1024){
  205. //echo "You have exceeded the size limit";
  206. $errors="<br/> You have exceeded the size limit.";
  207. return "-1";
  208. }else{
  209. // if (file_exists("upload/" . $TheFile["name"])){
  210. // echo $TheFile["name"] . " already exists. ";
  211. // }else{
  212. $thefile = $TargetName . "." . $extn;
  213. $file_save = $TargetPath . $thefile; // $TheFile['name'];
  214. move_uploaded_file($TheFile["tmp_name"], $file_save);
  215. // }
  216. return $thefile;
  217. }
  218. }
  219. }
  220. }