paniers/functions/functions_tmp_files.php

153 lines
4.8 KiB
PHP

<?php
include_once($GLOBALS['prefixe'].'conf/conf.php');
function uploadTmpFile($fichier_tmp) {
$r = array(
'tmp' => "", // NOM (AVEC EXTENSION) DU FICHIER TEMPORAIRE
'dir' => TMP_DIR, // DOSSIER DU FICHIER TEMPORAIRE (AVEC / DE FIN)
'nom' => "", // NOM DU FICHIER D'ORIGINE (SANS EXTENSION)
'size' => "", // TAILLE DU FICHIER TEMPORAIRE (NON FORMATEE)
'prw' => "", // URL PREVIEW ICON FILE
'ext' => "", // EXTENSION DU FICHIER D'ORIGINE (SANS LE POINT)
'erreur' => false,
'erreur_desc' => ""
);
if($fichier_tmp['tmp_name']!='') {
// RECUPERATION DE L'EXTENSION DU FICHIER EXISTANT
$r["ext"] = pathinfo($fichier_tmp['name'], PATHINFO_EXTENSION);
$r["nom"] = pathinfo($fichier_tmp['name'], PATHINFO_FILENAME );
// CREATION DU NOM DU FICHIER TEMPORAIRE
$r["tmp"] = replaceSymboles($r["nom"]).'-|_'.time().'.'.$r["ext"];
if(move_uploaded_file($fichier_tmp['tmp_name'], TMP_DIR.$r["tmp"])) {
// AJOUT DU FICHIER A L'HISTORIQUE D'UPLOAD TMP
if(isset($_SESSION['tmpUpload'])) $_SESSION['tmpUpload'] = array();
$_SESSION['tmpUpload'][] = TMP_DIR.$r["tmp"];
$r["prw"] = "index.php?action=get_tmp_file&file=".$r["tmp"];
$r["size"] = filesize(TMP_DIR.$r["tmp"]);
}
else {
$r['erreur'] = true;
$r['erreur'] = "ERREUR : une erreur est survenus durant l'enregistrement du fichier temporaire...' ";
}
}
else {
$r['erreur'] = true;
$r['erreur'] = "ERREUR : nom de fichier temporaire inconnu...' ";
}
return $r;
}
function saveTmpPhoto($tmp,$dest_dir,$dest_name=false,$rotate=0) {
if(file_exists(TMP_DIR.$tmp)) {
// RECUPERATION DU TYPE D'IMAGE
$ext = getImgExtFromMimeType(TMP_DIR.$tmp);
// DEST
$filename = $tmp;
if($dest_name!="") $filename = $dest_name.'.'.$ext;
if($dest_dir != "" && substr($dest_dir,-1) != "/") $dest_dir .= "/";
$dest = $dest_dir.$filename;
// REDIM & SAVE
redimg(TMP_DIR.$tmp, $dest, PHOTOS_SIZE, PHOTOS_SIZE, true, true, false, $ext);
// COPY
copy(TMP_DIR.$tmp,$dest);
// ROTATE
if($rotate==90 || $rotate==180 || $rotate==270) rotatePhoto($dest, $rotate, true);
return array('file' => $dest, 'filename' => $filename);
}
else {
return "nofile";
}
}
function clearTmpUpload() {
if(isset($_SESSION['tmpUpload']) && is_array($_SESSION['tmpUpload'])) {
foreach($_SESSION['tmpUpload'] as $k => $f) if(file_exists(TMP_DIR.$f)) unlink(TMP_DIR.$f);
$_SESSION['tmpUpload'] = array();
}
}
function makePhotoPath($file) {
if(is_null($file) || $file=="") return NULL;
$path = PHOTOS_DIR.$file;
return file_exists($path) ? $path : NULL;
}
function makeThumbnailPath($file) {
if(is_null($file) || $file=="") return NULL;
$thumb_path = THUMBNAIL_DIR.$file;
if(file_exists($thumb_path)) return $thumb_path;
$photo_path = PHOTOS_DIR.$file;
if(!file_exists($photo_path)) return NULL;
// MAKE THUMBNAIL
$ext = getImgExtFromMimeType($photo_path);
if($ext == "jpg" || $ext == "png" || $ext == "webp") redimg($photo_path, $thumb_path, 400, 400, false, true, false, $ext);
return file_exists($thumb_path) ? $thumb_path : NULL;
}
function rotatePhoto($filepath, $angle, $save = false) {
$ext = (strtolower(substr($filepath,-3))=="png") ? "png" : "jpg";
$angle = 360-$angle;
if(!file_exists($filepath) || !is_file($filepath)) return false;
// PNG
if($ext == "png") {
$img = imagecreatefrompng($filepath);
imageAlphaBlending($img, true);
imageSaveAlpha($img, true);
$img = imagerotate($img, $angle, 0);
// DISPLAY
if(!$save) {
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
die();
}
// SAVE
else {
$dest = $save;
if($save == true) $dest = $filepath;
if(file_exists($dest) && is_file($dest)) unlink($dest);
imagepng($img, $dest);
imagedestroy($img);
return file_exists($dest);
}
}
// JPG
else {
$img = imagecreatefromjpeg($filepath);
$img = imagerotate($img, $angle, 0);
// DISPLAY
if(!$save) {
header('Content-Type: image/jpeg');
imagejpeg($img);
imagedestroy($img);
die();
}
// SAVE
else {
$dest = $save;
if($save == true) $dest = $filepath;
if(file_exists($dest) && is_file($dest)) unlink($dest);
imagejpeg($img, $dest);
imagedestroy($img);
return file_exists($dest);
}
}
}
?>