Skip to content

Commit

Permalink
- more rendering logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Cupidr Live committed Feb 10, 2015
1 parent 3299c32 commit 4e957e9
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 33 deletions.
102 changes: 91 additions & 11 deletions bin/templates.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,93 @@
[
{template: "asgood.jpg", styles: {top: "320px", "max-height": "200px"}},
{template: "farva.jpg", styles: {top: "200px", "max-width": "300px", left: "30px", color: "#fff", "max-height": "220px", transform: "rotate(-30deg)"}},
{template: "kennypowers.jpg", styles: {right: "140px", left: "auto", width: "350px", color: "#fff"}},
{template: "loveactually.jpg", styles: {left: "200px", "max-width": "400px", top: "250px"}},
{template: "nickcage.jpg", styles: {"max-height": "150px", top: "320px", color: "#fff"}},
{template: "notebook.jpg", styles: {"max-height": "140px", top: "330px", color: "#fff"}},
{template: "officespace.jpg", styles: {"max-height": "150px", top: "320px", color: "#fff"}},
{template: "supertroopers.jpg", styles: {"max-height": "160px", top: "330px", color: "#fff"}},
{template: "titantic.jpg", styles: {"left": "300px", top: "20px", width: "400px", color: "#fff"}},
{template: "topgun.jpg", styles: {"top": "350px", "max-height": "100px", color: "#fff"}},
{template: "dirtydancing.jpg", styles: {"top": "320px", "max-height": "180px", color: "#fff"}},
{
"template":"asgood.jpg",
"styles":{
"top":"320px",
"max-height":"200px"
}
},
{
"template":"farva.jpg",
"styles":{
"top":"200px",
"max-width":"300px",
"left":"30px",
"color":"#ffffff",
"max-height":"220px",
"transform":"rotate(-30deg)"
}
},
{
"template":"kennypowers.jpg",
"styles":{
"left":"330px",
"max-width":"350px",
"color":"#ffffff"
}
},
{
"template":"loveactually.jpg",
"styles":{
"left":"200px",
"max-width":"400px",
"top":"250px"
}
},
{
"template":"nickcage.jpg",
"styles":{
"max-height":"150px",
"top":"320px",
"color":"#ffffff"
}
},
{
"template":"notebook.jpg",
"styles":{
"max-height":"140px",
"top":"330px",
"color":"#ffffff"
}
},
{
"template":"officespace.jpg",
"styles":{
"max-height":"150px",
"top":"320px",
"color":"#ffffff"
}
},
{
"template":"supertroopers.jpg",
"styles":{
"max-height":"160px",
"top":"330px",
"color":"#ffffff"
}
},
{
"template":"titantic.jpg",
"styles":{
"left":"300px",
"top":"20px",
"max-width":"400px",
"color":"#ffffff"
}
},
{
"template":"topgun.jpg",
"styles":{
"top":"350px",
"max-height":"140px",
"color":"#ffffff"
}
},
{
"template":"dirtydancing.jpg",
"styles":{
"top":"320px",
"max-height":"180px",
"color":"#ffffff"
}
}
]
2 changes: 1 addition & 1 deletion views/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
<script>
$(document).ready(function(){
var styleOverrides = {{ templateJson|raw }};
var styleOverrides = {{ templateJson|raw }};
$("[data-provide='address-form']").submit(function(){
return false;
Expand Down
135 changes: 114 additions & 21 deletions web/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,120 @@

error_reporting(E_ALL);

$template = "ryangosling.jpg";
$text = "Fox trot charlie!!!!";
class RenderImage {

private $jsonConfigs = [];

public function __construct(){
$this->jsonConfigs = json_decode(file_get_contents(dirname(__FILE__) . "/../bin/templates.json"), true);
}

$font = dirname(__FILE__) . "/../bin/OpenSans-ExtraBold.ttf";
$templateImg = imagecreatefromjpeg( dirname(__FILE__) . "/templates/" . $template );
public function getTextColor( $im, $template ){
$default = imagecolorallocate($im, 0, 0, 0);
$config = $this->getConfig( $template );

if( !$config || !array_key_exists("color", $config) ){
return $default;
}

$color = str_replace("#", "", $config["color"]);

$r = hexdec($color[0] . $color[1]);
$g = hexdec($color[2] . $color[3]);
$b = hexdec($color[4] . $color[5]);

return imagecolorallocate($im, $r, $g, $b);
}

public function getPasteCoords($template){
$default = [40, 40];
$config = $this->getConfig( $template );

if( !$config ){
return $default;
}

if( array_key_exists("left", $config) ){
$default[0] += (str_replace("px", "", $config["left"]) * 2.7) - 100;
$default[0] = $default[0] < 0 ? 40 : $default[0];
}

if( array_key_exists("top", $config) ){
$default[1] += (str_replace("px", "", $config["top"]) * 2.7) - 100;
$default[1] = $default[1] < 0 ? 40 : $default[1];
}

return $default;
}

public function getNumchars($template){
$default = 45;
$config = $this->getConfig( $template );

if( !$config || !array_key_exists("max-width", $config) ){
return $default;
}

return ceil( str_replace("px", "", $config["max-width"]) * .06 );
}

public function getConfig($template){

foreach( $this->jsonConfigs as $cfg ){
if( $cfg["template"] == $template ){
return $cfg["styles"];
}
}

return null;
}


public function render($params = []){

$template = $params["selected-template"];

$numChars = $this->getNumchars($template);
$lines = str_split($params["added-text"], $numChars);

$font = dirname(__FILE__) . "/../bin/OpenSans-ExtraBold.ttf";
$templateImg = imagecreatefromjpeg( dirname(__FILE__) . "/templates/" . $template );

$im = imagecreatetruecolor(1875, 1275);

$textColor = $this->getTextColor( $im, $template );
$transparentWhite = imagecolorallocatealpha($im, 0, 0, 0, 90);

imagesavealpha($im, true);
imagefill($im, 0, 0, $transparentWhite);

$widths = [];
$height = 0;

foreach( $lines as $text ){
$coords = imagettftext($im, 60, 0, 50, $height + 100, $textColor, $font, $text);

$height = $coords[3];
$widths[] = $coords[2] + 60;
}

$height += 60;
$width = max($widths);

$pasteCoords = $this->getPasteCoords($template);

imagecopy($templateImg, $im, $pasteCoords[0], $pasteCoords[1], 0, 0, $width, $height);

header('Content-Type: image/png');
imagepng($templateImg);
imagedestroy($templateImg);

}

}

$im = imagecreatetruecolor(1875, 1275);
$template = "nickcage.jpg";
$text = "I love you like a fat kid loves cake...I love you like a fat kid loves cake..";

$black = imagecolorallocate($im, 0, 0, 0);
$transparentWhite = imagecolorallocatealpha($im, 0, 0, 0, 90);

imagesavealpha($im, true);
imagefill($im, 0, 0, $transparentWhite);

$coords = imagettftext($im, 60, 0, 50, 100, $black, $font, $text);

$height = $coords[3] + 60;
$width = $coords[2] + 60;

imagecopy($templateImg, $im, 40, 40, 0, 0, $width, $height);

header('Content-Type: image/png');
imagepng($templateImg);
imagedestroy($templateImg);
$rm = new RenderImage();
$rm->render( ["added-text" => $text, "selected-template" => $template] );

0 comments on commit 4e957e9

Please sign in to comment.