Skip to content
Extraits de code Groupes Projets
verifier_taille_document_acceptable.php 5,5 ko
Newer Older
JamesRezo's avatar
JamesRezo a validé
/**
 * SPIP, Système de publication pour l'internet
 *
 * Copyright © avec tendresse depuis 2001
 * Arnaud Martin, Antoine Pitrou, Philippe Rivière, Emmanuel Saint-James
 *
 * Ce programme est un logiciel libre distribué sous licence GNU/GPL.
 */

/**
 * Gestion des vignettes de types de fichier
 *
 * @package SPIP\Medias\Vignette
JamesRezo's avatar
JamesRezo a validé
 */
if (!defined('_ECRIRE_INC_VERSION')) {
	return;
}

/**
 * Verifier si le fichier respecte les contraintes de tailles
 *
 * @param  array $infos
 * @param  bool $is_logo
 * @return bool|mixed|string
 */
function inc_verifier_taille_document_acceptable_dist(&$infos, $is_logo = false) {

	// si ce n'est pas une image
	if (!$infos['type_image']) {
		$max_size = (defined('_DOC_MAX_SIZE') && _DOC_MAX_SIZE) ? _DOC_MAX_SIZE : null;
cerdic's avatar
cerdic a validé
		$res = medias_verifier_poids_fichier($infos, $max_size, false);
		if ($res !== true) {
			return $res;
		// Ne pas contraindre les dimensions des images vectorielles, ça n'a pas de sens
		if ($infos['type_image'] !== 'svg') {
			if ($is_logo) {
				$max_width = (defined('_LOGO_MAX_WIDTH') && _LOGO_MAX_WIDTH) ? _LOGO_MAX_WIDTH : null;
				$max_height = (defined('_LOGO_MAX_HEIGHT') && _LOGO_MAX_HEIGHT) ? _LOGO_MAX_HEIGHT : null;
				$min_width = (defined('_LOGO_MIN_WIDTH') && _LOGO_MIN_WIDTH) ? _LOGO_MIN_WIDTH : null;
				$min_height = (defined('_LOGO_MIN_HEIGHT') && _LOGO_MIN_HEIGHT) ? _LOGO_MIN_HEIGHT : null;
JamesRezo's avatar
JamesRezo a validé
			} else {
				$max_width = (defined('_IMG_MAX_WIDTH') && _IMG_MAX_WIDTH) ? _IMG_MAX_WIDTH : null;
				$max_height = (defined('_IMG_MAX_HEIGHT') && _IMG_MAX_HEIGHT) ? _IMG_MAX_HEIGHT : null;
				$min_width = (defined('_IMG_MIN_WIDTH') && _IMG_MIN_WIDTH) ? _IMG_MIN_WIDTH : null;
				$min_height = (defined('_IMG_MIN_HEIGHT') && _IMG_MIN_HEIGHT) ? _IMG_MIN_HEIGHT : null;
			$res = medias_verifier_largeur_hauteur_image($infos, $max_width, $max_height, $min_width, $min_height);
			if ($res !== true) {
				return $res;
			}
			$max_size = (defined('_LOGO_MAX_SIZE') && _LOGO_MAX_SIZE) ? _LOGO_MAX_SIZE : null;
JamesRezo's avatar
JamesRezo a validé
		} else {
			$max_size = (defined('_IMG_MAX_SIZE') && _IMG_MAX_SIZE) ? _IMG_MAX_SIZE : null;
cerdic's avatar
cerdic a validé
		$res = medias_verifier_poids_fichier($infos, $max_size, true);
		if ($res !== true) {
			return $res;
/**
 * Verifier largeur maxi et hauteur maxi d'une image
 * @param array $infos
 * @param null|int $max_width
 * @param null|int $max_height
 * @param null|int $min_width
 * @param null|int $min_height
 * @return bool|string
 */
JamesRezo's avatar
JamesRezo a validé
function medias_verifier_largeur_hauteur_image(
	&$infos,
	$max_width = null,
	$max_height = null,
	$min_width = null,
	$min_height = null
) {
		$max_width && $infos['largeur'] > $max_width || $max_height && $infos['hauteur'] > $max_height
	) {
		// pas la peine d'embeter le redacteur avec ca si on a active le calcul des miniatures
		// on met directement a la taille maxi a la volee
		if (isset($GLOBALS['meta']['creer_preview']) && $GLOBALS['meta']['creer_preview'] == 'oui') {
			include_spip('inc/filtres');
			$img = filtrer('image_reduire', $infos['fichier'], $max_width ?: '*', $max_height ?: '*');
			$img = extraire_attribut($img, 'src');
			$img = supprimer_timestamp($img);
			if (@file_exists($img) && $img !== $infos['fichier']) {
				spip_unlink($infos['fichier']);
				@rename($img, $infos['fichier']);
				[$h, $w] = taille_image($infos['fichier'], true);
				$infos['largeur'] = $w;
				$infos['hauteur'] = $h;
				$infos['taille'] = @filesize($infos['fichier']);
			}
		}

			$max_width && $infos['largeur'] > $max_width || $max_height && $infos['hauteur'] > $max_height
		) {
			return _T(
				'medias:info_image_max_taille',
					'maxi' =>
						_T(
							'info_largeur_vignette',
JamesRezo's avatar
JamesRezo a validé
								'hauteur_vignette' => $max_height ?? '∞',
						),
					'actuel' =>
						_T(
							'info_largeur_vignette',
								'largeur_vignette' => $infos['largeur'],
JamesRezo's avatar
JamesRezo a validé
								'hauteur_vignette' => $infos['hauteur'],
JamesRezo's avatar
JamesRezo a validé
						),
		$min_width && $infos['largeur'] < $min_width || $min_height && $infos['hauteur'] < $min_height
		if ($min_width && $max_width && $min_width > $max_width) {
JamesRezo's avatar
JamesRezo a validé
			spip_log(
				'Constantes invalides détectées, modifiez votre fichier de configuration (_IMG_MIN_WIDTH > _IMG_MAX_WIDTH)',
				'medias' . _LOG_INFO_IMPORTANTE
			);
		if ($min_height && $max_height && $min_height > $max_height) {
JamesRezo's avatar
JamesRezo a validé
			spip_log(
				'Constantes invalides détectées, modifiez votre fichier de configuration (_IMG_MIN_HEIGHT > _IMG_MAX_HEIGHT)',
				'medias' . _LOG_INFO_IMPORTANTE
			);
		return _T(
			'medias:info_image_min_taille',
JamesRezo's avatar
JamesRezo a validé
							'hauteur_vignette' => $min_height ?? '0',
							'largeur_vignette' => $infos['largeur'],
JamesRezo's avatar
JamesRezo a validé
							'hauteur_vignette' => $infos['hauteur'],
JamesRezo's avatar
JamesRezo a validé
					),
}

/**
 * verifier le poids maxi d'une image
 * @param array $infos
 * @param null|int $max_size
 * @return bool|string
 */
cerdic's avatar
cerdic a validé
function medias_verifier_poids_fichier($infos, $max_size = null, $is_image = false) {
	if ($max_size && $infos['taille'] > $max_size * 1024) {
			$is_image ? 'medias:info_image_max_poids' : 'medias:info_doc_max_poids',
				'maxi' => taille_en_octets($max_size * 1024),
JamesRezo's avatar
JamesRezo a validé
				'actuel' => taille_en_octets($infos['taille']),