Changeset 91 for img

Show
Ignore:
Timestamp:
05/22/10 12:28:43 (4 months ago)
Author:
daniel
Message:

- Added: Upload multiple images at once

Location:
img/trunk
Files:
1 added
4 modified

Legend:

Unmodified
Added
Removed
  • img/trunk/browse.php

    r82 r91  
    9191 
    9292} elseif(isset($_GET['user'])) { 
    93  
    9493        // Calculate page offset 
    9594        $page = (isset($_GET['p']) && is_numeric($_GET['p'])) ? (int)$_GET['p'] : 1; 
     
    137136        outputHTML('<h2>' . htmlentities(one_wordwrap(urldecode($_GET['user']), 5, '&shy;'), ENT_QUOTES, 'UTF-8', false) . '</h2>' . $images . '<br style="clear: both;" />' . $pages, array('title' => 'Tag: ' . htmlentities($_GET['user'], ENT_QUOTES, 'UTF-8'), 'lightbox' => true)); 
    138137         
     138} elseif(isset($_GET['ip']) && isset($_GET['time'])) { 
     139        $sql = "SELECT 
     140 ROWID as id, 
     141 location as name, 
     142 original_name 
     143FROM 
     144 images 
     145WHERE 
     146 ip = '" . $db->escape($_GET['ip']) . "' and 
     147 time = '" . $db->escape($_GET['time']) . "';"; 
     148 
     149        $res = $db->query($sql); 
     150        $images = ''; 
     151        // Generate HTML output 
     152        while ($row = $db->fetch($res)) { 
     153                $preview = dirname($row['name']) . '/preview/' . basename($row['name']); 
     154                $images .= '<div class="previewimage"><a href="' . $row['name'] . '" class="lightbox" rel="lightbox"><img src="' . $preview . '" alt="' . htmlentities($row['original_name'], ENT_QUOTES, 'UTF-8') . '" /></a><br />' . "\n"; 
     155                $images .= '<a href="image.php?i=' . urlnumber_encode($row['id']) . '">Show</a></div>' . "\n"; 
     156        } 
     157         
     158        outputHTML('<h2>' . htmlentities(one_wordwrap(urldecode($_GET['ip'] . ' - ' . $_GET['time']), 5, '&shy;'), ENT_QUOTES, 'UTF-8', false) . '</h2>' . $images . '<br style="clear: both;" />', array('title' => 'Upload: ' . htmlentities($_GET['ip'] . ' - ' . $_GET['time'], ENT_QUOTES, 'UTF-8'), 'lightbox' => true)); 
    139159} else { 
    140160 
  • img/trunk/index.php

    r67 r91  
    4444        }); 
    4545}); 
     46 
     47$(document).ready(function () { 
     48        $('#addimage').click(function() { 
     49                $('#addimage').remove(); 
     50                $('#inputimagecontainer').append('<span class=\"text\">&nbsp;</span><input type=\"file\" size=\"39\" name=\"image[]\" />&nbsp;' + 
     51                        '<img src=\"images/add.png\" id=\"addimage\" alt=\"Add another image\" title=\"Add another image\" /><br /><br />'); 
     52                $('#addimage').click(arguments.callee); 
     53        }); 
     54}); 
    4655</script>"; 
    4756 
     
    4958                        <div> 
    5059                        <input type="hidden" name="MAX_FILE_SIZE" value="' . $maxsize.'" /> 
    51                         <span class="text">File:</span><input type="file" size="40" name="image" /><br /><br /> 
    52                         <span class="text">Tags:</span><input id="inputtags" type="text" size="40" name="tags" /> 
     60                        <div id="inputimagecontainer"> 
     61                        <span class="text">File:</span><input type="file" size="39" name="image[]" />&nbsp; 
     62                        <img src="images/add.png" id="addimage" alt="Add another image" title="Add another image" /><br /><br /> 
     63                        </div> 
     64                        <span class="text">Tags:</span><input id="inputtags" type="text" size="39" name="tags" /> 
    5365                        <span class="text">&nbsp;</span><input id="submit" type="submit" name="submit" value="Upload" /> 
    5466                        <p id="info"> 
  • img/trunk/lib/functions.php

    r85 r91  
    338338} 
    339339 
    340 ?> 
     340/** 
     341 * Fixes the odd indexing of multiple file uploads from the format: 
     342 * 
     343 * $_FILES['field']['key']['index'] 
     344 * 
     345 * To the more standard and appropriate: 
     346 * 
     347 * $_FILES['field']['index']['key'] 
     348 * 
     349 * @param array $files 
     350 * @author Corey Ballou 
     351 * @link http://www.jqueryin.com 
     352 */ 
     353function fixFilesArray(&$files) 
     354{ 
     355        $names = array( 'name' => 1, 'type' => 1, 'tmp_name' => 1, 'error' => 1, 'size' => 1); 
     356 
     357        foreach ($files as $key => $part) { 
     358                // only deal with valid keys and multiple files 
     359                $key = (string) $key; 
     360                if (isset($names[$key]) && is_array($part)) { 
     361                        foreach ($part as $position => $value) { 
     362                                $files[$position][$key] = $value; 
     363                        } 
     364                        // remove old key reference 
     365                        unset($files[$key]); 
     366                } 
     367        } 
     368} 
     369 
     370?> 
  • img/trunk/upload.php

    r81 r91  
    3838 * ["size"]             - Size in bytes 
    3939 */ 
    40 $img = $_FILES['image']; 
     40header('Content-type: text/plain'); 
     41fixFilesArray($_FILES['image']); 
     42$time = time(); 
     43$uploadcount = 0; 
    4144 
    42 // Upload failed 
    43 if ($img['error']) { 
    44         unlink_safe($img['tmp_name']); 
    45         errorMsg('Image uplaod error.'); 
     45foreach ($_FILES['image'] as $img) { 
     46        // Upload failed 
     47        if ($img['error']) { 
     48                unlink_safe($img['tmp_name']); 
     49                continue; 
     50                //errorMsg('Image uplaod error.'); 
     51        } 
     52 
     53        // The image is to big 
     54        if ($img['size'] > $maxsize) { 
     55                unlink_safe($img['tmp_name']); 
     56                continue; 
     57                //errorMsg('Image too big.'); 
     58        } 
     59 
     60        /* 
     61         * [0]                  - width 
     62         * [1]                  - geight 
     63         * [2]                  - IMAGETYPE_XXX 
     64         + [3]                  - Text string with width and height 
     65         * ["bits"] 
     66         * ["channels"] 
     67         * ["mime"]             - Mime type 
     68         */ 
     69        $info = getimagesize($img['tmp_name']); 
     70 
     71        // Check if this type of immage is allowed 
     72        if (!isset($mime[$info['mime']])) { 
     73                unlink_safe($img['tmp_name']); 
     74                continue; 
     75                //errorMsg('Imagetype not allowed.'); 
     76        } 
     77 
     78        // Assign the correct extension for this image 
     79        $name = (get_magic_quotes_gpc()) ? stripslashes($img['name']) : $img['name']; 
     80        $name = str_replace('\'', '', $name); 
     81        $name = explode('.', $name); 
     82 
     83        if(count($name) < 2) { 
     84                $name = $name[0] . '.' . $mime[$info['mime']]; 
     85        } else { 
     86                $name[count($name) - 1] = $mime[$info['mime']]; 
     87                $name = implode('.', $name); 
     88        } 
     89 
     90        // Choose the location for the file 
     91        $name = trim(str_replace('//', '/', checkExists($imgdir . '/' . $name))); 
     92         
     93        // Generate a URL save string to send to the browser 
     94        $location = explode('/', $name); 
     95        for ($i = 0; $i < count($location); $i++) { 
     96                $location[$i] = rawurlencode($location[$i]); 
     97        } 
     98        $location = implode('/', $location); 
     99         
     100        // Move the file to it's new location 
     101        if (!move_uploaded_file_save($img['tmp_name'], $name)) { 
     102                unlink_safe($img['tmp_name']); 
     103                errorMsg('Can\'t move uploaded file.'); 
     104        } 
     105 
     106        /* 
     107         * Create preview 
     108         *  
     109         * We use imagemagick because it suports a broad range of file 
     110         * types 
     111         *  
     112         * Also, we call it directly with exec 
     113         *  
     114         * See http://www.imagemagick.org/Usage/thumbnails/ for more 
     115         * information about the commands used 
     116         */ 
     117        $preview = dirname($name) . '/preview/' . basename($name); 
     118        if (!file_exists(dirname($preview))) mkdir(dirname($preview)); 
     119        exec('convert -define jpeg:size=' . $preview_width * 2 . 'x' . $preview_height * 2 . ' \\ 
     120          \'' . $name . '\'[0] -thumbnail ' . $preview_width . 'x' . $preview_height . ' \\ 
     121         -unsharp 0x.5 \'' . $preview . '\''); 
     122 
     123        $user = (isset($_SESSION['openid_identity'])) ? $_SESSION['openid_identity'] : ''; 
     124         
     125        // Save image info 
     126        $db->exec("INSERT INTO images ( 
     127         location, 
     128         path, 
     129         ip, 
     130         time, 
     131         original_name, 
     132         user 
     133        ) VALUES ( 
     134         '" . $db->escape($location) . "', 
     135         '" . $db->escape($name) . "', 
     136         '" . ip2long($_SERVER['REMOTE_ADDR']) . "', 
     137         '" . $time . "', 
     138         '" . $db->escape($img['name']) . "', 
     139         '" . $db->escape($user) . "' 
     140        );" ); 
     141        $res = $db->query("SELECT last_insert_rowid() as id;"); 
     142        $row = $db->fetch($res); 
     143        $id = $row['id']; 
     144 
     145        /* 
     146         * Tags 
     147         */ 
     148        if (isset($_POST['tags'])) { 
     149                $tags = explode(',', $_POST['tags']); 
     150                for ($i = 0; $i < count($tags); $i++) { 
     151                        $tags[$i] = trim($tags[$i]); 
     152                } 
     153                $tags = array_unique($tags); 
     154                $sql = "BEGIN;\n"; 
     155                foreach ($tags as $tag) { 
     156                        if (empty($tag)) continue; 
     157                        // check if the taga already exists 
     158                        $row = $db->fetch($db->query("SELECT ROWID as id FROM tags WHERE tag = '" . $db->escape(strtolower($tag)) . "'")); 
     159                        if (!$row) { 
     160                                $db->exec("INSERT INTO tags (tag, text) VALUES ('" . $db->escape(strtolower($tag)) . "', '" . $db->escape($tag) . "');"); 
     161                                $row = $db->fetch($db->query("SELECT last_insert_rowid() as id;")); 
     162                        } 
     163                        // Save the tag for this image and update tag counter 
     164                        $sql .= "INSERT INTO imagetags (image, tag) VALUES('" . $id . "', '" . $row['id'] . "');\n"; 
     165                        $sql .= "UPDATE tags SET count = count + 1 WHERE ROWID = '" . $row['id'] . "';\n"; 
     166                } 
     167                $sql .= "COMMIT;"; 
     168                // Commit all changes 
     169                $db->exec($sql); 
     170        } 
     171        $uploadcount++; 
    46172} 
    47173 
    48 // The image is to big 
    49 if ($img['size'] > $maxsize) { 
    50         unlink_safe($img['tmp_name']); 
    51         errorMsg('Image too big.'); 
     174if ($uploadcount > 0) { 
     175        // Redirect to image 
     176        header('Location: ' . url() . 'browse.php?ip=' . ip2long($_SERVER['REMOTE_ADDR']) . '&time=' . $time); 
     177        errorMsg('Image saved.<br /><br /><a href="' . url() . 'browse.php?ip=' . ip2long($_SERVER['REMOTE_ADDR']) . '&time=' . $time . '">Continue</a>'); 
     178} else { 
     179        errorMsg('No images uploaded.'); 
    52180} 
    53181 
    54 /* 
    55  * [0]                  - width 
    56  * [1]                  - geight 
    57  * [2]                  - IMAGETYPE_XXX 
    58  + [3]                  - Text string with width and height 
    59  * ["bits"] 
    60  * ["channels"] 
    61  * ["mime"]             - Mime type 
    62  */ 
    63 $info = getimagesize($img['tmp_name']); 
    64  
    65 // Check if this type of immage is allowed 
    66 if (!isset($mime[$info['mime']])) { 
    67         unlink_safe($img['tmp_name']); 
    68         errorMsg('Imagetype not allowed.'); 
    69 } 
    70  
    71 // Assign the correct extension for this image 
    72 $name = (get_magic_quotes_gpc()) ? stripslashes($img['name']) : $img['name']; 
    73 $name = str_replace('\'', '', $name); 
    74 $name = explode('.', $name); 
    75  
    76 if(count($name) < 2) { 
    77         $name = $name[0] . '.' . $mime[$info['mime']]; 
    78 } else { 
    79         $name[count($name) - 1] = $mime[$info['mime']]; 
    80         $name = implode('.', $name); 
    81 } 
    82  
    83 // Choose the location for the file 
    84 $name = trim(str_replace('//', '/', checkExists($imgdir . '/' . $name))); 
    85  
    86 // Generate a URL save string to send to the browser 
    87 $location = explode('/', $name); 
    88 for ($i = 0; $i < count($location); $i++) { 
    89         $location[$i] = rawurlencode($location[$i]); 
    90 } 
    91 $location = implode('/', $location); 
    92  
    93 // Move the file to it's new location 
    94 if (!move_uploaded_file_save($img['tmp_name'], $name)) { 
    95         unlink_safe($img['tmp_name']); 
    96         errorMsg('Can\'t move uploaded file.'); 
    97 } 
    98  
    99 /* 
    100  * Create preview 
    101  *  
    102  * We use imagemagick because it suports a broad range of file 
    103  * types 
    104  *  
    105  * Also, we call it directly with exec 
    106  *  
    107  * See http://www.imagemagick.org/Usage/thumbnails/ for more 
    108  * information about the commands used 
    109  */ 
    110 $preview = dirname($name) . '/preview/' . basename($name); 
    111 if (!file_exists(dirname($preview))) mkdir(dirname($preview)); 
    112 exec('convert -define jpeg:size=' . $preview_width * 2 . 'x' . $preview_height * 2 . ' \\ 
    113   \'' . $name . '\'[0] -thumbnail ' . $preview_width . 'x' . $preview_height . ' \\ 
    114  -unsharp 0x.5 \'' . $preview . '\''); 
    115  
    116 // Open database 
    117 $db = new sqlite('lib/db.sqlite'); 
    118  
    119 $user = (isset($_SESSION['openid_identity'])) ? $_SESSION['openid_identity'] : ''; 
    120  
    121 // Save image info 
    122 $db->exec("INSERT INTO images ( 
    123  location, 
    124  path, 
    125  ip, 
    126  time, 
    127  original_name, 
    128  user 
    129 ) VALUES ( 
    130  '" . $db->escape($location) . "', 
    131  '" . $db->escape($name) . "', 
    132  '" . ip2long($_SERVER['REMOTE_ADDR']) . "', 
    133  '" . time() . "', 
    134  '" . $db->escape($img['name']) . "', 
    135  '" . $db->escape($user) . "' 
    136 );" ); 
    137 $res = $db->query("SELECT last_insert_rowid() as id;"); 
    138 $row = $db->fetch($res); 
    139 $id = $row['id']; 
    140  
    141 /* 
    142  * Tags 
    143  */ 
    144 if (isset($_POST['tags'])) { 
    145         $tags = explode(',', $_POST['tags']); 
    146         for ($i = 0; $i < count($tags); $i++) { 
    147                 $tags[$i] = trim($tags[$i]); 
    148         } 
    149         $tags = array_unique($tags); 
    150         $sql = "BEGIN;\n"; 
    151         foreach ($tags as $tag) { 
    152                 if (empty($tag)) continue; 
    153                 // check if the taga already exists 
    154                 $row = $db->fetch($db->query("SELECT ROWID as id FROM tags WHERE tag = '" . $db->escape(strtolower($tag)) . "'")); 
    155                 if (!$row) { 
    156                         $db->exec("INSERT INTO tags (tag, text) VALUES ('" . $db->escape(strtolower($tag)) . "', '" . $db->escape($tag) . "');"); 
    157                         $row = $db->fetch($db->query("SELECT last_insert_rowid() as id;")); 
    158                 } 
    159                 // Save the tag for this image and update tag counter 
    160                 $sql .= "INSERT INTO imagetags (image, tag) VALUES('" . $id . "', '" . $row['id'] . "');\n"; 
    161                 $sql .= "UPDATE tags SET count = count + 1 WHERE ROWID = '" . $row['id'] . "';\n"; 
    162         } 
    163         $sql .= "COMMIT;"; 
    164         // Commit all changes 
    165         $db->exec($sql); 
    166 } 
    167  
    168 // Redirect to image 
    169 header('Location: ' . url() . 'image.php?i=' . urlnumber_encode($id)); 
    170 errorMsg('Image saved.<br /><br /><a href="' . url() . 'image.php?i=' . urlnumber_encode($id) . '">Continue</a>'); 
    171  
    172182?>