2017-08-08 42 views
0

我無法合併這些代碼。我已經測試但不工作(錯誤)。請幫助我如何創建一個image.php + imagebackground

顯示IP

<?php 
    $img_number = imagecreate(275,225); 
    $backcolor = imagecolorallocate($img_number,102,102,153); 
    $textcolor = imagecolorallocate($img_number,205,205,205); 
    imagefill($img_number,0,0,$backcolor); 
    $number = " Your IP is $_SERVER[REMOTE_ADDR]"; 
    Imagestring($img_number,10,5,5,$number,$textcolor); 
    header("Content-type: image/jpeg"); 
    imagejpeg($img_number); 
    ?> 

+背景圖

<?php 
$im = imagecreatefrompng("imagetest.png"); 
header('Content-Type: image/png'); 
imagepng($im); 
imagedestroy($im); 
?> 
在一個文件中(image.php)/請幫我

/感謝

回答

0

什麼y你想要做的是用第二個腳本中的第一行代替第一個腳本中的前兩行(除了<?php)和第四行。

在第一個腳本中,您正在創建圖像資源,創建一個顏色資源,然後填充背景。相反,您希望從圖像創建圖像資源並使用該圖像填充背景。

試試這個:

<?php 
    $img = imagecreatefrompng("imagetest.png"); 
    $textcolor = imagecolorallocate($img,205,205,205); 
    $number = "Your IP is {$_SERVER[REMOTE_ADDR]}"; //Also note the interpolation here. Not neccesary but it is nicer 
    imagestring($img,10,5,5,$number,$textcolor); 
    header("Content-type: image/jpeg"); 
    imagejpeg($img); 
    imagedestroy($img); 

我沒有測試過這一點,但是與已經做過類似的事情我自己,我相信它會成功。然而,我會建議,不要硬編碼文本的位置,你可以根據圖像的大小計算放置它的最佳位置。看看getimagesize函數。

+0

謝謝你的回答 – tmmovie