PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_tuiyo/libraries/tuiyo/filesystem/image.php

https://github.com/iduknow/ignite
PHP | 188 lines | 125 code | 17 blank | 46 comment | 23 complexity | b556bee429725cc40b9983018f6eb649 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * ******************************************************************
  4. * Image object for the Tuiyo platform *
  5. * ******************************************************************
  6. * @copyright : 2008 tuiyo Platform *
  7. * @license : http://platform.tuiyo.com/license BSD License *
  8. * @version : Release: $Id$ *
  9. * @link : http://platform.tuiyo.com/ *
  10. * @author : livingstone[at]drstonyhills[dot]com *
  11. * @access : Public *
  12. * @since : 1.0.0 alpha *
  13. * @package : tuiyo *
  14. * ******************************************************************
  15. */
  16. defined('TUIYO_EXECUTE') || die;
  17. jimport('joomla.filesystem.file');
  18. jimport('joomla.filesystem.folder');
  19. jimport('joomla.filesystem.path');
  20. /**
  21. * TuiyoImages
  22. * @package tuiyo
  23. * @author Livingstone Fultang
  24. * @copyright 2009
  25. * @version $Id$
  26. * @access public
  27. */
  28. class TuiyoImage
  29. {
  30. /**
  31. * TuiyoImage::TuiyoImage()
  32. * @param mixed $imgSrc
  33. * @param mixed $imgWidth
  34. * @param mixed $imgHieght
  35. * @param integer $Quality
  36. * @return void
  37. */
  38. public function TuiyoImage($imgSrc, $imgWidth, $imgHieght, $imgQuality = 85)
  39. {
  40. $imgType = JFile::getExt(basename($imgSrc));
  41. $imgErr = TUIYO_FILES . DS . 'noimage.jpg';
  42. //Image Parameters
  43. list($width, $height) = getimagesize($imgSrc);
  44. if ($width > $imgWidth || $height > $imgHieght) {
  45. if ((int)$imgWidth <> (int)$imgHieght) {
  46. $ratio = ($width > $height) ? $imgWidth / $width : $imgHieght / $height;
  47. $newWidth = $width * $ratio;
  48. $newHeight = $height * $ratio;
  49. } else {
  50. $tWidth = $imgWidth;
  51. $tHeight = $imgHeight;
  52. $imgdata = getimagesize($imgSrc);
  53. $widthOrig = $imgdata[0];
  54. $heightOrig = $imgdata[1];
  55. //Make Square
  56. if ($widthOrig < $heightOrig) {
  57. $newHeight = ($tWidth / $widthOrig) * $heightOrig;
  58. } else {
  59. $newWidth = ($tHeight / $heightOrig) * $widthOrig;
  60. }
  61. //Adjust width
  62. if ($newWidth < $tWidth) {
  63. $newWidth = $tWidth;
  64. $newHeight = ($tWidth / $widthOrig) * $heightOrig;
  65. }
  66. if ($newHeight < $tHeight) {
  67. $newHeight = $tHeight;
  68. $newWidth = ($tHeight / $heightOrig) * $widthOrig;
  69. }
  70. //$imgSrc = $imageR1;
  71. }
  72. } else {
  73. list($width, $height) = getimagesize($imgSrc);
  74. $ratio = ($width > $height) ? $imgWidth / $width : $imgHieght / $height;
  75. $newWidth = $imgWidth;
  76. $newHeight = ($imgWidth / $width) * $height;
  77. }
  78. //Type based
  79. switch ($imgType):
  80. //PNG
  81. case "png":
  82. header('Content-type: image/png');
  83. $imageX = imagecreatetruecolor($newWidth, $newHeight);
  84. $image = imagecreatefrompng($imgSrc);
  85. if ($width > $height) {
  86. $w1 = ($width - $height) / 2;
  87. $w1 = ceil($w1);
  88. $h1 = 0;
  89. } else
  90. if ($height > $width) {
  91. $w1 = 0;
  92. $h1 = ($height - $width) / 2;
  93. $h1 = ceil($h1);
  94. }
  95. imagecopyresampled($imageX, $image, 0, 0, $w1, $h1, $newWidth, $newHeight, $width,
  96. $height);
  97. imagepng($imageX, null, $imgQuality);
  98. imagedestroy($imageX);
  99. break;
  100. //JPEG
  101. case "jpg":
  102. case "jpeg":
  103. header('Content-type: image/jpeg');
  104. $image = imagecreatefromjpeg($imgSrc);
  105. $imageX = imagecreatetruecolor($imgWidth, $imgHieght);
  106. if ($width > $height) {
  107. $w1 = ($width - $height) / 2;
  108. $w1 = ceil($w1);
  109. $h1 = 0;
  110. } else
  111. if ($height > $width) {
  112. $w1 = 0;
  113. $h1 = ($height - $width) / 2;
  114. $h1 = ceil($h1);
  115. }
  116. //exit(0);
  117. imagecopyresampled($imageX, $image, 0, 0, $w1, $h1, $imgWidth, $imgHieght, $imgWidth,
  118. $imgHieght);
  119. imagejpeg($imageX, null, $imgQuality);
  120. imagedestroy($imageX);
  121. break;
  122. //GIF
  123. case "gif":
  124. header('Content-type: image/gif');
  125. $imageX = imagecreatetruecolor($newWidth, $newHeight);
  126. $image = imagecreatefromgif($imgSrc);
  127. if ($width > $height) {
  128. $w1 = ($width - $height) / 2;
  129. $w1 = ceil($w1);
  130. $h1 = 0;
  131. } else
  132. if ($height > $width) {
  133. $w1 = 0;
  134. $h1 = ($height - $width) / 2;
  135. $h1 = ceil($h1);
  136. }
  137. imagecopyresampled($imageX, $image, 0, 0, $w1, $h1, $newWidth, $newHeight, $width,
  138. $height);
  139. imagegif($imageX, null, $imgQuality);
  140. imagedestroy($imageX);
  141. break;
  142. endswitch;
  143. exit(0);
  144. }
  145. /**
  146. * TuiyoImage::getInstance()
  147. * @param bool $ifNotExist
  148. * @param array $params
  149. * @return
  150. */
  151. public function getInstance($params = array(), $ifNotExist = true)
  152. {
  153. /** Creates new instance if none already exists ***/
  154. static $instance = array();
  155. if (isset($instance[$params["s"]]) && !empty($instance[$params["s"]]) && $ifNotExist) {
  156. if (is_object($instance)) {
  157. return $instance[$params["s"]];
  158. } else {
  159. unset($instance[$params["s"]]);
  160. TuiyoImage::getInstance($ifNotExist, $params);
  161. }
  162. } else {
  163. $args = array("source" => (isset($params["s"])) ? $params["s"] : "", "width" =>
  164. (isset($params["h"])) ? $params["h"] : "", "height" => (isset($params["w"])) ? $params["w"] :
  165. "", "quality" => (isset($params["q"])) ? $params["q"] : 85, );
  166. $instance[$params["s"]] = new TuiyoImage($args["source"], $args["width"], $args["height"],
  167. $args["quality"]);
  168. }
  169. return $instance[$params["s"]];
  170. }
  171. }