login(); if( !$user->id ) { $errors['notLoggedIn'] = 'You are not logged in!'; return false; } // no non-images if( !preg_match( '/([^\/]*?)\.(gif|jpg|jpeg|png)$/i', $name, $fileMatch ) ) { $errors['wrongExtension'] = 'Your file has not the right extension (gif, jpg or png)!'; return false; } $time = time(); $keyword = Keyword::get( $fileMatch[1] ); // Iterate over all keywords, if this one is allready taken $taken = array(); $r = DB::query( 'SELECT keyword FROM '.TABLE_IMAGES.' WHERE keyword LIKE :1', date('Y/m/',$time).$keyword.'%' ); foreach( $r as $v ) { $taken[] = substr($v['keyword'],8); // Keyword without date! } $keyword = Keyword::iterate( $keyword, $taken ); list( $srcWidth, $srcHeight, $type ) = getimagesize( $localPath ); if( $type == IMAGETYPE_JPEG ) { $extension = 'jpg'; } else if( $type == IMAGETYPE_GIF ) { $extension = 'gif'; } else if( $type == IMAGETYPE_PNG ) { $extension = 'png'; } else { return false; } $thumbName = $keyword.".jpg"; // all thumbs are jpegs! $name = $keyword.".".$extension; $imageDir = Config::$images['imagePath'] . date('Y/m', $time); $thumbDir = Config::$images['thumbPath'] . date('Y/m', $time); $imagePath = $imageDir.'/'.$name; if( !Filesystem::mkdirr($imageDir) || !Filesystem::mkdirr($thumbDir) ) { return false; } if( $uploaded ) { if( !@move_uploaded_file( $localPath, $imagePath ) ) { return false; } } else { if( !@rename( $localPath, $imagePath ) ) { return false; } } $hash = md5_file( $imagePath ); $c = DB::query('SELECT id FROM '.TABLE_IMAGES.' WHERE hash = :1', $hash); if( !empty( $c ) ) { unlink( $imagePath ); $errors['duplicate'] = 'This image is already in our database!'; return false; } //watermark function watermark($SourceFile, $WatermarkFile, $SaveToFile = NULL) { list( $srcWidth, $srcHeight, $type ) = getimagesize( $SourceFile ); if( $type != IMAGETYPE_GIF ) { $watermark = @imagecreatefrompng($WatermarkFile) or exit('Cannot open the watermark file.'); imageAlphaBlending($watermark, false); imageSaveAlpha($watermark, true); $image_string = @file_get_contents($SourceFile) or exit('Cannot open image file.'); $image = @imagecreatefromstring($image_string) or exit('Not a valid image format.'); $imageWidth=imageSX($image); $imageHeight=imageSY($image); $watermarkWidth=imageSX($watermark); $watermarkHeight=imageSY($watermark); $coordinate_X = ( $imageWidth) - ( $watermarkWidth); $coordinate_Y = ( $imageHeight) - ( $watermarkHeight); imagecopy($image, $watermark, $coordinate_X, $coordinate_Y, 0, 0, $watermarkWidth, $watermarkHeight); if(!($SaveToFile)) header('Content-Type: image/jpeg'); imagejpeg ($image, $SaveToFile, 100); imagedestroy($image); imagedestroy($watermark); if(!($SaveToFile)) exit; } } // make thumbs foreach( Config::$gridView['classes'] as $gc ) { $thumbPath = $thumbDir.'/'.$gc['dir'].'/'.$thumbName; Filesystem::mkdirr($thumbDir.'/'.$gc['dir']); if( !Image::createThumb( $imagePath, $thumbPath, $gc['width'] * Config::$gridView['gridSize'] - Config::$gridView['borderWidth'], $gc['height'] * Config::$gridView['gridSize'] - Config::$gridView['borderWidth'] )) { unlink( $imagePath ); $errors['internalError'] = 'Internal error processing the image!'; return false; } } ///watermark watermark($imagePath,"watermark.png",$imagePath); ///watermark $user->increaseScore( Config::$postScore ); DB::query( 'UPDATE '.TABLE_USERS.' SET images = images + 1 WHERE id = :1', $user->id ); if ($commentUpload && $cid != 0){ DB::insertRow( "pl_commentuploads",array('c_id' => $cid,"image_link" =>$imagePath,"thumb_link" => "data/thumbs/".date('Y/m/', $time)."/128x128/".$name)); }else{ DB::insertRow( TABLE_IMAGES, array( 'logged' => date('Y-m-d H:i:s', $time), 'user' => $user->id, 'score' => 3, 'votes' => 0, 'keyword' => date('Y/m/', $time).$keyword, 'tags' => $tags, 'image' => $name, 'thumb' => $thumbName, 'hash' => $hash, 'source' => $source ) ); } $imageId = DB::insertId(); $colors = Image::getColors( $imagePath, 4 ); foreach( $colors as $c ) { DB::insertRow( TABLE_IMAGECOLORS, array( 'imageId' => $imageId, 'r' => $c['r'], 'g' => $c['g'], 'b' => $c['b'] ) ); } return true; } } ?>