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
| <?php
// cropResizeAndSave(200,400,'/images/source.jpg','.jpg','/images/thumbs/thumbSource.jpg');
function cropResizeAndSave($nw, $nh, $source, $stype, $dest)
{
$size = getimagesize($source);
$w = $size[0];
$h = $size[1];
switch($stype)
{
case '.gif':
$sourceImg = imagecreatefromgif($source);
break;
case '.jpeg':
$sourceImg = imagecreatefromjpeg($source);
break;
case '.jpg':
$sourceImg = imagecreatefromjpeg($source);
break;
case '.png':
$sourceImg = imagecreatefrompng($source);
break;
}
$dimg = imagecreatetruecolor($nw, $nh); // imagecreatetruecolor(largeur, hauteur) créé un rectange de séléction, ca va etre la taille de notre image générée
$ratioX = $w/$nw; // ratio : ( largeur / nouvelle largeur), X : axe des abscysses
$ratioY = $h/$nh; // idem pour la hauteur, hauteur de l'image source divisée par la hauteur qu'on veut pour voir le coefficient de différence
$h_height = $nh/2;
$w_height = $nw/2;
if($ratioX> $ratioY)
{
$adjusted_width = $w / $ratioY;
$half_width = $adjusted_width / 2;
$int_width = $half_width - $w_height;
imagecopyresampled($dimg,$sourceImg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
}
elseif(($ratioX <$ratioY))
{
$adjusted_height = $h / $ratioX;
$half_height = $adjusted_height / 2;
$int_height = $half_height - $h_height;
imagecopyresampled($dimg,$sourceImg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
}
else
{
imagecopyresampled($dimg,$sourceImg,0,0,0,0,$nw,$nh,$w,$h);
}
imagejpeg($dimg,$dest,100);
}
?> |
Partager