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
| <?php
function creerImageDepuis($source) {
$imageCreateFrom = array(
"jpg" => "imagecreatefromjpeg",
"png" => "imagecreatefrompng"
);
// Récupération de l'extention du fichier
$extention = pathinfo($source, PATHINFO_EXTENSION);
$extention = strtolower($extention);
$extention = ($extention == "jpeg") ? "jpg" : $extention;
$function = $imageCreateFrom[$extention];
$image_source = $function($source);
// Sauvegarde de la transparence de l'image
imagealphablending($image_source, false);
imagesavealpha($image_source, true);
return $image_source;
}
function creerImage($image, $destination, $quality = 100) {
$imageCreate = array(
"jpg" => "imagejpeg",
"png" => "imagepng"
);
// Récupération de l'extention du fichier
$extention = pathinfo($destination, PATHINFO_EXTENSION);
$extention = strtolower($extention);
$function = $imageCreate[$extention];
if ($extention == "jpg") {
$function($image, $destination, $quality);
} else {
$function($image, $destination);
}
imagedestroy($image);
}
function redimensionneImage($source, $destination, $width, $height,
$quality = 100){
$image_source = creerImageDepuis($source);
$size = getimagesize($source);
if ($width == -1 || $height == -1) { // Si valeur automatique
// On remplace le -1 par la valeur calculée
$height = ($width != -1 && $height == -1) ?
(($size[1] * $width) / $size[0]) : $height;
$width = ($width == -1 && $height != -1) ?
(($size[0] * $height) / $size[1]) : $width;
} else if ($size[0] < $size[1]) { // Si portrait
$tmp = $height;
$height = $width;
$width = $tmp;
}
$image_redim = @imagecreatetruecolor($width, $height);
imagealphablending($image_redim, false);
imagesavealpha($image_redim, true);
imagecopyresampled ($image_redim, $image_source, 0, 0, 0, 0,
$width, $height, $size[0], $size[1]);
imagedestroy($image_source);
creerImage($image_redim, $destination);
}
?> |
Partager