| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2004 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 | |
| 24 | class 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 | global $globals, $page; |
| 48 | |
| 49 | $this->Validate($_uid, true, 'photo', $_stamp); |
| 50 | |
| 51 | // calcul de la taille de l'image |
| 52 | require_once('xorg.varstream.inc.php'); |
| 53 | $GLOBALS['photoreq'] = $_data; |
| 54 | $image_infos = getimagesize('var://photoreq'); |
| 55 | unset ($GLOBALS['photoreq']); |
| 56 | |
| 57 | if (empty($image_infos)) { |
| 58 | $page->trig("Image invalide"); |
| 59 | return ($this = null); |
| 60 | } |
| 61 | list($this->x, $this->y, $this->mimetype) = $image_infos; |
| 62 | |
| 63 | switch ($this->mimetype) { |
| 64 | case 1: $this->mimetype = "gif"; break; |
| 65 | case 2: $this->mimetype = "jpeg"; break; |
| 66 | case 3: $this->mimetype = "png"; break; |
| 67 | default: |
| 68 | $page->trig("Type d'image invalide"); |
| 69 | return ($this = null); |
| 70 | } |
| 71 | |
| 72 | if (strlen($_data) > SIZE_MAX) { |
| 73 | $img = imagecreatefromstring($_data); |
| 74 | if (!$img) { |
| 75 | $page->trig("image trop grande et impossible à retailler automatiquement"); |
| 76 | return ($this = null); |
| 77 | } |
| 78 | |
| 79 | $nx = $x = imagesx($img); |
| 80 | $ny = $y = imagesy($img); |
| 81 | |
| 82 | if ($nx > 240) { $ny = intval($ny*240/$nx); $nx = 240; } |
| 83 | if ($ny > 300) { $ny = intval($nx*300/$nx); $ny = 300; } |
| 84 | if ($nx < 160) { $ny = intval($ny*160/$nx); $nx = 160; } |
| 85 | |
| 86 | $comp = '90'; |
| 87 | $file = tempnam('/tmp', 'photo'); |
| 88 | |
| 89 | while (strlen($_data) > SIZE_MAX) { |
| 90 | $img2 = imagecreatetruecolor($nx, $ny); |
| 91 | imagecopyresampled($img2, $img, 0, 0, 0, 0, $nx, $ny, $x, $y); |
| 92 | imagejpeg($img2, $file, $comp); |
| 93 | $_data = file_get_contents($file); |
| 94 | $this->mimetype = 'jpeg'; |
| 95 | |
| 96 | $comp --; |
| 97 | } |
| 98 | |
| 99 | unlink($file); |
| 100 | } |
| 101 | $this->data = $_data; |
| 102 | } |
| 103 | |
| 104 | // }}} |
| 105 | // {{{ function get_request() |
| 106 | |
| 107 | function get_request($uid) |
| 108 | { |
| 109 | return parent::get_request($uid,'photo'); |
| 110 | } |
| 111 | |
| 112 | // }}} |
| 113 | // {{{ function formu() |
| 114 | |
| 115 | function formu() |
| 116 | { return 'include/form.valid.photos.tpl'; } |
| 117 | |
| 118 | // }}} |
| 119 | // {{{ function _mail_subj |
| 120 | |
| 121 | function _mail_subj() |
| 122 | { |
| 123 | return "[Polytechnique.org/PHOTO] Changement de photo"; |
| 124 | } |
| 125 | |
| 126 | // }}} |
| 127 | // {{{ function _mail_body |
| 128 | |
| 129 | function _mail_body($isok) |
| 130 | { |
| 131 | if ($isok) { |
| 132 | return "Le changement de photo que tu as demandé vient d'être effectué."; |
| 133 | } else { |
| 134 | return "La demande de changement de photo que tu avais faite a été refusée."; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // }}} |
| 139 | // {{{ function commit() |
| 140 | |
| 141 | function commit() |
| 142 | { |
| 143 | global $globals; |
| 144 | |
| 145 | $globals->xdb->execute('REPLACE INTO photo (uid, attachmime, attach, x, y) |
| 146 | VALUES ({?},{?},{?},{?},{?})', |
| 147 | $this->uid, $this->mimetype, $this->data, $this->x, $this->y); |
| 148 | require_once('notifs.inc.php'); |
| 149 | register_watch_op($this->uid,WATCH_FICHE); |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | // }}} |
| 154 | } |
| 155 | |
| 156 | // }}} |
| 157 | |
| 158 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: |
| 159 | ?> |