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 116 117 118 119 120
|
public function upload($files) {
$upload_dir = IMAGE_DIR_PATH;
foreach($files['upload']['error'] as $key => $error) {
$error = 0;
if($error == UPLOAD_ERR_OK) {
$tmp_name = $files['upload']['tmp_name'][$key];
$filename = $files['upload']['name'][$key];
$filename = $this->cleanText($filename);
$filename = strtolower($filename);
$type = $files['upload']['type'][$key];
$size = $files['upload']['size'][$key];
if($type == 'image/jpeg') {
if($size <= 10000000) {
if(move_uploaded_file($tmp_name, $upload_dir . $filename) === false) {
$error++;
}
else {
$this->createThumbnail($filename);
}
}
else {
$error++;
}
}
else {
$error++;
}
}
}
if($error == 0) {
return true;
}
else {
return false;
}
}
public function createThumbnail($filename) {
$image = IMAGE_DIR_PATH . $filename;
$vignette = THUMB_DIR_PATH . $filename;
$size = getimagesize($image);
$largeur = $size[0];
$hauteur = $size[1];
$largeur_max = 200;
$hauteur_max = 200;
$image_src = imagecreatefromjpeg($image);
if($largeur > $largeur_max OR $hauteur > $hauteur_max) {
if($hauteur <= $largeur) {
$ratio = $largeur_max / $largeur;
}
else {
$ratio = $hauteur_max / $hauteur;
}
}
else {
$ratio = 1;
}
$image_destination = imagecreatetruecolor(round($largeur * $ratio), round($hauteur * $ratio));
imagecopyresampled($image_destination, $image_src, 0, 0, 0, 0, round($largeur * $ratio),
round($hauteur * $ratio), $largeur, $hauteur);
if(!imagejpeg($image_destination, $vignette)) {
$error_msg = 'la création de la vignette a echouée pour l\'image ' . $image;
return $error_msg;
exit;
}
else {
return true;
}
}
public function cleanText($filename) {
$special = array(' ', '\'', 'á','à','â','ä','ã','å','ç','é','è','ê'
,'ë','í','ì','î','ï','ñ','ó','ò','ô','ö','õ','ú','ù','û','ü','ý','ÿ
','Á','À','Â','Ä','Ã','Å','Ç','É','È','Ê','Ë','Í','Ï','Î','Ì','Ñ','
Ó','Ò','Ô','Ö','Õ','Ú','Ù','Û','Ü','Ý');
$normal = array('_', '', 'a','a','a','a','a','a','c','e','e','e','e
','i','i','i','i','n','o','o','o','o','o','u','u','u','u','y','y','
A','A','A','A','A','A','C','E','E','E','E','E','I','I','I','I','N',
'O','O','O','O','O','U','U','U','U','Y');
$result = str_replace($special, $normal, "c'áàâäã'AAåçéèêëíìîïñóòôöõúùûüýÿÁÀÂÄÃÅÇÉÈÊËÍÏÎÌÑÓÒÔÖÕÚÙÛÜÝ");
} |
Partager