require_once("../include/config.php");
require_once("../include/libraries/phpFlickr/phpFlickr.php");
if(isset($_POST['submit'])){
$q[0] = $_POST['q1'];
$q[1] = $_POST['q2'];
$q[2] = $_POST['q3'];
$q[3] = $_POST['q4'];
$q[4] = $_POST['q5'];
$q[5] = $_POST['q6'];
$q[6] = $_POST['q7'];
$q[7] = $_POST['q8'];
$q[8] = $_POST['q9'];
$q[9] = $_POST['q10'];
$q[10] = $_POST['q11'];
$q[11] = $_POST['q12'];
//validate input
for($i = 0; $i < 12; $i++){
if(strlen($q[$i]) > 40){
echo "Error: error in input, please hit back and try again.";
exit;
}
}
/* LICENSE INFO COPIED FROM FLICKR
*/
$f = new phpFlickr("7d49bbebc1ce90659a40fac79aba534f");
$invalid = array();
$invalidCount = 0;
//check ip to see if they have submitted recently
$ip=$_SERVER['REMOTE_ADDR'];
$oldTime = time() - 60;
$query = "SELECT * FROM meme_recent WHERE ip = '$ip' AND time > '$oldTime'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0){
//Recently run, so wait before running it again
$error = "Please wait 60 seconds before creating another photo mosaic meme.";
//load it to put it back in the form.
for($i = 0; $i < 12; $i++){
//strip non alpha numeric characters
$answer = preg_replace("/[^a-zA-Z0-9s ]/", "", $q[$i]);
$photos[$i]['answer'] = $answer;
}
}
else{
$query = "DROP FROM meme_recent time < '$oldTime'";
$result = mysql_query($query);
$time = time();
$query = "INSERT INTO meme_recent (ip,time) VALUES ('$ip','$time')";
$result = mysql_query($query);
for($i = 0; $i < 12; $i++){
//strip non alpha numeric characters
$answer = preg_replace("/[^a-zA-Z0-9s ]/", "", $q[$i]);
if(!isset($answer) || $answer == ""){
$invalid[$invalidCount] = $i;
$invalidCount++;
$photos[$i]['invalid'] = TRUE;
$photos[$i]['answer'] = $answer;
}
else{
$page = rand(1,4);
$tempPhotos = $f->photos_search(array("license"=>"4,5", "text"=>"$answer", "per_page"=>"5", "page"=>"$page" ));
$max = count($tempPhotos['photo']);
//there are not that many photos by that search term
if($max != 5){
$tempPhotos = $f->photos_search(array("license"=>"4,5", "text"=>"$answer", "per_page"=>"1", "page"=>"1" ));
if(count($tempPhotos['photo']) == 1){
//this is the only way the photo will be made, so this is where we gather info for the dtabase
$tempPhoto = $tempPhotos['photo'][0];
$photos[$i]['small'] = $f->buildPhotoURL($tempPhoto, "small");
$photos[$i]['owner'] = $tempPhoto['owner']['username'];
$photos[$i]['title'] = $tempPhoto['title'];
$photos[$i]['answer'] = $answer;
$photos[$i]['url'] = "http://www.flickr.com/photos/" . $tempPhoto['owner'] . "/" . $tempPhoto['id'] ."";
}
else{
//there is no photo
$invalid[$invalidCount] = $i;
$invalidCount++;
$photos[$i]['invalid'] = TRUE;
$photos[$i]['answer'] = $answer;
}
}
else{
$rand = rand(0,4);
$tempPhoto = $tempPhotos['photo'][$rand];
$photos[$i]['small'] = $f->buildPhotoURL($tempPhoto, "small");
$photos[$i]['owner'] = $tempPhoto['owner'];
$photos[$i]['title'] = $tempPhoto['title'];
$photos[$i]['answer'] = $answer;
$photos[$i]['url'] = "http://www.flickr.com/photos/" . $tempPhoto['owner'] . "/" . $tempPhoto['id'] ."";
}
}
}
if($invalidCount == 0){
$finalImage = imagecreatetruecolor(920, 1225);
$white = imagecolorallocate($finalImage, 255, 255, 255);
imagefill($finalImage, 0, 0, $white);
for($i = 0; $i < 12; $i++){
$image = cropImage($photos[$i]['small']);
switch($i){
case 0:
$x = 5;
$y = 5;
break;
case 1:
$x = 310;
$y = 5;
break;
case 2:
$x = 615;
$y = 5;
break;
case 3:
$x = 5;
$y = 310;
break;
case 4:
$x = 310;
$y = 310;
break;
case 5:
$x = 615;
$y = 310;
break;
case 6:
$x = 5;
$y = 615;
break;
case 7:
$x = 310;
$y = 615;
break;
case 8:
$x = 615;
$y = 615;
break;
case 9:
$x = 5;
$y = 920;
break;
case 10:
$x = 310;
$y = 920;
break;
case 11:
$x = 615;
$y = 920;
break;
}
imagecopy($finalImage, $image, $x, $y, 0, 0, 300, 300);
}
$identifier = time() . '' . rand(0,999999);
$identifier = "$identifier";
for($i = 0; $i < 12; $i++){
$answers[$i] = $photos[$i]['answer'];
$urls[$i] = $photos[$i]['url'];
}
$serialized_answers = serialize($answers);
$serialized_urls = serialize($urls);
$time = time();
$query = "INSERT INTO memes (identifier, serialized_answers, serialized_urls, time) VALUES ('$identifier', '$serialized_answers', '$serialized_urls', '$time')";
$result = mysql_query($query);
if(!$result){
echo "$query
";
echo mysql_error();
exit;
}
$img = $identifier . '.jpg';
$thumb = $identifier . '_thumb.jpg';
$thumbImage = imagecreatetruecolor(460, 612);
imagecopyresampled($thumbImage, $finalImage, 0, 0, 0, 0, 460, 612, 920, 1225);
imagejpeg($finalImage, '/home/virtual/site22/fst/var/www/html/temp/' . $img, 80);
imagejpeg($thumbImage, '/home/virtual/site22/fst/var/www/html/temp/' . $thumb, 80);
imagedestroy($finalImage);
imagedestroy($thumbImage);
header("Location: /view.php?identifier=$identifier");
}
else{
$error = 'Your attempt to make a photo mosaic meme failed. We probably could not find any images for one of the search terms. Please change the search terms indicated in red below and try again.';
}
}
}
include_once("templates/photo-mosaic-meme.php");
function cropImage($url){
$image = imagecreatefromjpeg($url);
$width = imagesx($image);
$height = imagesy($image);
if($height > $width){
$finalHeight = $width;
$finalWidth = $width;
$heightDiff = $height - $finalHeight;
$widthDiff = 0;
}
else{
$finalWidth = $height;
$finalHeight = $height;
$widthDiff = $width - $finalWidth;
$heightDiff = 0;
}
$newImage = imagecreatetruecolor($finalWidth, $finalHeight);
imagecopy($newImage, $image, 0, 0, $widthDiff / 2, $heightDiff / 2, $finalWidth, $finalHeight);
$finalImage = imagecreatetruecolor(300,300);
imagecopyresampled($finalImage, $newImage, 0, 0, 0, 0, 300, 300, $finalWidth, $finalHeight);
imagedestroy($image);
imagedestroy($newImage);
return $finalImage;
}
?>