PageRenderTime 65ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/public/shop/core/lib/image.php

https://bitbucket.org/rusbal/club4causes
PHP | 220 lines | 164 code | 38 blank | 18 comment | 38 complexity | 505713804f3929b10a2f215823de94d7 MD5 | raw file
  1. <?php
  2. /*------------------------------------------------------------------------------
  3. $Id$
  4. Shopping Cart
  5. http://dev.club4causes.com/shop
  6. Club4Causes v1.0
  7. This source file is subject to Open Software License (OSL 3.0)
  8. License details is bundled with this package in the file LICENSE.txt.
  9. It is also available at this URL:
  10. <http://www.opensource.org/licenses/OSL-3.0>
  11. UPGRADE NOTE:
  12. Do not edit or add to this file if you wish to upgrade Shopping Cart to newer
  13. versions in the future. If you wish to customize Shopping Cart for your
  14. needs please refer to http://dev.club4causes.com/shop for more information.
  15. ------------------------------------------------------------------------------*/
  16. if (! defined ( 'DIR_CORE' )) {
  17. header ( 'Location: static_pages/' );
  18. }
  19. final class AImage {
  20. private $file;
  21. private $image;
  22. private $info;
  23. public function __construct($file) {
  24. if (file_exists($file)) {
  25. $this->file = $file;
  26. $info = getimagesize($file);
  27. if(!$info){
  28. $this->file = null;
  29. return false;
  30. }
  31. $this->info = array(
  32. 'width' => $info[0],
  33. 'height' => $info[1],
  34. 'bits' => $info['bits'],
  35. 'mime' => $info['mime']
  36. );
  37. $this->image = $this->create($file);
  38. } else {
  39. throw new AException(AC_ERR_LOAD, 'Error: Could not load image ' . $file . '!');
  40. }
  41. }
  42. private function create($image) {
  43. $mime = $this->info['mime'];
  44. if ($mime == 'image/gif') {
  45. return imagecreatefromgif($image);
  46. } elseif ($mime == 'image/png') {
  47. return imagecreatefrompng($image);
  48. } elseif ($mime == 'image/jpeg') {
  49. return imagecreatefromjpeg($image);
  50. }
  51. }
  52. public function save($file, $quality = 100) {
  53. $extension = pathinfo($file,PATHINFO_EXTENSION);
  54. if ($extension == 'jpeg' || $extension == 'jpg') {
  55. imagejpeg($this->image, $file, $quality);
  56. } elseif($extension == 'png') {
  57. imagepng($this->image, $file, 0);
  58. } elseif($extension == 'gif') {
  59. imagegif($this->image, $file);
  60. }
  61. chmod($file, 0777);
  62. imagedestroy($this->image);
  63. }
  64. public function resize($width = 0, $height = 0, $nofill=false) {
  65. if (!$this->info['width'] || !$this->info['height']) {
  66. return;
  67. }
  68. if ($width == 0 && $height == 0) {
  69. return;
  70. }
  71. $scale = min($width / $this->info['width'], $height / $this->info['height']);
  72. if ($scale == 1 && $this->info['mime'] != 'image/png') {
  73. return;
  74. }
  75. $new_width = (int)round($this->info['width'] * $scale,0);
  76. $new_height = (int)round($this->info['height'] * $scale,0);
  77. $xpos = (int)(($width - $new_width) / 2);
  78. $ypos = (int)(($height - $new_height) / 2);
  79. $image_old = $this->image;
  80. $this->image = imagecreatetruecolor($width, $height);
  81. if (isset($this->info['mime']) && $this->info['mime'] == 'image/png') {
  82. imagealphablending($this->image, false);
  83. imagesavealpha($this->image, true);
  84. $background = imagecolorallocatealpha($this->image, 255, 255, 255, 127);
  85. imagefill($this->image, 0,0,$background);
  86. } else {
  87. if(!$nofill){ // if image no transparant
  88. $background = imagecolorallocate($this->image, 255, 255, 255);
  89. imagefilledrectangle($this->image, 0, 0, $width, $height, $background);
  90. }
  91. }
  92. if(is_resource($this->image)){
  93. imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->info['width'], $this->info['height']);
  94. }
  95. if(is_resource($image_old)){
  96. imagedestroy($image_old);
  97. }
  98. $this->info['width'] = $width;
  99. $this->info['height'] = $height;
  100. return true;
  101. }
  102. public function watermark($file, $position = 'bottomright') {
  103. $watermark = $this->create($file);
  104. $watermark_width = imagesx($watermark);
  105. $watermark_height = imagesy($watermark);
  106. switch($position) {
  107. case 'topleft':
  108. $watermark_pos_x = 0;
  109. $watermark_pos_y = 0;
  110. break;
  111. case 'topright':
  112. $watermark_pos_x = $this->info['width'] - $watermark_width;
  113. $watermark_pos_y = 0;
  114. break;
  115. case 'bottomleft':
  116. $watermark_pos_x = 0;
  117. $watermark_pos_y = $this->info['height'] - $watermark_height;
  118. break;
  119. case 'bottomright':
  120. $watermark_pos_x = $this->info['width'] - $watermark_width;
  121. $watermark_pos_y = $this->info['height'] - $watermark_height;
  122. break;
  123. }
  124. imagecopy($this->image, $watermark, $watermark_pos_x, $watermark_pos_y, 0, 0, 120, 40);
  125. imagedestroy($watermark);
  126. }
  127. public function crop($top_x, $top_y, $bottom_x, $bottom_y) {
  128. $image_old = $this->image;
  129. $this->image = imagecreatetruecolor($bottom_x - $top_x, $bottom_y - $top_y);
  130. imagecopy($this->image, $image_old, 0, 0, $top_x, $top_y, $this->info['width'], $this->info['height']);
  131. imagedestroy($image_old);
  132. $this->info['width'] = $bottom_x - $top_x;
  133. $this->info['height'] = $bottom_y - $top_y;
  134. }
  135. public function rotate($degree, $color = 'FFFFFF') {
  136. $rgb = $this->html2rgb($color);
  137. $this->image = imagerotate($this->image, $degree, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]));
  138. $this->info['width'] = imagesx($this->image);
  139. $this->info['height'] = imagesy($this->image);
  140. }
  141. private function filter($filter) {
  142. imagefilter($this->image, $filter);
  143. }
  144. private function text($text, $x = 0, $y = 0, $size = 5, $color = '000000') {
  145. $rgb = $this->html2rgb($color);
  146. imagestring($this->image, $size, $x, $y, $text, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]));
  147. }
  148. private function merge($file, $x = 0, $y = 0, $opacity = 100) {
  149. $merge = $this->create($file);
  150. $merge_width = imagesx($merge);
  151. $merge_height = imagesy($merge);
  152. imagecopymerge($this->image, $merge, $x, $y, 0, 0, $merge_width, $merge_height, $opacity);
  153. }
  154. private function html2rgb($color) {
  155. if ($color[0] == '#') {
  156. $color = substr($color, 1);
  157. }
  158. if (strlen($color) == 6) {
  159. list($r, $g, $b) = array($color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5]);
  160. } elseif (strlen($color) == 3) {
  161. list($r, $g, $b) = array($color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]);
  162. } else {
  163. return FALSE;
  164. }
  165. $r = hexdec($r);
  166. $g = hexdec($g);
  167. $b = hexdec($b);
  168. return array($r, $g, $b);
  169. }
  170. public function __destruct() {
  171. if (is_resource($this->image)){
  172. imagedestroy($this->image);
  173. }else{
  174. $this->image = null;
  175. }
  176. }
  177. }
  178. ?>