source: github/program/steps/addressbook/upload_photo.inc @ a71a97f

HEADcourier-fixdev-browser-capabilitiespdorelease-0.8
Last change on this file since a71a97f was a71a97f, checked in by alecpl <alec@…>, 14 months ago
  • Image resize with GD extension (#1488383)
  • Property mode set to 100644
File size: 3.9 KB
Line 
1<?php
2
3/*
4 +-----------------------------------------------------------------------+
5 | program/steps/addressbook/upload_photo.inc                            |
6 |                                                                       |
7 | This file is part of the Roundcube Webmail client                     |
8 | Copyright (C) 2005-2011, The Roundcube Dev Team                       |
9 |                                                                       |
10 | Licensed under the GNU General Public License version 3 or            |
11 | any later version with exceptions for skins & plugins.                |
12 | See the README file for a full license statement.                     |
13 |                                                                       |
14 | PURPOSE:                                                              |
15 |   Handles contact photo uploads                                       |
16 |                                                                       |
17 +-----------------------------------------------------------------------+
18 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
19 +-----------------------------------------------------------------------+
20
21 $Id$
22
23*/
24
25// Supported image format types
26// ImageMagick works with other non-image types (e.g.pdf) we don't want here
27$IMAGE_TYPES = explode(',', 'jpeg,jpg,jp2,tiff,tif,bmp,eps,gif,png,png8,png24,png32,svg,ico');
28
29// clear all stored output properties (like scripts and env vars)
30$OUTPUT->reset();
31
32if ($filepath = $_FILES['_photo']['tmp_name']) {
33    // check file type and resize image
34    $image     = new rcube_image($_FILES['_photo']['tmp_name']);
35    $imageprop = $image->props();
36
37    if (in_array(strtolower($imageprop['type']), $IMAGE_TYPES)
38        && $imageprop['width'] && $imageprop['height']
39    ) {
40        $maxsize   = intval($RCMAIL->config->get('contact_photo_size', 160));
41        $tmpfname  = tempnam($RCMAIL->config->get('temp_dir'), 'rcmImgConvert');
42        $save_hook = 'attachment_upload';
43
44        // scale image to a maximum size
45        if (($imageprop['width'] > $maxsize || $imageprop['height'] > $maxsize) && $image->resize($maxsize, $tmpfname)) {
46            $filepath  = $tmpfname;
47            $save_hook = 'attachment_save';
48        }
49
50        // save uploaded file in storage backend
51        $attachment = $RCMAIL->plugins->exec_hook($save_hook, array(
52            'path' => $filepath,
53            'size' => $_FILES['_photo']['size'],
54            'name' => $_FILES['_photo']['name'],
55            'mimetype' => 'image/' . $imageprop['type'],
56            'group' => 'contact',
57        ));
58    }
59    else {
60        $attachment['error'] = rcube_label('invalidimageformat');
61    }
62
63    if ($attachment['status'] && !$attachment['abort']) {
64        $file_id = $attachment['id'];
65        $_SESSION['contacts']['files'][$file_id] = $attachment;
66        $OUTPUT->command('replace_contact_photo', $file_id);
67    }
68    else {  // upload failed
69        $err = $_FILES['_photo']['error'];
70        if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE)
71            $msg = rcube_label(array('name' => 'filesizeerror', 'vars' => array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize'))))));
72        else if ($attachment['error'])
73            $msg = $attachment['error'];
74        else
75            $msg = rcube_label('fileuploaderror');
76           
77        $OUTPUT->command('display_message', $msg, 'error');
78    }
79}
80else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
81    // if filesize exceeds post_max_size then $_FILES array is empty,
82    // show filesizeerror instead of fileuploaderror
83    if ($maxsize = ini_get('post_max_size'))
84        $msg = rcube_label(array('name' => 'filesizeerror', 'vars' => array('size' => show_bytes(parse_bytes($maxsize)))));
85    else
86        $msg = rcube_label('fileuploaderror');
87
88    $OUTPUT->command('display_message', $msg, 'error');
89}
90
91$OUTPUT->command('photo_upload_end');
92$OUTPUT->send('iframe');
Note: See TracBrowser for help on using the repository browser.