PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/blog/wp-content/themes/elegant-grunge/Image.class.php

https://github.com/chuckmitchell/Squall.ca
PHP | 253 lines | 187 code | 36 blank | 30 comment | 39 complexity | e7d498a23ca97a465cb3de8c29d8e7ec MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Image manipulation class
  4. *
  5. * Copyright (C) 2005, Mike Tyson <mike@tzidesign.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. *
  21. * $Id$
  22. */
  23. class Image
  24. {
  25. var $resource;
  26. var $orig_path;
  27. var $metadata;
  28. function &Load($path)
  29. {
  30. if ( !function_exists("imagecreatefromjpeg") )
  31. {
  32. //DEBUG("GD not installed.");
  33. $ret = null;
  34. return $ret;
  35. }
  36. $image = new Image;
  37. $image->orig_path = $path;
  38. // Open image
  39. if( preg_match("/.jpe?g$/i", $path))
  40. {
  41. $image->resource = @imagecreatefromjpeg($path);
  42. if ( !$image->resource )
  43. {
  44. // Try to use imagemagick to convert to PNG, then open the PNG
  45. exec("convert '".mysql_escape_string($path)."' /tmp/'".mysql_escape_string(basename($path))."'.png");
  46. if ( file_exists("/tmp/".basename($path).".png") )
  47. {
  48. $image->resource = @imagecreatefrompng("/tmp/".basename($path).".png");
  49. @unlink("/tmp/".basename($path).".png");
  50. if ( !$image->resource) return null;
  51. }
  52. else
  53. {
  54. return null;
  55. }
  56. }
  57. }
  58. else if ( preg_match("/.gif$/i", $path))
  59. {
  60. $image->resource = imagecreatefromgif($path);
  61. }
  62. else if( preg_match("/.png$/i", $path))
  63. {
  64. $image->resource = imagecreatefrompng($path);
  65. }
  66. else
  67. {
  68. //DEBUG("Cannot open image $imagepath: File type not supported");
  69. return false;
  70. }
  71. if ( !$image->resource )
  72. {
  73. //DEBUG("Cannot open image $imagepath: Invalid file");
  74. return false;
  75. }
  76. return $image;
  77. }
  78. function Release()
  79. {
  80. if ( $this->resource ) imagedestroy($this->resource);
  81. }
  82. function Width()
  83. {
  84. return imagesx($this->resource);
  85. }
  86. function Height()
  87. {
  88. return imagesy($this->resource);
  89. }
  90. function Scale($width, $height, $crop=false, $crop_width=-1, $crop_height=-1, $crop_x=-1, $crop_y=-1)
  91. {
  92. // Read original image dimensions
  93. $orig_w=imagesx($this->resource);
  94. $orig_h=imagesy($this->resource);
  95. if ( !$crop )
  96. {
  97. // If not cropping, then scale proportionately to within height/width
  98. if ($orig_w>$width || $orig_h>$height)
  99. {
  100. $img_w=$width;
  101. $img_h=$height;
  102. if ($img_w/$orig_w*$orig_h>$img_h)
  103. $img_w=round($img_h*$orig_w/$orig_h);
  104. else
  105. $img_h=round($img_w*$orig_h/$orig_w);
  106. }
  107. else
  108. {
  109. $img_w=$orig_w;
  110. $img_h=$orig_h;
  111. }
  112. // Create scaled image
  113. $img=imagecreatetruecolor($img_w,$img_h);
  114. if ( !$img )
  115. return false;
  116. imagecopyresampled($img,$this->resource,
  117. 0,0,0,0,$img_w,$img_h,$orig_w,$orig_h);
  118. }
  119. else
  120. {
  121. $img = imagecreatetruecolor($width, $height);
  122. $crop_height = ( $crop_height != -1 ? $crop_height : $orig_h );
  123. $crop_width = ( $crop_width != -1 ? $crop_width : $orig_w );
  124. if ($crop_width/$width*$height>$crop_height)
  125. $crop_width=round($crop_height*$width/$height);
  126. else
  127. $crop_height=round($crop_width*$height/$width);
  128. $x = ( $crop_x != -1 ? $crop_x : round((($orig_w/2) - ($crop_width/2))));
  129. $y = ( $crop_y != -1 ? $crop_y : round((($orig_h/2) - ($crop_height/2))));
  130. imagecopyresampled($img, $this->resource, 0,0, $x, $y,
  131. $width, $height, $crop_width, $crop_height);
  132. }
  133. $this->resource = $img;
  134. }
  135. function RoundCorners($bgcolor="FFFFFF", $radius=10)
  136. {
  137. imagealphablending($this->resource, false);
  138. $red = hexdec(substr($bgcolor, 0, 2));
  139. $green = hexdec(substr($bgcolor, 2, 2));
  140. $blue = hexdec(substr($bgcolor, 4, 2));
  141. $alpha = (strlen($bgcolor)>6 ? ((255/hexdec(substr($bgcolor, 4, 2)))*127) : false);
  142. $loops = array(
  143. array("startx" => 0, "endx" => $radius,
  144. "starty" => 0, "endy" => $radius,
  145. "centerx" => $radius, "centery" => $radius), // Top left
  146. array("startx" => imagesx($this->resource)-$radius, "endx" => imagesx($this->resource),
  147. "starty" => 0, "endy" => $radius,
  148. "centerx" => imagesx($this->resource)-1-$radius, "centery" => $radius), // Top right
  149. array("startx" => 0, "endx" => $radius,
  150. "starty" => imagesy($this->resource)-1-$radius, "endy" => imagesy($this->resource),
  151. "centerx" => $radius, "centery" => imagesy($this->resource)-1-$radius), // Bottom left
  152. array("startx" => imagesx($this->resource)-1-$radius, "endx" => imagesx($this->resource),
  153. "starty" => imagesy($this->resource)-1-$radius, "endy" => imagesy($this->resource),
  154. "centerx" => imagesx($this->resource)-1-$radius, "centery" => imagesy($this->resource)-1-$radius), // Bottom right
  155. );
  156. foreach ( $loops as $loop )
  157. for ( $i=$loop["startx"]; $i<$loop["endx"] && $i<imagesx($this->resource); $i++ )
  158. for ( $j=$loop["starty"]; $j<$loop["endy"] && $j<imagesy($this->resource); $j++ )
  159. {
  160. $distFromRadius = sqrt((($loop["centerx"]-$i)*($loop["centerx"]-$i)) + (($loop["centery"]-$j)*($loop["centery"]-$j)));
  161. $intensity = $distFromRadius - $radius;
  162. if ( $intensity < 0.0 ) continue;
  163. if ( $intensity > 1.0 ) $intensity = 1.0;
  164. $color = imagecolorat($this->resource, $i, $j);
  165. $r = ($color >> 16) & 0xFF;
  166. $g = ($color >> 8) & 0xFF;
  167. $b = ($color ) & 0xFF;
  168. $a = ($color >> 24) & 0xFF;
  169. if ( $alpha === false || $alpha == 0 )
  170. {
  171. $color = imagecolorallocate($this->resource,
  172. ($intensity*$red)+((1.0-$intensity)*$r),
  173. ($intensity*$green)+((1.0-$intensity)*$g),
  174. ($intensity*$blue)+((1.0-$intensity)*$b));
  175. }
  176. else if ( $alpha == 127 )
  177. {
  178. $color = imagecolorallocatealpha($this->resource,
  179. $r, $g, $b,
  180. ($intensity*$alpha)+((1.0-$intensity)*$a));
  181. }
  182. else
  183. {
  184. $color = imagecolorallocatealpha($this->resource,
  185. ($intensity*$red)+((1.0-$intensity)*$r),
  186. ($intensity*$green)+((1.0-$intensity)*$g),
  187. ($intensity*$blue)+((1.0-$intensity)*$b),
  188. ($intensity*$alpha)+((1.0-$intensity)*$a));
  189. }
  190. imagesetpixel($this->resource, $i, $j, $color);
  191. }
  192. }
  193. function Save($path="")
  194. {
  195. if ( !$path ) $path = $orig_path;
  196. if( preg_match("/.jpe?g$/i", $path))
  197. {
  198. return imagejpeg($this->resource,$path,90);
  199. }
  200. else if ( preg_match("/.gif$/i", $path))
  201. {
  202. return imagegif($this->resource,$path);
  203. }
  204. else if( preg_match("/.png$/i", $path))
  205. {
  206. return imagesavealpha($this->resource, true);
  207. return imagepng($this->resource,$path);
  208. }
  209. else
  210. {
  211. //DEBUG("Cannot save image: File type not supported");
  212. return false;
  213. }
  214. return true;
  215. }
  216. };
  217. ?>