lib.image.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. # ***** BEGIN LICENSE BLOCK *****
  3. # This file is part of DotClear.
  4. # Copyright (c) 2004 Olivier Meunier and contributors. All rights
  5. # reserved.
  6. #
  7. # DotClear 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. # DotClear 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 General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with DotClear; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. #
  21. # ***** END LICENSE BLOCK *****
  22. class images
  23. {
  24. function cropImg($uri,$file,$dest_type,$w=120,$h=200)
  25. {
  26. if (!file_exists($uri)) {
  27. return false;
  28. }
  29. if (($size = @getimagesize($uri)) === false) {
  30. return false;
  31. }
  32. $type = $size[2];
  33. $H = $size[1];
  34. $W = $size[0];
  35. if ($type == '2') {
  36. $function = 'imagecreatefromjpeg';
  37. } elseif ($type == '3') {
  38. $function = 'imagecreatefrompng';
  39. } else {
  40. return false;
  41. }
  42. if (!function_exists($function)) {
  43. return false;
  44. }
  45. if (($img = @$function($uri)) == false) {
  46. return false;
  47. }
  48. # Si original plus petit
  49. if ($H <= $h && $W <= $w)
  50. {
  51. if (copy($uri,$file) !== false) {
  52. return true;
  53. } else {
  54. return false;
  55. }
  56. }
  57. $rB = $H/$W;
  58. $rS = $h/$w;
  59. if (($H > $h) && ($W > $w)) {
  60. if ($rB > $rS) {
  61. $height = $h;
  62. $width = $height/$rB;
  63. } else {
  64. $width = $w;
  65. $height = $width*$rB;
  66. }
  67. } elseif ($H > $h) {
  68. $height = $h;
  69. $width = $height/$rB;
  70. } elseif ($W > $w) {
  71. $width = $w;
  72. $height = $width*$rB;
  73. } else {
  74. $height = $H;
  75. $width = $W;
  76. }
  77. $zx = $W/$width;
  78. $zy = $H/$height;
  79. $create_function = images::checkTrueColor() ? 'imagecreatetruecolor' : 'imagecreate';
  80. if (($img2 = $create_function(round($width),round($height))) === false) {
  81. return false;
  82. }
  83. //images::CopyResampleBicubic($img2,$img,0,0,0,0,$width,$height,$zx,$zy);
  84. imagecopyresized($img2,$img,0,0,0,0,round($width),round($height),$W,$H);
  85. if (!is_dir(dirname($file))) {
  86. $dmod = fileperms(dirname(dirname($file)));
  87. if (@mkdir(dirname($file),$dmod) === false) {
  88. return false;
  89. }
  90. chmod(dirname($file),$dmod);
  91. }
  92. if ($dest_type == 'png') {
  93. $endfunction = 'imagepng';
  94. } elseif ($dest_type == 'jpeg') {
  95. $endfunction = 'imagejpeg';
  96. }
  97. if (@$endfunction($img2,$file,80) === false) {
  98. return false;
  99. }
  100. chmod($file,fileperms(dirname($file)) & ~0111);
  101. imagedestroy($img2);
  102. return true;
  103. }
  104. function CopyResampleBicubic(&$dst, &$src, $dstx, $dsty, $srcx, $srcy, $w, $h, $zoomX, $zoomY = '')
  105. {
  106. if (!$zoomY) {
  107. $zoomY = $zoomX;
  108. }
  109. $palsize = ImageColorsTotal($src);
  110. for ($i = 0; $i<$palsize; $i++)
  111. {
  112. $colors = ImageColorsForIndex($src, $i);
  113. ImageColorAllocate($dst, $colors['red'], $colors['green'], $colors['blue']);
  114. }
  115. $zoomX2 = (int)($zoomX/2);
  116. $zoomY2 = (int)($zoomY/2);
  117. $dstX = imagesx($dst);
  118. $dstY = imagesy($dst);
  119. $srcX = imagesx($src);
  120. $srcY = imagesy($src);
  121. for ($j = 0; $j<($h-$dsty); $j++)
  122. {
  123. $sY = (int)($j*$zoomY)+$srcy;
  124. $y13 = $sY+$zoomY2;
  125. $dY = $j+$dsty;
  126. if (($sY >= $srcY) or ($dY >= $dstY) or ($y13 >= $srcY)) {
  127. break 1;
  128. }
  129. for ($i = 0; $i<($w-$dstx); $i++)
  130. {
  131. $sX = (int)($i*$zoomX)+$srcx;
  132. $x34 = $sX+$zoomX2;
  133. $dX = $i+$dstx;
  134. if (($sX >= $srcX) or ($dX >= $dstX) or ($x34 >= $srcX)) {
  135. break 1;
  136. }
  137. $c1 = ImageColorsForIndex($src, ImageColorAt($src, $sX, $y13));
  138. $c2 = ImageColorsForIndex($src, ImageColorAt($src, $sX, $sY));
  139. $c3 = ImageColorsForIndex($src, ImageColorAt($src, $x34, $y13));
  140. $c4 = ImageColorsForIndex($src, ImageColorAt($src, $x34, $sY));
  141. $r = ($c1['red']+$c2['red']+$c3['red']+$c4['red'])/4;
  142. $g = ($c1['green']+$c2['green']+$c3['green']+$c4['green'])/4;
  143. $b = ($c1['blue']+$c2['blue']+$c3['blue']+$c4['blue'])/4;
  144. ImageSetPixel($dst, $dX, $dY, ImageColorClosest($dst, $r, $g, $b));
  145. }
  146. }
  147. }
  148. function type($p)
  149. {
  150. if (($size = @getimagesize($p)) === false) {
  151. return false;
  152. }
  153. $type = $size[2];
  154. if ($type == '1') {
  155. return 'gif';
  156. } elseif ($type == '2') {
  157. return 'jpeg';
  158. } elseif ($type == '3') {
  159. return 'png';
  160. } else {
  161. return false;
  162. }
  163. }
  164. function checkTrueColor()
  165. {
  166. if (function_exists('gd_info'))
  167. {
  168. $gdinfo = gd_info();
  169. $gdversion = $gdinfo['GD Version'];
  170. if (strpos($gdversion,'2.') !== false) {
  171. return true;
  172. }
  173. }
  174. return false;
  175. }
  176. }
  177. ?>