fix the pdf code with a twisted way to do that.
[platal.git] / include / validations / photos.inc.php
... / ...
CommitLineData
1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22// {{{ class PhotoReq
23
24class PhotoReq extends Validate
25{
26 // {{{ properties
27
28 var $mimetype;
29 var $data;
30 var $x;
31 var $y;
32
33 var $unique = true;
34
35 var $rules = "Refuser les photos copyrightées, de mineurs, ou ayant
36 un caractère pornographique, violent, etc... Si une photo est mal
37 cadrée (20% de photo et 80% de blanc par exemple), si c'est un
38 camarade antique, on lui arrange sinon on lui
39 refuse en lui expliquant gentiment le problème. Idem si les dimensions de
40 la photo sont archi trop grandes ou archi trop petites.";
41
42 // }}}
43 // {{{ constructor
44
45 function PhotoReq($_uid, $_data, $_stamp=0)
46 {
47 $this->Validate($_uid, true, 'photo', $_stamp);
48 }
49
50 // }}}
51 // {{{ function _get_image()
52
53 function _get_image($_data)
54 {
55 global $page;
56
57 // calcul de la taille de l'image
58 require_once dirname(__FILE__).'/../../classes/varstream.php';
59 $GLOBALS['photoreq'] = $_data;
60 $image_infos = getimagesize('var://photoreq');
61 unset ($GLOBALS['photoreq']);
62
63 if (empty($image_infos)) {
64 $page->trig("Image invalide");
65 return false;
66 }
67 list($this->x, $this->y, $this->mimetype) = $image_infos;
68
69 switch ($this->mimetype) {
70 case 1: $this->mimetype = "gif"; break;
71 case 2: $this->mimetype = "jpeg"; break;
72 case 3: $this->mimetype = "png"; break;
73 default:
74 $page->trig("Type d'image invalide");
75 return false;
76 }
77
78 if (strlen($_data) > SIZE_MAX) {
79 $img = imagecreatefromstring($_data);
80 if (!$img) {
81 $page->trig("image trop grande et impossible à retailler automatiquement");
82 return false;
83 }
84
85 $nx = $x = imagesx($img);
86 $ny = $y = imagesy($img);
87
88 if ($nx > 240) { $ny = intval($ny*240/$nx); $nx = 240; }
89 if ($ny > 300) { $ny = intval($nx*300/$nx); $ny = 300; }
90 if ($nx < 160) { $ny = intval($ny*160/$nx); $nx = 160; }
91
92 $comp = '90';
93 $file = tempnam('/tmp', 'photo');
94
95 while (strlen($_data) > SIZE_MAX) {
96 $img2 = imagecreatetruecolor($nx, $ny);
97 imagecopyresampled($img2, $img, 0, 0, 0, 0, $nx, $ny, $x, $y);
98 imagejpeg($img2, $file, $comp);
99 $_data = file_get_contents($file);
100 $this->mimetype = 'jpeg';
101
102 $comp --;
103 }
104
105 unlink($file);
106 }
107 $this->data = $_data;
108 return true;
109 }
110
111 // }}}
112 // {{{ function get_request()
113
114 function get_request($uid)
115 {
116 return parent::get_request($uid,'photo');
117 }
118
119 // }}}
120 // {{{ function formu()
121
122 function formu()
123 { return 'include/form.valid.photos.tpl'; }
124
125 // }}}
126 // {{{ function editor()
127
128 function editor()
129 {
130 return 'include/form.valid.edit-photo.tpl';
131 }
132
133 // }}}
134 // {{{ function handle_editor()
135
136 function handle_editor()
137 {
138 if (isset($_FILES['userfile']['tmp_name'])) {
139 $file = $_FILES['userfile']['tmp_name'];
140 if ($data = file_get_contents($file)) {
141 if ($this->_get_image($data)) {
142 return true;
143 }
144 } else {
145 $page->trig('Fichier inexistant ou vide');
146 }
147 }
148 return false;
149 }
150
151 // }}}
152 // {{{ function _mail_subj
153
154 function _mail_subj()
155 {
156 return "[Polytechnique.org/PHOTO] Changement de photo";
157 }
158
159 // }}}
160 // {{{ function _mail_body
161
162 function _mail_body($isok)
163 {
164 if ($isok) {
165 return "Le changement de photo que tu as demandé vient d'être effectué.";
166 } else {
167 return "La demande de changement de photo que tu avais faite a été refusée.";
168 }
169 }
170
171 // }}}
172 // {{{ function commit()
173
174 function commit()
175 {
176 XDB::execute('REPLACE INTO photo (uid, attachmime, attach, x, y)
177 VALUES ({?},{?},{?},{?},{?})',
178 $this->uid, $this->mimetype, $this->data, $this->x, $this->y);
179 require_once('notifs.inc.php');
180 register_watch_op($this->uid,WATCH_FICHE);
181 return true;
182 }
183
184 // }}}
185}
186
187// }}}
188
189// vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
190?>