Beispiel #1
0
function image_storeUploaded(&$up)
{
    $ext = image_fileExt($up['type']);
    if (is_null($ext)) {
        return NULL;
    }
    $inf = getimagesize($up['tmp_name']);
    if ($inf === FALSE) {
        return NULL;
    }
    $w = $inf[0];
    $h = $inf[1];
    $image_id = db_getOne("select nextval('image_id_seq' )");
    $filename = "{$image_id}.{$ext}";
    if (!move_uploaded_file($up['tmp_name'], OPTION_JL_IMG_UPLOAD . '/' . $filename)) {
        return NULL;
    }
    db_do("INSERT INTO image (id,filename,width,height) VALUES (?,?,?,?)", $image_id, $filename, $w, $h);
    return array('id' => $image_id, 'width' => $w, 'height' => $h, 'filename' => $filename);
}
 function handleUpload()
 {
     $file = $_FILES['file'];
     $err = $file['error'];
     switch ($err) {
         case UPLOAD_ERR_OK:
             break;
         case UPLOAD_ERR_INI_SIZE:
             // 1
             $this->uploadError = "That file was too big (maximum size is " . ini_get('upload_max_filesize') . ")";
             return;
         case UPLOAD_ERR_NO_FILE:
             // 4
             $this->uploadError = "No file was uploaded";
             return;
         default:
             $this->uploadError = "Upload failed (code {$file['error']})";
             return;
     }
     if (image_fileExt($file['type']) == NULL) {
         $this->uploadError = 'Image must be jpeg, gif or png';
         return;
     }
     $inf = getimagesize($file['tmp_name']);
     if ($inf === FALSE) {
         $this->uploadError = "can't determine image size";
         return;
     }
     $w = $inf[0];
     $h = $inf[1];
     $MAXW = 480;
     if ($w > $MAXW) {
         // scale it down to a reasonable width
         $scale = (double) $MAXW / (double) $w;
         $new_w = $w * $scale;
         $new_h = $h * $scale;
         $source = imagecreatefromstring(file_get_contents($file['tmp_name']));
         $new_image = imagecreatetruecolor($new_w, $new_h);
         imagecopyresampled($new_image, $source, 0, 0, 0, 0, $new_w, $new_h, $w, $h);
         $this->replacePhoto($new_image, FALSE);
         return;
     }
     if ($w <= THUMB_W && $h <= THUMB_H) {
         // It's small enough to use as a thumbnail straight off
         $this->replacePhoto($file, TRUE);
         return;
     }
     //
     $this->replacePhoto($file, FALSE);
 }