summaryrefslogtreecommitdiff
path: root/model/Image.php
blob: d77cfb1a2d4f4e14d0a30c6ae3ca4d59a1388aa5 (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
// model/Image.php

class Image
{
	private $page;
	private $folder;

	private $ajax; // vaut true avec le ckeditor
	public $reponseAjax;

	public $path;
	public $pathMini;
	public $FileName; // après sécurisation
	public $permissions;
	public $width;
	public $pathInfos;
	public $erreur;

	public function __construct(string $page, string $folder, bool $ajax, int $permissions = 0666)
	{
		$this->ajax = $ajax;
		$this->page = $page;
		$this->folder = $folder;
		$this->permissions = $permissions;
		$this->path = 'data/' . $this->folder . '/images/';
		$this->pathMini = 'data/' . $this->folder . '/images-mini/';
	}

	// GET
	
	// SET
	public function setFolder(string $folder)
	{
		$this->folder = $folder;
		$this->path = 'data/' . $this->folder . '/images/';
		$this->pathMini = 'data/' . $this->folder . '/images-mini/';
	}

	public function setFileName($fileName)
	{
		$this->FileName = $fileName;
	}

	public function setThumbnailWidth($width)
	{
		$this->width = $width;
	}

	public function upload()
    {
		// traitement et enregistrement de l'image
		if (isset($_FILES['upload']) AND $_FILES['upload']['error'] == 0) // 0 signifie ok
		{
	        //$this->pathInfos = pathinfo($_FILES['upload']['name']);
	        $this->pathInfos = pathinfo($this->FileName);

	        $extension = $this->pathInfos['extension'];
	        $extautorisées = array('jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'tiff');
	        // on prend la même liste que celle côté javascript, le SVG est bloqué pour raison de sécurité (javascript à l'intérieur)
	        if (in_array($extension, $extautorisées))
	        {
	            move_uploaded_file($_FILES['upload']['tmp_name'], $this->path . $this->FileName);
	            chmod($this->path . $this->FileName, $this->permissions);
	        }
	        else
	        {
	        	$this->erreur = 'mauvais format, veuillez utiliser une image comportant un de ces formats: jpg ou jpeg, png, gif, bmp, webp, tiff<br />le format svg n\'est pas supporté';
	        }
		}
		else
		{
		    $this->erreur = 'Erreur: Le fichier n\'a pas pu être téléchargé correctement.<br/>
				Au fait, "upload_max_filesize" dans le php.ini vaut ' . ini_get('upload_max_filesize') . '.';
		}

		// retour des rêquetes AJAX
		if($this->ajax && empty($Image->erreur))
		{
			// chemin en JSON attendu par l'éditeur
			$this->reponseAjax = '{"url": "data/' . $this->folder . '/images/' . $this->FileName . '"}';
		}
    }

    // miniatures des photos
    public function makeThumbnail()
    {
    	global $imageLibrary;

    	if($imageLibrary == 'imagick')
    	{
    		$Image = new Imagick($this->path . $this->FileName);

    		$source = $Image->getImageGeometry();
    		if($source['width'] > $this->width)
    		{
    			// 0 signifie qu'on conserve les proportions
    			$Image->thumbnailImage($this->width, 0);
    		}

    		// écriture dans un fichier
    		$nomMiniImage = $this->pathMini . $this->pathInfos['filename'] . '-mini.' . $this->pathInfos['extension'];
    		$Image->writeImage($nomMiniImage);
    		chmod($nomMiniImage, $this->permissions);
    	}
    	elseif($imageLibrary == 'gd')
    	{
	    	// cette fonction fonctionne pour tous les formats
	    	$source = imagecreatefromstring(file_get_contents($this->path . $this->FileName));
	    	// on créera un jpg quelque soit l'image d'origine
	    	// nécessite une correction du html dans Article::makeHtmlMiniImages()
	    	$nomMiniImage = $this->pathMini . $this->pathInfos['filename'] . '-mini.jpg';
			$forme = imagesy($source) / imagesx($source);

	    	if(imagesx($source) > $this->width)
	    	{
	    		// créer un rectangle noir
	    		$destination = imagecreatetruecolor($this->width, $this->width * $forme);

	    		// sélectionne un rectangle dans l'image source
	    		// et le place dans un rectangle dans la nouvelle
	    		imagecopyresampled($destination, $source, 0, 0, 0, 0, $this->width, $this->width * $forme, imagesx($source), imagesy($source));

	    		// envoie l'image dans un fichier
	    		imagejpeg($destination, $nomMiniImage);
	    	}
	    	else
	    	{
	    		imagejpeg($source, $nomMiniImage);
	    	}
	    	chmod($nomMiniImage, $this->permissions);
    	}
    	else
    	{
    		// utiliser la grande image si il est impossible de créer une miniature

    		// message d'erreur
    		$_SESSION['erreur'] = addslashes("Echec de la création d'une miniature. Vérifier le fichier config.php");
    		header('Location: index.php?page=' . $this->folder . '&erreur=dependance_bibli_images');
            exit();
    	}
    }
}