[ Index ] |
|
Code source de Dotclear 1.2.5 |
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 23 class images 24 { 25 function cropImg($uri,$file,$dest_type,$w=120,$h=200) 26 { 27 if (!file_exists($uri)) { 28 return false; 29 } 30 31 if (($size = @getimagesize($uri)) === false) { 32 return false; 33 } 34 35 $type = $size[2]; 36 $H = $size[1]; 37 $W = $size[0]; 38 39 if ($type == '2') { 40 $function = 'imagecreatefromjpeg'; 41 } elseif ($type == '3') { 42 $function = 'imagecreatefrompng'; 43 } else { 44 return false; 45 } 46 47 if (!function_exists($function)) { 48 return false; 49 } 50 51 if (($img = @$function($uri)) == false) { 52 return false; 53 } 54 55 # Si original plus petit 56 if ($H <= $h && $W <= $w) 57 { 58 if (copy($uri,$file) !== false) { 59 return true; 60 } else { 61 return false; 62 } 63 } 64 65 $rB = $H/$W; 66 $rS = $h/$w; 67 if (($H > $h) && ($W > $w)) { 68 if ($rB > $rS) { 69 $height = $h; 70 $width = $height/$rB; 71 } else { 72 $width = $w; 73 $height = $width*$rB; 74 } 75 } elseif ($H > $h) { 76 $height = $h; 77 $width = $height/$rB; 78 } elseif ($W > $w) { 79 $width = $w; 80 $height = $width*$rB; 81 } else { 82 $height = $H; 83 $width = $W; 84 } 85 86 $zx = $W/$width; 87 $zy = $H/$height; 88 89 $create_function = images::checkTrueColor() ? 'imagecreatetruecolor' : 'imagecreate'; 90 91 if (($img2 = $create_function(round($width),round($height))) === false) { 92 return false; 93 } 94 95 //images::CopyResampleBicubic($img2,$img,0,0,0,0,$width,$height,$zx,$zy); 96 imagecopyresized($img2,$img,0,0,0,0,round($width),round($height),$W,$H); 97 98 if (!is_dir(dirname($file))) { 99 $dmod = fileperms(dirname(dirname($file))); 100 if (@mkdir(dirname($file),$dmod) === false) { 101 return false; 102 } 103 chmod(dirname($file),$dmod); 104 } 105 106 if ($dest_type == 'png') { 107 $endfunction = 'imagepng'; 108 } elseif ($dest_type == 'jpeg') { 109 $endfunction = 'imagejpeg'; 110 } 111 112 if (@$endfunction($img2,$file,80) === false) { 113 return false; 114 } 115 chmod($file,fileperms(dirname($file)) & ~0111); 116 117 imagedestroy($img2); 118 return true; 119 } 120 121 function CopyResampleBicubic(&$dst, &$src, $dstx, $dsty, $srcx, $srcy, $w, $h, $zoomX, $zoomY = '') 122 { 123 if (!$zoomY) { 124 $zoomY = $zoomX; 125 } 126 127 $palsize = ImageColorsTotal($src); 128 129 for ($i = 0; $i<$palsize; $i++) 130 { 131 $colors = ImageColorsForIndex($src, $i); 132 ImageColorAllocate($dst, $colors['red'], $colors['green'], $colors['blue']); 133 } 134 135 $zoomX2 = (int)($zoomX/2); 136 $zoomY2 = (int)($zoomY/2); 137 138 $dstX = imagesx($dst); 139 $dstY = imagesy($dst); 140 $srcX = imagesx($src); 141 $srcY = imagesy($src); 142 143 for ($j = 0; $j<($h-$dsty); $j++) 144 { 145 $sY = (int)($j*$zoomY)+$srcy; 146 $y13 = $sY+$zoomY2; 147 $dY = $j+$dsty; 148 149 if (($sY >= $srcY) or ($dY >= $dstY) or ($y13 >= $srcY)) { 150 break 1; 151 } 152 153 for ($i = 0; $i<($w-$dstx); $i++) 154 { 155 $sX = (int)($i*$zoomX)+$srcx; 156 $x34 = $sX+$zoomX2; 157 $dX = $i+$dstx; 158 159 if (($sX >= $srcX) or ($dX >= $dstX) or ($x34 >= $srcX)) { 160 break 1; 161 } 162 163 $c1 = ImageColorsForIndex($src, ImageColorAt($src, $sX, $y13)); 164 $c2 = ImageColorsForIndex($src, ImageColorAt($src, $sX, $sY)); 165 $c3 = ImageColorsForIndex($src, ImageColorAt($src, $x34, $y13)); 166 $c4 = ImageColorsForIndex($src, ImageColorAt($src, $x34, $sY)); 167 168 $r = ($c1['red']+$c2['red']+$c3['red']+$c4['red'])/4; 169 $g = ($c1['green']+$c2['green']+$c3['green']+$c4['green'])/4; 170 $b = ($c1['blue']+$c2['blue']+$c3['blue']+$c4['blue'])/4; 171 172 ImageSetPixel($dst, $dX, $dY, ImageColorClosest($dst, $r, $g, $b)); 173 } 174 } 175 } 176 177 function type($p) 178 { 179 if (($size = @getimagesize($p)) === false) { 180 return false; 181 } 182 183 $type = $size[2]; 184 185 if ($type == '1') { 186 return 'gif'; 187 } elseif ($type == '2') { 188 return 'jpeg'; 189 } elseif ($type == '3') { 190 return 'png'; 191 } else { 192 return false; 193 } 194 } 195 196 function checkTrueColor() 197 { 198 if (function_exists('gd_info')) 199 { 200 $gdinfo = gd_info(); 201 $gdversion = $gdinfo['GD Version']; 202 if (strpos($gdversion,'2.') !== false) { 203 return true; 204 } 205 } 206 207 return false; 208 } 209 } 210 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Feb 23 21:40:15 2007 | par Balluche grâce à PHPXref 0.7 |