PageRenderTime 26ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/catalogo/myCore/clases/myFunciones.php

https://gitlab.com/fabian.morales/Mezanine
PHP | 157 lines | 112 code | 36 blank | 9 comment | 9 complexity | 26ce60e0044627118abd0f2546fdb8cb MD5 | raw file
  1. <?php
  2. /*
  3. Héctor Fabián Morales Ramírez
  4. Tecnólogo en Ingeniería de Sistemas
  5. Enero 2011
  6. */
  7. class myFunciones{
  8. public function generarToken($owner=""){
  9. $token = md5(mktime().uniqid());
  10. mySession::set("token".$owner, $token);
  11. }
  12. public function getToken($owner=""){
  13. $token = mySession::get("token".$owner);
  14. return $token;
  15. }
  16. public function borrarToken($owner=""){
  17. mySession::clear("token".$owner);
  18. }
  19. public function reprocesarHtml($html){
  20. $html = htmlspecialchars_decode($html, ENT_QUOTES);
  21. return $html;
  22. }
  23. public function procesarHtml($html){
  24. $html = htmlspecialchars($html, ENT_QUOTES);
  25. return $html;
  26. }
  27. public function descargarArchivo($path, $nombreArchivo){
  28. $len = filesize($path);
  29. header('Expires: 0');
  30. header('Cache-Control: must-revalidate');
  31. header("Content-Description: File Transfer");
  32. header('Content-Type: application/octet-stream');
  33. header("Content-Disposition: attachment; filename=".$nombreArchivo);
  34. header("Content-Transfer-Encoding: binary");
  35. header("Content-Length: ".$len);
  36. @set_time_limit(0);
  37. $fp = @fopen($path, "rb");
  38. $tamBuffer = 524288;
  39. ob_clean();
  40. flush();
  41. while($fp && !feof($fp)) {
  42. $buffer = fread($fp, $tamBuffer);
  43. print $buffer;
  44. flush();
  45. }
  46. fclose($fp);
  47. die();
  48. }
  49. public function redimensionarImg($archivo, $dirOrig, $dirDest, $ancho=80, $alto=80){
  50. $image = new Imagick($dirOrig.DS.$archivo);
  51. $image->cropThumbnailImage($ancho, $alto);
  52. $image->writeImage($dirDest.DS.$archivo);
  53. }
  54. public function crearThumb($fileName, $imageDir, $imageDest, $ancho = 80, $alto = 80){
  55. global $maxPixels;
  56. $maxPixels = 3840*3840; /*1920x1920*/
  57. $file = $imageDir.DS.$fileName;
  58. $fileDest = $imageDest.DS.$fileName;
  59. list($width, $height) = @getimagesize($file);
  60. $imagePixels = $width * $height;
  61. if($maxPixels < $imagePixels)
  62. return false;
  63. /*if ($width == $ancho && $height == $alto){
  64. return true;
  65. }*/
  66. $outputX = $ancho;
  67. $outputY = $alto;
  68. $quality = 100;
  69. $anchoResultado = $width;
  70. $altoResultado = $height;
  71. $anchoMax = $ancho;
  72. $altoMax = $alto;
  73. if($anchoResultado > $anchoMax){
  74. $anchoResultado = $anchoMax;
  75. $altoResultado = ($height/$width)*$anchoResultado;
  76. }
  77. if($altoResultado > $altoMax){
  78. $altoResultado = $altoMax;
  79. $anchoResultado = ($width/$height)*$altoResultado;
  80. }
  81. $outputX = $anchoResultado;
  82. $outputY = $altoResultado;
  83. $deltaX = 0;
  84. $deltaY = 0;
  85. $portionX = $width;
  86. $portionY = $height;
  87. $filePartes = pathinfo($file);
  88. $filePartes['extension'] = strtolower($filePartes['extension']);
  89. if($filePartes['extension']=="jpg"){
  90. $filePartes['extension']="jpeg";
  91. }
  92. $funcionCreateImageFrom = "imagecreatefrom".$filePartes['extension'];
  93. $funcionImage = "image".$filePartes['extension'];
  94. $imageSrc = @$funcionCreateImageFrom($file);
  95. try{
  96. if (!$imageDest = @imagecreatetruecolor($outputX, $outputY)){
  97. $imageDest = @imagecreate($outputX, $outputY);
  98. }
  99. }
  100. catch(Exception $e){
  101. $imageDest = @imagecreate($outputX, $outputY);
  102. }
  103. //if (@imagecopyresized($imageDest, $imageSrc, 0, 0, $deltaX, $deltaY, $outputX, $outputY, $portionX, $portionY)) {
  104. if (@imagecopyresampled($imageDest, $imageSrc, 0, 0, $deltaX, $deltaY, $outputX, $outputY, $portionX, $portionY)) {
  105. if($filePartes['extension']=="png"){
  106. $quality = 0;
  107. }
  108. @$funcionImage($imageDest, $fileDest, $quality);
  109. @imagedestroy($imageSrc);
  110. @imagedestroy($imageDest);
  111. return true;
  112. }
  113. return false;
  114. }
  115. function str_to_utf8($str) {
  116. if (mb_detect_encoding($str, 'UTF-8', true) === false) {
  117. $str = utf8_encode($str);
  118. }
  119. return $str;
  120. }
  121. }