1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
<?
class images {
function getAdjustedSize($orig_w, $orig_h, $max_w, $max_h) {
if(!$max_w && $max_h) $max_w = $orig_w * $max_h / $orig_h;
else if(!$max_h && $max_w) $max_h = $orig_h * $max_w / $orig_w;
else if(!$max_w && !$max_h) { $max_w = $orig_w; $max_h = $orig_h; }
$ratio = $max_w / $orig_w;
$h = $orig_h * $ratio;
if($h <= $max_h) $w = $max_w;
else {
$ratio = $max_h / $orig_h;
$w = $orig_w * $ratio;
$h = $orig_h * $ratio;
}
if($w>$orig_w || $h>$orig_h) { // si agrandissement
$w = $orig_w;
$h = $orig_h;
}
return array(round($w), round($h));
}
function echoResized($source, $w, $h) {
if(!file_exists($source)) return false;
list($sw, $sh) = getimagesize($source);
list($tw, $th) = images::getAdjustedSize($sw, $sh, $w, $h);
$pi = pathinfo($source);
$type = strtolower($pi['extension']);
switch($type)
{
case 'jpg':
$srcImg = ImageCreateFromJPEG($source);
$im=ImageCreateTrueColor($tw, $th);
ImageCopyResampled($im,$srcImg,0,0,0,0,$tw,$th,$sw,$sh);
header("Content-type: image/jpeg");
$r = @ImageJPEG($im, '' ,80);
ImageDestroy($im);
return $r;
case 'gif':
$srcImg = ImageCreateFromGIF($source);
$im=ImageCreateTrueColor($tw, $th);
imageAlphaBlending($im, false);
imageSaveAlpha($im,true);
$transparent = imageColorAllocateAlpha($im, 255, 255, 255, 0);
for($y=0;$y<$th;$y++) for($x=0;$x<$tw;$x++) imageSetPixel( $im, $x, $y, $transparent );
ImageCopyResampled($im,$srcImg,0,0,0,0,$tw,$th,$sw,$sh);
header("Content-type: image/gif");
$r = @ImageGIF($im);
ImageDestroy($im);
return $r;
case 'png':
$srcImg = ImageCreateFromPNG($source);
$im=ImageCreateTrueColor($tw, $th);
//imageAntiAlias($im,true);
imageAlphaBlending($im, false);
imageSaveAlpha($im,true);
$transparent = imageColorAllocateAlpha($im, 255, 255, 255, 0);
for($y=0;$y<$th;$y++) for($x=0;$x<$tw;$x++) imageSetPixel( $im, $x, $y, $transparent );
ImageCopyResampled($im,$srcImg,0,0,0,0,$tw,$th,$sw,$sh);
header("Content-type: image/png");
$r = @ImagePNG($im);
ImageDestroy($im);
return $r;
}
return false;
}
}
?> |
Partager