Creating images with text 'imagecreate ()'

2

This will be quite difficult to understand, because it does not have a very dynamic explanation. I will try to be as clear as possible!

I want to create a letter generator that looks like this:

link

This is the site: link

But as everything with me has to be difficult the creation of the image went wrong. I do not know where or why the code does not run perfectly, and no place reports error.

I've reviewed the code several times and can not seem to find the error.

This is what happens when I run: link

<?php

$date = time();
//header("Content-disposition: attachment; filename=$date.png");
header('Content-type: image/png');

error_reporting(10);
$text = $_GET['text'];
$folder = $_GET['folder'];
$spacing = $_GET['space'];
if($spacing == ""){
    $spacing = 0;
}
if($text == ""){
    $text = "Habbo";
    $spacing = -2;
}

if($folder == ""){
    $folder = "wabbo4";
}

if($spacing == ""){
    if($folder == "1"){
        $spacing = 1;
    }
    if($folder == "2"){
        $spacing = 1;
    }
    if($folder == "3"){
        $spacing = -1;
    }
}
$folder = preg_replace("/[^a-zA-Z0-9s]/", "", $folder);
if(!is_dir($folder)){
    $folder = "1";
}

$length = strlen($text);

for($i = 0; $i < $length; $i++){

    $letter = substr($text, $i, 1);
    if($letter == "+"){
        $imgwidth = $imgwidth+5+$spacing;
    }else{
        if (preg_match("/[a-z]/", $letter)) {
            $letterimg = imagecreatefrompng("".$folder."/".$letter.".png"); 
            $letterimgwidth = ImageSX($letterimg);
            $letterimgheight = ImageSY($letterimg);
            $yoffset = 21-$letterimgheight;
            $imgwidth = $imgwidth+$letterimgwidth+$spacing;
        }elseif (preg_match("/[A-Z]/", $letter)){
            $letter = strtolower($letter);
            $letterimg = imagecreatefrompng("".$folder."/".$letter.".png"); 
            $letterimgwidth = ImageSX($letterimg);
            $letterimgheight = ImageSY($letterimg);
            $yoffset = 21-$letterimgheight;
            $imgwidth = $imgwidth+$letterimgwidth+$spacing;
        }else{
            $imgwidth = $imgwidth+5;
        }
    }
}

$imgwidth = $imgwidth-$spacing+1;

$im = imagecreate($imgwidth, $letterimgheight+1);
$overimageheight = $letterimgheight+1;
$background = imagecolorallocatealpha($im, 0, 255, 0, 0);
imagecolortransparent($im, $background);
$xcoord = 1;
for($i = 0; $i < $length; $i++){

    $letter = substr($text, $i, 1);
    if($letter == "+"){
        $xcoord = $xcoord+5+$spacing;
    }else{
        if (preg_match("/[a-z]/", $letter)) {
            $letterimg = imagecreatefrompng("".$folder."/".$letter.".png"); 
            $letterimgwidth = ImageSX($letterimg);
            $letterimgheight = ImageSY($letterimg);
            $yoffset = $overimageheight-$letterimgheight;
            if($letter == "g" || $letter == "j" || $letter == "q" || $letter == "p" || $letter == "y"){
            }
            imagecopy($im, $letterimg, $xcoord, $yoffset, 0, 0, $letterimgwidth, $letterimgheight); 
            $xcoord = $xcoord+$letterimgwidth+$spacing;
        }elseif (preg_match("/[A-Z]/", $letter)){
            $letter = strtolower($letter);
            $letterimg = imagecreatefrompng("".$folder."/".$letter.".png"); 
            $letterimgwidth = ImageSX($letterimg);
            $letterimgheight = ImageSY($letterimg);
            $yoffset = $overimageheight-$letterimgheight;
            imagecopy($im, $letterimg, $xcoord, $yoffset, 0, 0, $letterimgwidth, $letterimgheight); 
            $xcoord = $xcoord+$letterimgwidth+$spacing;
        }else{
            $xcoord = $xcoord+5;
        }
    }
}
header("Content-type: image/png");

imagepng($im); 
imagedestroy($im);

?>

In case folder would be the folder where the letters are, explaining better, the user writes the testo and the folder would be the source where would get the letter a, b, c ... space would be the space between the letters because some fonts need a larger space.

follow image:

link

The result as I said would be the text formed by the letters of a certain source, but it error, DOES NOT REPORT ANY ERROR IN PHP.

I need this help, I've never been good with imagecreate (); I hope you have been clear with my problem.

    
asked by anonymous 18.01.2017 / 23:58

1 answer

0

In fact it was not very clear, next time try to improve the description with images, attempts of failures and success, detailed cases of debug, etc ...

Anyway, in this function you want to insert a content (INPUT) and only change the source of this content? If I am correct, I made a quick code that is self explanatory, give one analyzed and any questions ask.

function generateText() {
    // Getting the values
    var initialText = document.getElementById("initialText").value;
    var finalText = document.getElementById("finalText");
    var fontSource = document.getElementById("fontSource").value;

    finalText.innerHTML = initialText;
    switch (fontSource) {
        case "arial":
            finalText.style.fontFamily = "arial, sans-serif";
            break;
        case "lucida":
            finalText.style.fontFamily = "lucida, sans-serif";
            break;
        case "verdana":
            finalText.style.fontFamily = "verdana, sans-serif";
            break;
    }
}
<input id="initialText" type="text" placeholder="Texto">
<select id="fontSource">
    <option value="arial">Arial</option>
    <option value="lucida">Lucida</option>
    <option value="verdana">Verdana</option>
</select>

<button onclick="generateText()">Alterar fonte</button>
<p id="finalText"></p>
Do you want to turn some content of your page into an image? Use html2canvas . Solve your problem or not, return with your feedback.     
27.08.2017 / 05:57