2017-08-08 38 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