par
yoann38 » 26 juin 2016, 20:04
j'ai ma premiere fonction
createMiniature avec $quality mais même si je change 75 par 50 par exemple, je ne gagne rien du tout en poids
et ensuite ma deuxième fonction
dessous
// Creation d'une miniature
function createMiniature($originalFileName, $destinationFileName, $finalWidth=NULL, $finalHeight=NULL, $quality=75){
// Si l'image existe déjà, on la supprime
if(file_exists($destinationFileName) && ($originalFileName != $destinationFileName)) unlink($destinationFileName);
// Variable
$fichier = pathinfo($destinationFileName, PATHINFO_BASENAME);
$content_dir = pathinfo($destinationFileName, PATHINFO_DIRNAME);
$extension = pathinfo($originalFileName, PATHINFO_EXTENSION);
$return = NULL;
// Si le dossier n'existe pas, on le créé
createDir($content_dir);
if (preg_match("/^jpe?g$/i", $extension)){
$sourceImage = imagecreatefromjpeg($originalFileName);
$jpeg = TRUE;
}
if (preg_match("/^png$/i", $extension)){
$sourceImage = imagecreatefrompng($originalFileName);
$png = TRUE;
}
if (preg_match("/^gif$/i", $extension)){
$sourceImage = imagecreatefromgif($originalFileName);
$gif = TRUE;
}
// On recupere les tailles de l'image de base
$imgWidth = imagesx($sourceImage);
$imgHeight = imagesy($sourceImage);
// Hack pour que l'image reste proportionnel si les deux cotés sont renseigné
$finalWidth = ($finalWidth == $finalHeight) && ($imgHeight >= $imgWidth) ? NULL : $finalWidth;
$finalHeight = ($finalWidth == $finalHeight) && ($imgHeight <= $imgWidth) ? NULL : $finalHeight;
// En cas d'informations que sur un coté
if(empty($finalWidth) && empty($finalHeight)) {
$finalWidth = $imgWidth;
$finalHeight = $imgHeight;
} elseif(empty($finalWidth) && !empty($finalHeight)) {
$finalWidth = $imgWidth*($finalHeight/$imgHeight);
} elseif(empty($finalHeight) && !empty($finalWidth)) {
$finalHeight = $imgHeight*($finalWidth/$imgWidth);
}
// Si ca ne miniature pas, on annule
if(($finalHeight >= $imgHeight) || ($finalWidth >= $imgWidth)) {
// Si l'image est different de la destination alors on copie la même image ailleurs
if($originalFileName != $destinationFileName) {
copy($originalFileName, $destinationFileName);
$return .= "<br />L'image <strong>".$fichier."</strong> n'avait pas besoin d'être miniaturisée. Elle a juste été dupliquée vers la nouvelle destination.";
}
} else {
// Création de la miniature
$newImage = imagecreatetruecolor($finalWidth, $finalHeight);
$color_bg = imagecolorallocate($newImage, 252, 252, 252);
imagefill($newImage, 0, 0, $color_bg);
$imagecopyresampled = imagecopyresampled($newImage, $sourceImage, 0, 0, 0, 0, $finalWidth, $finalHeight, $imgWidth, $imgHeight);
!empty($jpeg) ? imagejpeg($newImage, $destinationFileName, $quality) : false;
!empty($png) ? imagepng($newImage, $destinationFileName, $quality) : false;
!empty($gif) ? imagegif($newImage, $destinationFileName, $quality) : false;
imagedestroy($newImage);
$return .= "<br />Le fichier <strong>".$fichier."</strong> à bien été miniaturisé au format ".round($finalWidth)."x".round($finalHeight);
}
return $return;
}
Deuxième fonction:
// Creation d'une image coupé
function makeSample($originalFileName, $destinationFileName=NULL, $finalWidth=NULL, $finalHeight=NULL, $random=0, $quality=75) {
// Variable
$destinationFileName = !empty($destinationFileName) ? $destinationFileName : $originalFileName;
$fichier = pathinfo($destinationFileName, PATHINFO_BASENAME);
$content_dir = pathinfo($destinationFileName, PATHINFO_DIRNAME);
$extension = pathinfo($originalFileName, PATHINFO_EXTENSION);
$return = NULL;
// Si le dossier n'existe pas, on le créé
createDir($content_dir);
if (preg_match("/^jpe?g$/i", $extension)){
$sourceImage = imagecreatefromjpeg($originalFileName);
$jpeg = TRUE;
}
if (preg_match("/^png$/i", $extension)){
$sourceImage = imagecreatefrompng($originalFileName);
$png = TRUE;
}
if (preg_match("/^gif$/i", $extension)){
$sourceImage = imagecreatefromgif($originalFileName);
$gif = TRUE;
}
// On recuperere les tailles de l'image
$imgWidth = imagesx($sourceImage);
$imgHeight = imagesy($sourceImage);
if(($imgWidth == $finalWidth) && ($imgHeight == $finalHeight)) {
$return .= "<br />L'image ".$fichier." n'avait pas besoin d'être recadrée";
} else {
// Recadrage minimal
$min = ($imgWidth < $imgHeight) ? $imgWidth : $imgHeight;
$finalWidth = !empty($finalWidth) ? $finalWidth : $min;
$finalHeight = !empty($finalHeight) ? $finalHeight : $min;
// On recadre l'image en coupant les parties en dehors du carré
if($random == 1) {
$cropX = ($imgWidth <= $finalWidth) ? 0 : rand(0, $imgWidth-$finalWidth);
$cropY = ($imgHeight <= $finalHeight) ? 0 : rand(0, $imgHeight-$finalHeight);
$debutWidth = ($imgWidth < $finalWidth) ? ($finalWidth-$imgWidth)/2 : 0;
$debutHeight = ($imgHeight < $finalHeight) ? ($finalHeight-$imgHeight)/2 : 0;
} else {
$cropX = ($imgWidth <= $finalWidth) ? 0 : ($imgWidth-$finalWidth)/2;
$cropY = ($imgHeight <= $finalHeight) ? 0 : ($imgHeight-$finalHeight)/2;
$debutWidth = ($imgWidth < $finalWidth) ? ($finalWidth-$imgWidth)/2 : 0;
$debutHeight = ($imgHeight < $finalHeight) ? ($finalHeight-$imgHeight)/2 : 0;
}
// Création de la miniature
$newImage = imagecreatetruecolor($finalWidth, $finalHeight);
$color_bg = imagecolorallocate($newImage, 255, 255, 255);
imagefill($newImage, 0, 0, $color_bg);
$imagecopyresampled = imagecopyresampled($newImage, $sourceImage, $debutWidth, $debutHeight, $cropX, $cropY, $imgWidth, $imgHeight, $imgWidth, $imgHeight);
!empty($jpeg) ? imagejpeg($newImage, $destinationFileName, $quality) : false;
!empty($png) ? imagepng($newImage, $destinationFileName, $quality) : false;
!empty($gif) ? imagegif($newImage, $destinationFileName, $quality) : false;
imagedestroy($newImage);
$return .= "<br />Le fichier <strong>".$fichier."</strong> a bien été recadré avec la taille ".round($finalWidth)."x".round($finalHeight);
}
return $return;
}
j'ai ma premiere fonction[b] createMiniature[/b] avec $quality mais même si je change 75 par 50 par exemple, je ne gagne rien du tout en poids
et ensuite ma deuxième fonction
dessous
[php]// Creation d'une miniature
function createMiniature($originalFileName, $destinationFileName, $finalWidth=NULL, $finalHeight=NULL, $quality=75){
// Si l'image existe déjà, on la supprime
if(file_exists($destinationFileName) && ($originalFileName != $destinationFileName)) unlink($destinationFileName);
// Variable
$fichier = pathinfo($destinationFileName, PATHINFO_BASENAME);
$content_dir = pathinfo($destinationFileName, PATHINFO_DIRNAME);
$extension = pathinfo($originalFileName, PATHINFO_EXTENSION);
$return = NULL;
// Si le dossier n'existe pas, on le créé
createDir($content_dir);
if (preg_match("/^jpe?g$/i", $extension)){
$sourceImage = imagecreatefromjpeg($originalFileName);
$jpeg = TRUE;
}
if (preg_match("/^png$/i", $extension)){
$sourceImage = imagecreatefrompng($originalFileName);
$png = TRUE;
}
if (preg_match("/^gif$/i", $extension)){
$sourceImage = imagecreatefromgif($originalFileName);
$gif = TRUE;
}
// On recupere les tailles de l'image de base
$imgWidth = imagesx($sourceImage);
$imgHeight = imagesy($sourceImage);
// Hack pour que l'image reste proportionnel si les deux cotés sont renseigné
$finalWidth = ($finalWidth == $finalHeight) && ($imgHeight >= $imgWidth) ? NULL : $finalWidth;
$finalHeight = ($finalWidth == $finalHeight) && ($imgHeight <= $imgWidth) ? NULL : $finalHeight;
// En cas d'informations que sur un coté
if(empty($finalWidth) && empty($finalHeight)) {
$finalWidth = $imgWidth;
$finalHeight = $imgHeight;
} elseif(empty($finalWidth) && !empty($finalHeight)) {
$finalWidth = $imgWidth*($finalHeight/$imgHeight);
} elseif(empty($finalHeight) && !empty($finalWidth)) {
$finalHeight = $imgHeight*($finalWidth/$imgWidth);
}
// Si ca ne miniature pas, on annule
if(($finalHeight >= $imgHeight) || ($finalWidth >= $imgWidth)) {
// Si l'image est different de la destination alors on copie la même image ailleurs
if($originalFileName != $destinationFileName) {
copy($originalFileName, $destinationFileName);
$return .= "<br />L'image <strong>".$fichier."</strong> n'avait pas besoin d'être miniaturisée. Elle a juste été dupliquée vers la nouvelle destination.";
}
} else {
// Création de la miniature
$newImage = imagecreatetruecolor($finalWidth, $finalHeight);
$color_bg = imagecolorallocate($newImage, 252, 252, 252);
imagefill($newImage, 0, 0, $color_bg);
$imagecopyresampled = imagecopyresampled($newImage, $sourceImage, 0, 0, 0, 0, $finalWidth, $finalHeight, $imgWidth, $imgHeight);
!empty($jpeg) ? imagejpeg($newImage, $destinationFileName, $quality) : false;
!empty($png) ? imagepng($newImage, $destinationFileName, $quality) : false;
!empty($gif) ? imagegif($newImage, $destinationFileName, $quality) : false;
imagedestroy($newImage);
$return .= "<br />Le fichier <strong>".$fichier."</strong> à bien été miniaturisé au format ".round($finalWidth)."x".round($finalHeight);
}
return $return;
}[/php]
[b]Deuxième fonction:[/b]
[php]// Creation d'une image coupé
function makeSample($originalFileName, $destinationFileName=NULL, $finalWidth=NULL, $finalHeight=NULL, $random=0, $quality=75) {
// Variable
$destinationFileName = !empty($destinationFileName) ? $destinationFileName : $originalFileName;
$fichier = pathinfo($destinationFileName, PATHINFO_BASENAME);
$content_dir = pathinfo($destinationFileName, PATHINFO_DIRNAME);
$extension = pathinfo($originalFileName, PATHINFO_EXTENSION);
$return = NULL;
// Si le dossier n'existe pas, on le créé
createDir($content_dir);
if (preg_match("/^jpe?g$/i", $extension)){
$sourceImage = imagecreatefromjpeg($originalFileName);
$jpeg = TRUE;
}
if (preg_match("/^png$/i", $extension)){
$sourceImage = imagecreatefrompng($originalFileName);
$png = TRUE;
}
if (preg_match("/^gif$/i", $extension)){
$sourceImage = imagecreatefromgif($originalFileName);
$gif = TRUE;
}
// On recuperere les tailles de l'image
$imgWidth = imagesx($sourceImage);
$imgHeight = imagesy($sourceImage);
if(($imgWidth == $finalWidth) && ($imgHeight == $finalHeight)) {
$return .= "<br />L'image ".$fichier." n'avait pas besoin d'être recadrée";
} else {
// Recadrage minimal
$min = ($imgWidth < $imgHeight) ? $imgWidth : $imgHeight;
$finalWidth = !empty($finalWidth) ? $finalWidth : $min;
$finalHeight = !empty($finalHeight) ? $finalHeight : $min;
// On recadre l'image en coupant les parties en dehors du carré
if($random == 1) {
$cropX = ($imgWidth <= $finalWidth) ? 0 : rand(0, $imgWidth-$finalWidth);
$cropY = ($imgHeight <= $finalHeight) ? 0 : rand(0, $imgHeight-$finalHeight);
$debutWidth = ($imgWidth < $finalWidth) ? ($finalWidth-$imgWidth)/2 : 0;
$debutHeight = ($imgHeight < $finalHeight) ? ($finalHeight-$imgHeight)/2 : 0;
} else {
$cropX = ($imgWidth <= $finalWidth) ? 0 : ($imgWidth-$finalWidth)/2;
$cropY = ($imgHeight <= $finalHeight) ? 0 : ($imgHeight-$finalHeight)/2;
$debutWidth = ($imgWidth < $finalWidth) ? ($finalWidth-$imgWidth)/2 : 0;
$debutHeight = ($imgHeight < $finalHeight) ? ($finalHeight-$imgHeight)/2 : 0;
}
// Création de la miniature
$newImage = imagecreatetruecolor($finalWidth, $finalHeight);
$color_bg = imagecolorallocate($newImage, 255, 255, 255);
imagefill($newImage, 0, 0, $color_bg);
$imagecopyresampled = imagecopyresampled($newImage, $sourceImage, $debutWidth, $debutHeight, $cropX, $cropY, $imgWidth, $imgHeight, $imgWidth, $imgHeight);
!empty($jpeg) ? imagejpeg($newImage, $destinationFileName, $quality) : false;
!empty($png) ? imagepng($newImage, $destinationFileName, $quality) : false;
!empty($gif) ? imagegif($newImage, $destinationFileName, $quality) : false;
imagedestroy($newImage);
$return .= "<br />Le fichier <strong>".$fichier."</strong> a bien été recadré avec la taille ".round($finalWidth)."x".round($finalHeight);
}
return $return;
}[/php]