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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
// redimensionne une image , si newWidth =0 , pas de redimensionnement
public function redimension($imageFile='',$imageType='jpg',$chemin='',$newLength=0)
{
set_time_limit(0);
$imageOK=true;
$f = new fileObject();
switch($imageType)
{
case 'gif':
$im=imagecreatefromgif($imageFile);
break;
case 'jpg':
// attention pour les jpg , si on ne les redimensionne pas , on va se contenter de la copier (pour essayer d'eviter les problemes d'images tronquees)
if($newLength!=0)
$im=imagecreatefromjpeg($imageFile);
break;
case 'peg': // pour les format 'jpeg' , cette detection devrait suffir
$im=imagecreatefromjpeg($imageFile);
break;
case 'png':
$im=imagecreatefrompng($imageFile);
break;
default:
echo 'format d\'image non supporté';
$imageOK=false;
break;
}
list($originalWidth, $originalHeight, $type, $attr) = getimagesize($imageFile);
if($newLength==0) // on ne redimensionne pas
{
$newWidth = $originalWidth;
$newHeight = $originalHeight;
if($imageType=='jpg')
{
// dans le cas d'une image jpg , on se contente de la copier
if(!copy($imageFile,$chemin))
{
$imageOK=false;
}
else
{
// verification du CRC
$i=0;
//echo "crc32 image file : ".$f->crc32_file($imageFile)."<br><br>";
//echo "crc32 chemin : ".$f->crc32_file($chemin)."<br><br>";
while(!($f->crc32_file($imageFile)==$f->crc32_file($chemin)))
{
copy($imageFile,$chemin);
$i++;
if($i>=5)
{
echo "il y a une erreur a la copie de l'image originale. Effacez l'image et retentez l'opération, si le problème persiste merci de contacter l'administrateur.<br>";
break;
}
}
}
}
else
{
$imDestination = imagecreatetruecolor( $newWidth, $newHeight);
imagecopyresampled( $imDestination, $im, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
imagejpeg( $imDestination, $chemin, 100);
imagedestroy($im);
imagedestroy($imDestination);
}
}
elseif($originalWidth>$originalHeight)
{
/*if($originalWidth/$originalHeight>2.5)
{
// on admet qu'a ce ratio entre la longueur et la hauteur , on a affaire a une image panoramique, on va donc calculer les dimensions de la nouvelle image en fonction de sa hauteur
$newHeight = $newLength;
$newWidth = round($originalWidth * $newHeight / $originalHeight);
}
else
{*/
$newWidth=$newLength;
$newHeight = round($originalHeight * $newWidth / $originalWidth);
//}
$imDestination = imagecreatetruecolor( $newWidth, $newHeight);
imagecopyresampled( $imDestination, $im, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
imagejpeg( $imDestination, $chemin, 100);
imagedestroy($im);
imagedestroy($imDestination);
}
else
{
$newHeight = $newLength;
$newWidth = round($originalWidth * $newHeight / $originalHeight);
$imDestination = imagecreatetruecolor( $newWidth, $newHeight);
imagecopyresampled( $imDestination, $im, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
imagejpeg( $imDestination, $chemin, 100);
imagedestroy($im);
imagedestroy($imDestination);
}
return $imageOK;
} |
Partager