Skip to content

Instantly share code, notes, and snippets.

@daveloodts
Created June 12, 2018 09:01
Show Gist options
  • Save daveloodts/f044192dd5cc484bd7975e4460cd647f to your computer and use it in GitHub Desktop.
Save daveloodts/f044192dd5cc484bd7975e4460cd647f to your computer and use it in GitHub Desktop.
<?php
// link to the font file no the server
$fontname = 'font/Capriola-Regular.ttf';
// controls the spacing between text
$i=30;
//JPG image quality 0-100
$quality = 90;
function create_image($user){
global $fontname;
global $quality;
$file = "covers/".md5($user[0]['name'].$user[1]['name'].$user[2]['name']).".jpg";
// if the file already exists dont create it again just serve up the original
//if (!file_exists($file)) {
// define the base image that we lay our text on
$im = imagecreatefromjpeg("field.jpg");
// setup the text colours
$color['grey'] = imagecolorallocate($im, 54, 56, 60);
$color['green'] = imagecolorallocate($im, 55, 189, 102);
// this defines the starting height for the text block
$y = imagesy($im) - $height - 365;
// loop through the array and write the text
foreach ($user as $value){
// center the text in our image - returns the x value
$x = center_text($value['name'], $value['font-size'], $value['xxx'], $value['yyy']);
imagettftext($im, $value['font-size'], 0, $value['xxx'], $value['yyy'], $color[$value['color']], $fontname,$value['name']);
}
// create the image
imagejpeg($im, $file, $quality);
//}
return $file;
}
function center_text($string, $font_size){
global $fontname;
$image_width = 600;
$dimensions = imagettfbbox($font_size, 0, $fontname, $string);
return ceil(($image_width - $dimensions[4]) / 2);
}
$user = array(
array(
'name'=> '...',
'font-size'=>'13',
'color'=>'grey'),
array(
'name'=> '...',
'font-size'=>'13',
'color'=>'grey'),
array(
'name'=> '...',
'font-size'=>'13',
'color'=>'green'
)
);
if(isset($_POST['submit'])){
$error = array();
if(strlen($_POST['keeper'])==0){
$error[] = 'Forgot position 1';
}
if(strlen($_POST['def1'])==0){
$error[] = 'Forgot position 2';
}
if(strlen($_POST['def2'])==0){
$error[] = 'Forgot position 3';
}
if(strlen($_POST['def3'])==0){
$error[] = 'Forgot position 4';
}
if(strlen($_POST['def4'])==0){
$error[] = 'Forgot position 5';
}
if(strlen($_POST['mid1'])==0){
$error[] = 'Forgot position 6';
}
if(strlen($_POST['mid2'])==0){
$error[] = 'Forgot position 7';
}
if(strlen($_POST['mid3'])==0){
$error[] = 'Forgot position 8';
}
if(strlen($_POST['att1'])==0){
$error[] = 'Forgot position 9';
}
if(strlen($_POST['att2'])==0){
$error[] = 'Forgot position 10';
}
if(strlen($_POST['att3'])==0){
$error[] = 'Forgot position 11';
}
if(count($error)==0){
$user = array(
array(
'name'=> $_POST['keeper'],
'font-size'=>'11',
'color'=>'black',
'xxx'=>'252',
'yyy'=>'794'),
array(
'name'=> $_POST['def1'],
'font-size'=>'11',
'color'=>'black',
'xxx'=>'147',
'yyy'=>'688'),
array(
'name'=> $_POST['def2'],
'font-size'=>'11',
'color'=>'black',
'xxx'=>'341',
'yyy'=>'688'),
array(
'name'=> $_POST['def3'],
'font-size'=>'11',
'color'=>'black',
'xxx'=>'124',
'yyy'=>'619'),
array(
'name'=> $_POST['def4'],
'font-size'=>'11',
'color'=>'black',
'xxx'=>'391',
'yyy'=>'619'),
array(
'name'=> $_POST['mid1'],
'font-size'=>'11',
'color'=>'black',
'xxx'=>'70',
'yyy'=>'528'),
array(
'name'=> $_POST['mid2'],
'font-size'=>'11',
'color'=>'black',
'xxx'=>'248',
'yyy'=>'528'),
array(
'name'=> $_POST['mid3'],
'font-size'=>'11',
'color'=>'black',
'xxx'=>'431',
'yyy'=>'528'),
array(
'name'=> $_POST['att1'],
'font-size'=>'11',
'color'=>'black',
'xxx'=>'118',
'yyy'=>'442'),
array(
'name'=> $_POST['att2'],
'font-size'=>'11',
'color'=>'black',
'xxx'=>'387',
'yyy'=>'442'),
array(
'name'=> $_POST['att3'],
'font-size'=>'11',
'color'=>'black',
'xxx'=>'255',
'yyy'=>'386')
);
}
}
// run the script to create the image
$filename = create_image($user);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WKWP - What's your WorldCup WordPress Team </title>
<meta name="twitter:card" content="WCreate your WordPress WorldCup Team" />
<meta name="twitter:site" content="@daveloodts" />
<meta name="twitter:creator" content="@daveloodts" />
<meta property="og:url" content="https://wkwp.org" />
<meta property="og:title" content="Create your WordPress WorldCup Team" />
<meta property="og:description" content="Create your WordPress WorldCup Team" />
<meta property="og:image" content="https://www.wkwp.org/<?php echo $filename; ?>" />
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@daveloodts">
<meta name="twitter:creator" content="@daveloodts">
<meta name="twitter:title" content="Create your WordPress WorldCup Team">
<meta name="twitter:description" content="WKWP.org shares everyone personal WordPress WorldCup Teams">
<meta name="twitter:image" content="https://www.wkwp.org/<?php echo $filename; ?>">
<style>
body { background: url('bg.jpg'); color: #FFF; padding-top: 2rem; font-family: "Arial Black", Times, serif; }
input{
border:1px solid #ccc;
padding:8px;
font-size:14px;
width:250px;
}
.submit{
padding: 40px 20px; font-size: 16px; font-weight: 700;
background-color:#FFFF00;
padding:3px;
border:1px solid #FC0;
margin-top:20px;
color: #000;
height: 80px; width:265px; }
.submit:hover { cursor: pointer; opacity: 0.8; }
.content { margin: 0 auto; width: 1000px; }
.half-col, .half-col-two { float: left; }
.half-col { width: 600px; margin-top: 0.8rem; }
.half-col-two { width: 300px; padding-left: 100px}
form { border: 2px solid #FFF; padding: 1rem; }
.twitter-share-button { background: #00aced; color: #FFF; width: 100%; text-align: center; display: block; padding: 20px 0; text-decoration: none; border-radius: 20px; }
.twitter-share-button:hover { background: #0084b4; }
.text-center { text-align: center; width: 100%; clear: both; padding: 40px 0; }
.text-center a { color: #FFF; }
@media only screen and (max-width: 1000px) {
.half-col-two { width: 300px; margin: 0 auto; padding-left: 50px; clear: both;}
.content { width: 100% !important; }
.half-col { width: 100% !important;}
.content img { width: 95%; height: auto; }
}
</style>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-31371744-54"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-31371744-54');
</script>
</head>
<body>
<div class="content">
<div class="half-col">
<p>Generate image, save it and tweet it with #WKWP. Thanks!<br/> <br/>
<img src="<?=$filename;?>?id=<?=rand(0,1292938);?>" width="600" height="840"/><br/><br/>
</div>
<div class="half-col-two">
<ul>
<?php if(isset($error)){
foreach($error as $errors){
echo '<li>'.$errors.'</li>';
}
}?>
</ul>
<h4>Share your favourite WordPress plugins in WorldCup-style!</h4>
<div class="dynamic-form">
<form action="" method="post">
<label>1:</label>
<input type="text" value="<?php if(isset($_POST['keeper'])){echo $_POST['keeper'];}?>" name="keeper" maxlength="15" placeholder="Keeper"><br/>
<label>2:</label>
<input type="text" value="<?php if(isset($_POST['def1'])){echo $_POST['def1'];}?>" name="def1" placeholder="Defender"><br/>
<label>3:</label>
<input type="text" value="<?php if(isset($_POST['def2'])){echo $_POST['def2'];}?>" name="def2" placeholder="Central Defender"><br/>
<label>4:</label>
<input type="text" value="<?php if(isset($_POST['def3'])){echo $_POST['def3'];}?>" name="def3" placeholder="Central Defender"><br/>
<label>5:</label>
<input type="text" value="<?php if(isset($_POST['def4'])){echo $_POST['def4'];}?>" name="def4" placeholder="Defender"><br/>
<label>6:</label>
<input type="text" value="<?php if(isset($_POST['mid1'])){echo $_POST['mid1'];}?>" name="mid1" placeholder="Midfield"><br/>
<label>7:</label>
<input type="text" value="<?php if(isset($_POST['mid2'])){echo $_POST['mid2'];}?>" name="mid2" placeholder="Central Midfield"><br/>
<label>8:</label>
<input type="text" value="<?php if(isset($_POST['mid3'])){echo $_POST['mid3'];}?>" name="mid3" placeholder="Midfield"><br/>
<label>9:</label>
<input type="text" value="<?php if(isset($_POST['att1'])){echo $_POST['att1'];}?>" name="att1" placeholder="Attack"><br/>
<label>10:</label>
<input type="text" value="<?php if(isset($_POST['att2'])){echo $_POST['att2'];}?>" name="att2" placeholder="Attack"><br/>
<label>11:</label>
<input type="text" value="<?php if(isset($_POST['att3'])){echo $_POST['att3'];}?>" name="att3" placeholder="Attack"><br/>
<input name="submit" type="submit" class="btn btn-primary submit" value="Create your WorldCup Team" />
</form>
</div>
</div>
<div class="text-center">
<a href="https://www.twitter.com/daveloodts">Made by Dave Loodts / @daveloodts</div>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment