2016-07-15 93 views
1

我目前正在學習代碼,我正在爲自己做一個小項目。我目前正在用html5和php來試用這個項目。這是我想要做的:試圖從TXT輸入創建一個PNG文件PHP

我希望用戶輸入他們的信息,如姓名,地址,電話號碼等。然後,我想抓取這些信息,並以某種方式將其放到.png文件中,以便他們可以保存該圖片。我已經有了獲取文本的代碼,但是我被困在了下一部分。

這裏是我用來獲取信息的部分代碼:

$first = $_REQUEST['first']; 
$last = $_REQUEST['last']; 
$email = $_REQUEST['email']; 
$address = $_REQUEST['address']; 
$city = $_REQUEST['city']; 

我希望這個問題是有道理的。

+0

檢查這個http://stackoverflow.com/questions/8429011/how-to-write-the-text-on-top-of-image-in-html5-canvas – danielarend

+0

這裏有一個小文章,你可以(https://en.m.wikipedia.org/wiki/POST_(HTTP)) – Jek

+0

請參閱[使用PHP創建PNG](http://php.net/manual/) EN/image.examples-png.php)。 – showdev

回答

0

這是使用PHP GD庫完成的。

您需要一張PNG圖像才能開始。你可以選擇任何東西,例如100 x 100 canvas.png,只有白色的東西(所以你可以讀取黑色文字)。

也找到一個字體來寫文字。將其保存在與腳本相同的目錄中。

<?php 
    // fetch the text to write 
    $first = $_REQUEST['first']; 
    $last = $_REQUEST['last']; 
    $email = $_REQUEST['email']; 
    $address = $_REQUEST['address']; 
    $city = $_REQUEST['city']; 

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

    $canvas = imagecreatefrompng('canvas.png'); // let's suppose canvas.png is a 100x100 pure white PNG 

    $black = imagecolorallocate($canvas, 0, 0, 0); // create a color object (choosing black for a white canvas!) 

    $font_path = './myfont.ttf'; // select font 

    // imagettftext (resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text) 

    imagettftext($canvas, 10, 0, 10, 10, $black, $font_path, $first); // place text on canvas 
    imagettftext($canvas, 10, 0, 10, 25, $black, $font_path, $last); // place text on canvas 
    imagettftext($canvas, 10, 0, 10, 40, $black, $font_path, $email); // place text on canvas 
    imagettftext($canvas, 10, 0, 10, 55, $black, $font_path, $address); // place text on canvas 
    imagettftext($canvas, 10, 0, 10, 60, $black, $font_path, $city); // place text on canvas 

    imagepng($canvas); // send image to browser 

    imagedestroy($canvas); // clear memory 
?>