标题位置不起作用

时间:2016-12-20 07:47:32

标签: php image header location

您好我有一种方法可以在图片上添加文字:

class Generate 
{


    public function image($name, $surname, $city){


            if (empty($_GET['name'])) {

                return header("Location: https://wsaib.pl/index.php?error=name&surname=$surname&city=$city");
                //exit();

            } elseif(strlen(mb_strlen($_GET['name'])) > 20){

                return header("Location: index.php?error=longname&surname=$surname&city=$city");
                //exit();

            } elseif($_GET['surname'] === null) {

                return header("Location: index.php?error=surname&name=$name&city=$city");
                //exit();

            } elseif (strlen(mb_strlen($_GET['surname'])) > 20){

                return header("Location: index.php?error=longsurname&name=$name&city=$city");
                //exit();

            } elseif($_GET['city'] === null) {

                return header("Location: index.php?error=city&name=$name&surname=$surname");
                //exit();

            } elseif (strlen(mb_strlen($_GET['city'])) > 15){

                return header("Location: index.php?error=longcity&name=$name&surname=$surname");
                //exit();

            } else {





        if ((isset($_GET['send']) && $_GET['send'] == 'card') && (isset($_GET['name']) && $_GET['name'] == $name) && (isset($_GET['surname']) && $_GET['surname'] == $surname) && (isset($_GET['city']) && $_GET['city'] == $city)) {


                $getName = $name;
                $getSurname = $surname;
                $getCity = $city;
                //$getWishes = "";


                $today = date("d.m.Y"); 

                $text_length = 38;
                //$textName = wordwrap($getName, $text_length, "<br />", true);
                $textSurname= wordwrap($getSurname, 18, "-<br />", true);
                $textCity = wordwrap($getCity, 11, "-<br />", true);

                //$textN = str_replace('<br />', "\n", $textName);
                $textS = str_replace('<br />', "\n", $textSurname);
                $textC = str_replace('<br />', "\n", $textCity);

                $picture = imagecreatefrompng("merry-christmas.png");

                //$black = imagecolorallocate($picture, 0, 0, 0);
                $white = imagecolorallocate($picture, 0xFF, 0xFF, 0xFF);

                // zyczenia
                //imagettftext($picture, 18, 2, 20, 60, $white, 'fonts/Courgette/Courgette-Regular.ttf', trim(ucfirst($textW)));

                // imie i nazwisko
                if (strlen($getName) >= 15 ) {

                    imagettftext($picture, 35, 5, 280, 540, $white, 'fonts/Cookie/Cookie-Regular.ttf', trim(ucfirst($getName)));
                    imagettftext($picture, 35, 5, 280, 590, $white, 'fonts/Cookie/Cookie-Regular.ttf', trim(ucfirst($textS)));

                } else {

                    imagettftext($picture, 35, 5, 280, 540, $white, 'fonts/Cookie/Cookie-Regular.ttf', trim(ucfirst($getName . "  " . $textS)));

                }


                // data
                imagettftext($picture, 20, 0, 530, 710, $white, 'fonts/Cookie/Cookie-Regular.ttf', trim(ucfirst($textC.", ".$today)));

                header("Content-type: image/png");

                $generateImage = imagepng($picture, "kartka_swiateczna_wsaib.png");
                //imagedestroy($picture, "kartka_swiateczna_wsaib.png");

            }

        }


    }

    public function location($getName, $getSurname, $getCity){
        return header("Location: index.php?query=done&name=$getName&surname=$getSurname&city=$getCity");
    }

文件格式:

<?php

require_once "generator.php";
?>

<!DOCTYPE html>
<html>
<head>
    <title>Kartka Świąteczna</title>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" >
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="Stylesheet" type="text/css" href="style.css" />

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="./bootstrap-3.3.7-dist/css/bootstrap.min.css">

    <!-- Optional theme -->
    <link rel="stylesheet" href="./bootstrap-3.3.7-dist/css/bootstrap-theme.min.css">

    <!-- Latest compiled and minified JavaScript -->
    <script src="./bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>


</head>
<body style="background-color: #c13213">

<div class="container">
    <div class="row">
        <div class="col-md-8">
            <?php

                @$getName = $_GET['name'];
                @$getSurname = $_GET['surname'];
                @$getCity = $_GET['city'];

            if ( (isset($_GET['send']) && $_GET['send'] === 'card') && (isset($_GET['name']) && $_GET['name'] === $getName) && (isset($_GET['surname']) && $_GET['surname'] === $getSurname)  && (isset($_GET['city']) && $_GET['city'] === $getCity)) {                

                $image = new Generate();
                $image->image($getName, $getSurname, $getCity); // zamiana !!!!!!!
                $image->location($getName, $getSurname, $getCity); // zamiana !!!!!!!


            } if (isset($_GET['query']) && $_GET['query'] === 'done'){

                $saveFile = "kartka_swiateczna_wsaib.png";
                echo '<img src="' . $saveFile . '" id="kartka-photo" width="100%" height="100%"/>'; ?>

                <p><a class="btn btn-success" href="kartka_swiateczna_wsaib.png" download="kartka_swiateczna_wsaib.png">Pobierz kartkę</a></p>

            <?php } else { ?>

                <img src="merry-christmas.png" id="kartka-photo" width="100%" height="100%">

            <?php } 




