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)!';
self::writeError("1");
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, $attr ) = getimagesize($localPath);
if( $type == IMAGETYPE_JPEG ) {
$extension = 'jpg';
} else if( $type == IMAGETYPE_GIF ) {
$extension = 'gif';
} else if( $type == IMAGETYPE_PNG ) {
$extension = 'png';
} else if($uploaded == false) {
if(preg_match( '/([^\/]*?)\.(jpg|jpeg)$/i', $localPath, $fileMatch )) {
$extension = 'jpg';
}
else if(preg_match( '/([^\/]*?)\.(png)$/i', $localPath, $fileMatch )) {
$extension = 'png';
}
else if(preg_match( '/([^\/]*?)\.(gif)$/i', $localPath, $fileMatch )) {
$extension = 'gif';
}
} else {
self::writeError("2");
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) ) {
$errors['downloadFailed'] = 'folder failed';
self::writeError("3");
return false;
}
if( $uploaded ) {
if( !@move_uploaded_file( $localPath, $imagePath ) ) {
$errors['downloadFailed'] = 'move failed';
return false;
}
} else {
if( !@rename( $localPath, $imagePath ) ) {
$errors['downloadFailed'] = 'rename failed';
self::writeError($extension);
return false;
}
}
self::writeError($imagePath);
if($commentUpload == false && $cid == 0){
$hash = md5_file( $imagePath );
$c = DB::getRow('SELECT id,keyword FROM '.TABLE_IMAGES.' WHERE hash = :1', $hash);
if( !empty( $c ) ) {
unlink( $imagePath );
$errors['duplicate'] =
'This image is already in our database!'."
Click here to comment it.";
self::writeError("5");
return false;
}else{
$c = DB::query('SELECT id FROM pl_bannedimages WHERE hash = :1', $hash);
if( !empty( $c ) ) {
unlink( $imagePath );
$errors['duplicate'] = 'This image is already in our database!';
self::writeError("6");
return false;
}
}
}
// 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'] = 'Error generating thumbnails!';
return false;
}
}
///watermark
watermark($imagePath,"media/watermark.png",$imagePath);
///watermark
$user->increaseScore( Config::$postScore );
if (($commentUpload) && ($cid != 0)){
DB::query('INSERT INTO pl_commentuploads (c_id,image_link,thumb_link) VALUES (:1,:2,:3)',$cid,$imagePath,"data/thumbs/".date('Y/m/', $time)."64x64/".$thumbName);
/*DB::insertRow( "pl_commentuploads",array('c_id' => $cid,"image_link" =>$imagePath,"thumb_link" => "data/thumbs/".date('Y/m/', $time)."/128x128/".$name));*/
}else{
$iCount = DB::getRow("SELECT COUNT(*) as c FROM pl_images WHERE user=:1",$user->id);
DB::query( 'UPDATE '.TABLE_USERS.' SET images =:2 WHERE id = :1', $user->id,(int)$iCount["c"]);
DB::insertRow( TABLE_IMAGES,
array(
'logged' => date('Y-m-d H:i:s', $time),
'user' => $user->id,
'score' => 1,
'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;
}
}
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.');
/*$tmp = imagecreatetruecolor($srcWidth,($srcHeight + 30));*/
/*$back = imagecolorallocatealpha($image, 0, 0, 0,60);
imagefilledrectangle($image,0, $srcHeight - 20,$srcWidth,$srcHeight ,$back); */
/*imagecopy($tmp,$image,0,0,0,0,$srcWidth,$srcHeight);
$image = $tmp;*/
$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;
}
}
?>