Files
ms1inscription-v5/superadm/php/inc_files.php
2026-05-13 09:43:32 -04:00

193 lines
6.7 KiB
PHP

<?php
//phpinfo();
//fonction pour téléverser un fichier
//param: nom du fichier, nom tempo du fichier téléversé, chemin du dossier
//retourne: nouveau nom du fichier
function fxUploadFile($strName, $strTmp, $strPath) {
// get the file extension first
$strExt = strtolower(substr(strrchr($strName, "."), 1));
$strFilename = str_replace('.' . $strExt, '', strtolower(basename($strName)));
$strNewName = fxRenameFile($strFilename, $strExt, '');
$strTargetPath = $strPath . $strNewName;
$moved =move_uploaded_file($strTmp, $strTargetPath);
if( $moved ) {
//echo($strTargetPath);
} else {
// echo "Not uploaded because of error #".$_FILES["file"]["error"];
}
return $strNewName;
}
//fonction pour renommer un fichier à uploader
//param: nom du fichier, extension du fichier, préfixe
//retourne le nom du fichier renommée
function fxRenameFile($strName, $strExt, $strPrefixe) {
$intRandom = rand(0000, 9999);
$strSearch = array(" ", ",", "à", "â", "ä", "é", "è", "ê", "ë", "ì", "î", "ï", "ò", "ô", "ö", "ù", "û", "ü", "ç", "-", "'");
$strReplace = array("_", "_", "a", "a", "a", "e", "e", "e", "e", "i", "i", "i", "o", "o", "o", "u", "u", "u", "c", "_", "_");
$strNewName = str_replace($strSearch, $strReplace, $strName);
$strNewName .= '_' . $intRandom . '.' . $strExt;
if ($strPrefixe != '')
$strNewName = $strPrefixe . '_' . $strNewName;
$strNewName = strtolower($strNewName);
return $strNewName;
}
//fonction pour redimensionner les images
//param: nom de l'image (sans extension), extension de l'image, chemin de l'image, largeur, hauteur, cropper l'image ?, qualité (en %), suffixe
//retourne le nom de l'image redimensionnée
function fxResizeImage($strImage, $strPath, $intWidth, $intHeight, $intCrop, $intQuality, $strSuffixe) {
// get the file extension first
$strExt = strtolower(substr(strrchr($strImage, "."), 1));
$strImage = str_replace('.' . $strExt, '', strtolower(basename($strImage)));
// Get the size and MIME type of the requested image
$tabSize = getimagesize($strPath . $strImage . '.' . $strExt);
$strMime = $tabSize['mime'];
$intOriginalWidth = $tabSize[0];
$intOriginalHeight = $tabSize[1];
$intMaxWidth = $intWidth;
$intMaxHeight = $intHeight;
// Ratio cropping
$intOffsetX = 0;
$intOffsetY = 0;
if (($intOriginalWidth > $intMaxWidth) || ($intOriginalHeight > $intMaxHeight)) {
if ($intCrop === 1) {
$fltRatioComputed = $intOriginalWidth / $intOriginalHeight;
$intThumbWidth = $intMaxWidth;
$intThumbHeight = $intMaxHeight;
if ($intMaxWidth != $intMaxHeight)
$fltCropRatio = $intMaxWidth / $intMaxHeight;
else
$fltCropRatio = 1;
if ($fltRatioComputed < $fltCropRatio) { // Image is too tall so we will crop the top and bottom
$intOriginalHeight1 = $intOriginalHeight;
$intOriginalHeight = $intOriginalWidth / $fltCropRatio;
$intOffsetY = ($intOriginalHeight1 - $intOriginalHeight) / 2;
}
elseif ($fltRatioComputed > $fltCropRatio) { // Image is too wide so we will crop off the left and right sides
$intOriginalWidth1 = $intOriginalWidth;
$intOriginalWidth = $intOriginalHeight * $fltCropRatio;
$intOffsetX = ($intOriginalWidth1 - $intOriginalWidth) / 2;
}
}
else {
// Setting up the ratios needed for resizing. We will compare these below to determine how to
// resize the image (based on height or based on width)
$fltRatioX = $intMaxWidth / $intOriginalWidth;
$fltRatioY = $intMaxHeight / $intOriginalHeight;
if ($fltRatioX * $intOriginalHeight < $intMaxHeight)
{ // Resize the image based on width
$intThumbHeight = ceil($fltRatioX * $intOriginalHeight);
$intThumbWidth = $intMaxWidth;
}
else { // Resize the image based on height
$intThumbWidth = ceil($fltRatioY * $intOriginalWidth);
$intThumbHeight = $intMaxHeight;
}
}
// We don't want to run out of memory
// Set up a blank canvas for our resized image (destination)
$strDestination = imagecreatetruecolor($intThumbWidth, $intThumbHeight);
// Set up the appropriate image handling functions based on the original image's mime type
switch ($tabSize['mime']) {
case 'image/gif':
// We will be converting GIFs to PNGs to avoid transparency issues when resizing GIFs
// This is maybe not the ideal solution, but IE6 can suck it
$strCreationFunction = 'imagecreatefromgif';
$strOutputFunction = 'imagepng';
$strMime = 'image/png'; // We need to convert GIFs to PNGs
$intQuality = round(10 - ($intQuality / 10)); // We are converting the GIF to a PNG and PNG needs a compression level of 0 (no compression) through 9
$blnDoSharpen = false;
break;
case 'image/x-png':
case 'image/png':
$strCreationFunction = 'imagecreatefrompng';
$strOutputFunction = 'imagepng';
$intQuality = round(10 - ($intQuality / 10)); // PNG needs a compression level of 0 (no compression) through 9
$blnDoSharpen = false;
break;
default:
$strCreationFunction = 'imagecreatefromjpeg';
$strOutputFunction = 'imagejpeg';
$blnDoSharpen = true;
break;
}
// Read in the original image
$strSource = $strCreationFunction($strPath . $strImage . '.' . $strExt);
$strResized = $strPath . $strImage . '_' . $strSuffixe . '.' . $strExt;
if (in_array($tabSize['mime'], array('image/gif', 'image/png'))) {
// If this is a GIF or a PNG, we need to set up transparency
imagealphablending($strDestination, false);
imagesavealpha($strDestination, true);
}
// Resample the original image into the resized canvas we set up earlier
imagecopyresampled($strDestination, $strSource, 0, 0, $intOffsetX, $intOffsetY, $intThumbWidth, $intThumbHeight, $intOriginalWidth, $intOriginalHeight);
if ($blnDoSharpen) {
// Sharpen the image based on two things:
// (1) the difference between the original size and the final size
// (2) the final size
$intSharpness = findsharp($intOriginalWidth, $intThumbWidth);
$tabSharpenMatrix = array(
array(-1, -2, -1),
array(-2, $intSharpness + 12, -2),
array(-1, -2, -1)
);
$intDivisor = $intSharpness;
$intOffset = 0;
imageconvolution($strDestination, $tabSharpenMatrix, $intDivisor, $intOffset);
}
// Write the resized image to the cache
$strOutputFunction($strDestination, $strResized, $intQuality);
// Clean up the memory
imagedestroy($strSource);
imagedestroy($strDestination);
return str_replace($strPath, '', $strResized);
}
else
return $strImage . '.' . $strExt;
}
// function from Ryan Rud (http://adryrun.com)
function findSharp($orig, $final) {
$final = $final * (750.0 / $orig);
$a = 52;
$b = -0.27810650887573124;
$c = .00047337278106508946;
$result = $a + $b * $final + $c * $final * $final;
return max(round($result), 0);
}
?>