            ?>
        </div>
        <div class="col-md-4">
            <p id="title">Zaprojektuj kartkę</p>
                <form action="index.php" method="get">

                    <input type="text" class="form-control" placeholder="Podaj imię" id="name" name="name" value="<?php echo $getName; ?>"><div id="counterName"></div><br />
                    <?php if (isset($_GET['error']) && $_GET['error'] === 'name') { ?>

                        <div class="alert alert-danger fade in">
                          <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                          <strong><span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Nie podałeś imienia.</strong>
                        </div>

                    <?php } elseif (isset($_GET['error']) && $_GET['error'] === 'longname'){ ?>

                        <div class="alert alert-danger fade in">
                          <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                          <strong><span class="glyphicon glyphicon-th-list" aria-hidden="true"></span> Maksymalna długość imienia to 20 znaków.</strong>
                        </div>

                    <?php } ?>

                    <input type="text" class="form-control" placeholder="Podaj nazwisko" id="surname" name="surname" value="<?php echo $getSurname; ?>"><div id="counterSurname"></div><br />
                    <?php if (isset($_GET['error']) && $_GET['error'] === 'surname') { ?> 

                        <div class="alert alert-danger fade in">
                          <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                          <strong><span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Nie podałeś nazwiska.</strong>
                        </div>

                    <?php } elseif (isset($_GET['error']) && $_GET['error'] === 'longsurname'){ ?>

                        <div class="alert alert-danger fade in">
                          <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                          <strong><span class="glyphicon glyphicon-th-list" aria-hidden="true"></span> Maksymalna długość nazwiska to 20 znaków.</strong>
                        </div>

                    <?php } ?>

                    <input type="text" class="form-control" placeholder="Podaj miasto" id="city" name="city" value="<?php echo $getCity; ?>"><div id="counterCity"></div><br />
                    <?php if (isset($_GET['error']) && $_GET['error'] === 'city') { ?> 

                        <div class="alert alert-danger fade in">
                          <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                          <strong><span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Nie podałeś nazwy miasta.</strong>
                        </div>

                    <?php } elseif (isset($_GET['error']) && $_GET['error'] === 'longcity'){ ?>

                        <div class="alert alert-danger fade in">
                          <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                          <strong><span class="glyphicon glyphicon-th-list" aria-hidden="true"></span> Maksymalna długość miasta to 15 znaków.</strong>
                        </div>

                    <?php } ?>


                    <button type="submit" class="btn btn-info" name="send" id="send" value="card" style="margin-bottom: 20px;">Generuj</button>
                    <a href="/index.php" class="btn btn-default" role="button" style="margin:  0px 0px 20px 5px;">Odśwież</a>

                    <?php

                    if (isset($_GET['query']) && $_GET['query'] === 'done') { ?>

                        <div class="alert alert-success fade in">
                          <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                          <strong><span class="glyphicon glyphicon-ok" aria-hidden="true"></span> Grafika została wygenerowana poprawnie, możesz ją teraz pobrać kikając na przycisk <i><a href="kartka_swiateczna_wsaib.png" download="kartka_swiateczna_wsaib.png" class="download">Pobierz kartkę</a></i>.</strong>
                        </div>

                    <?php } ?>

                </form>
        </div>
    </div>
</div>

<script type="text/javascript">
var minName = 20;
document.querySelector('#name').onkeyup = function(e){
document.querySelector('#counterName').innerHTML = 
               this.value.length <= minName 
               ? 'Pozostało '+(minName - this.value.length)+' znaków.'
               : 'Imię zbyt długie!';
}

var minSurname = 20;
document.querySelector('#surname').onkeyup = function(e){
document.querySelector('#counterSurname').innerHTML = 
               this.value.length <= minSurname 
               ? 'Pozostało '+(minSurname - this.value.length)+' znaków.'
               : 'Nazwisko zbyt długie!';
}

var minCity = 15;
document.querySelector('#city').onkeyup = function(e){
document.querySelector('#counterCity').innerHTML = 
               this.value.length <= minCity 
               ? 'Pozostało '+(minCity - this.value.length)+' znaków.'
               : 'Nazwa miasta zbyt długa!';
}
</script>

问题在于函数Header Location

return header("Location: index.php?error=longcity&name=$name&surname=$surname");

在GET(在网址中)我只有name=$name&surname=$surname但功能header未返回error=longcity。问题出在哪儿 ?当我添加到url例如?query=done脚本工作正常时。我不知道是错误。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我必须在index.php文件的开头添加:

<?php ob_start(); ?>

http://php.net/manual/en/function.ob-start.php

这是非常好的答案:How to fix "Headers already sent" error in PHP