コード例 #1
0
ファイル: gallery.model.php プロジェクト: gpuenteallott/rox
    /**
     * processing image uploads
     *
     * @todo sizes should be customizable
     */
    public function uploadProcess(&$vars)
    {
        // NEW CHECKS
        if (!($member = $this->getLoggedInMember())) {
            $vars['error'] = 'Gallery_NotLoggedIn';
            return false;
        }
        if (!isset($_FILES['gallery-file']) || !is_array($_FILES['gallery-file']) || count($_FILES['gallery-file']) == 0) {
            $vars['error'] = 'Gallery_UploadError';
            return false;
        }
        $noError = true;
        // flag for error on one file
        $userDir = new PDataDir('gallery/user' . $member->get_userid());
        $insert = $this->dao->prepare('
INSERT INTO `gallery_items`
(`id`, `user_id_foreign`, `file`, `original`, `flags`, `mimetype`, `width`, `height`, `title`, `created`)
VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())
            ');
        $itemId = false;
        $insert->bindParam(0, $itemId);
        $userId = $member->get_userid();
        $insert->bindParam(1, $userId);
        $hash = false;
        $insert->bindParam(2, $hash);
        $orig = false;
        $insert->bindParam(3, $orig);
        $flags = 0;
        $insert->bindParam(4, $flags);
        $mimetype = false;
        $insert->bindParam(5, $mimetype);
        $width = false;
        $insert->bindParam(6, $width);
        $height = false;
        $insert->bindParam(7, $height);
        $title = false;
        $insert->bindParam(8, $title);
        foreach ($_FILES['gallery-file']['error'] as $key => $error) {
            $fileName = $_FILES['gallery-file']['name'][$key];
            // if upload failed, set error message
            if (!empty($fileName) && $error != UPLOAD_ERR_OK) {
                $noError = false;
                switch ($error) {
                    case UPLOAD_ERR_INI_SIZE:
                    case UPLOAD_ERR_FORM_SIZE:
                        $vars['fileErrors'][$fileName] = 'Gallery_UploadFileTooLarge';
                        break;
                    default:
                        $vars['fileErrors'][$fileName] = 'Gallery_UploadError';
                        break;
                }
            } elseif (!empty($fileName)) {
                // upload succeeded -> check if image
                $img = new MOD_images_Image($_FILES['gallery-file']['tmp_name'][$key]);
                if (!$img->isImage()) {
                    $noError = false;
                    $vars['fileErrors'][$fileName] = 'Gallery_UploadNotImage';
                } else {
                    // upload is image
                    // resize
                    $size = $img->getImageSize();
                    $original_x = min($size[0], PVars::getObj('images')->max_width);
                    $original_y = min($size[1], PVars::getObj('images')->max_height);
                    $tempDir = dirname($_FILES['gallery-file']['tmp_name'][$key]);
                    $resizedName = md5($_FILES['gallery-file']['tmp_name'][$key]) . '_resized';
                    $img->createThumb($tempDir, $resizedName, $original_x, $original_y, true, 'ratio');
                    $tempFile = $tempDir . '/' . $resizedName;
                    // create new image object from resized image
                    $img = new MOD_images_Image($tempFile);
                    $size = $img->getImageSize();
                    $type = $size[2];
                    $hash = $img->getHash();
                    if ($userDir->fileExists($hash)) {
                        continue;
                    }
                    if (!$userDir->copyTo($tempFile, $hash)) {
                        continue;
                    }
                    if (!($result = $this->createThumbnails($userDir, $img))) {
                        return false;
                    }
                    $itemId = $this->dao->nextId('gallery_items');
                    $orig = $_FILES['gallery-file']['name'][$key];
                    $mimetype = image_type_to_mime_type($type);
                    $width = $size[0];
                    $height = $size[1];
                    $title = $orig;
                    try {
                        $insert->execute();
                    } catch (PException $e) {
                        error_log($e->__toString());
                    }
                    if ($vars['galleryId']) {
                        $this->dao->exec("INSERT INTO `gallery_items_to_gallery` SET `gallery_id_foreign` = '" . $vars['galleryId'] . "', `item_id_foreign`= " . $itemId);
                    }
                    $vars['fileErrors'][$_FILES['gallery-file']['name'][$key]] = 'Gallery_UploadFileSuccessfule';
                    unlink($tempFile);
                }
            }
        }
        return $noError;
    }
コード例 #2
0
ファイル: groups.model.php プロジェクト: gpuenteallott/rox
 /**
  * takes care of a group image being uploaded
  *
  * @access private
  * @return mixed - false on fail or the name of the uploaded image
  */
 private function handleImageUpload()
 {
     if ($_FILES['group_image']['error'] != 0) {
         return false;
     } else {
         $dir = new PDataDir('groups');
         $img = new MOD_images_Image($_FILES['group_image']['tmp_name']);
         $new_name = $img->getHash();
         if (empty($new_name)) {
             return false;
         }
         if (!($dir->fileExists($new_name) || $dir->copyTo($_FILES['group_image']['tmp_name'], $new_name))) {
             return false;
         } else {
             // yup, hackish way of resizing an image
             // feel free to add a resize function to MOD_images_Image and change this bit
             // or create an image entity with all needed functionality in ONE place
             // ... in my dreams ...
             $img->createThumb($dir->dirName(), $new_name, 300, 300, true);
             $img->createThumb($dir->dirName(), 'thumb', 100, 100);
             return $new_name;
         }
     }
 }