2012-03-29 130 views
0

我以前问过为什么\ n在一个字符串中没有返回换行符。imagettftext \ n换行符第2部分

这是因为字符串需要在双引号中。 (!感谢@Juhana)

现在我使用GET这样我就可以在URL

$text = $_GET["msg"]; 

和URL更改文本字符串:

http://www.mywebsite.co.uk/image.php?msg=Something\nElse 

结果在输出文本“的东西\ \ nElse”
但我要的是一个换行符是这样的:
‘一些
否则’

为什么文本仍然有这两个反斜杠?
我认为这是错误的,因为“味精”在双引号应该创建一个换行符?

感谢

EDIT完整的PHP代码

<?php 
// Set the content-type 

头( '内容 - 类型:图像/ PNG');

// Create the image 
$im = imagecreatetruecolor(400, 100); //image big enough to display two lines 

// Create some colors 
$black = imagecolorallocate($im, 0, 0, 0); 
imagefilledrectangle($im, 0, 0, 399, 29, $white); 

// The text to draw 

//$text = 'Testing\ntesting'; using SINGLE quotes like this results \\n 
//$text = "Testing\ntesting"; using DOUBLE quotes like this results in line break 

$text = $_GET["msg"]; 

// Replace path by your own font path 
$font = 'arial.ttf'; 


// Add the text 
imagettftext($im, 20, 0, 10, 20, $black, $font, $text); 

// Using imagepng() results in clearer text compared with imagejpeg() 
imagepng($im); 
imagedestroy($im); 
?> 
+0

你试过用'
'而不是'\ n'??? – Ace 2012-03-29 13:32:01

+0

是的,它只是显示
就好像这就是我想让图片中的文字说的那样。 – mattneedshelp 2012-03-29 13:35:25

+0

请发布完整的PHP代码 – Ace 2012-03-29 13:38:33

回答

1

我认为magic_quotes在配置上是ON。这就是为什么当有特殊字符如\(反斜杠)时自动添加斜线。

if(get_magic_quotes_gpc()) 
    $text = stripslashes($_GET['msg']); 
else 
    $text = $_GET['msg']; 

但是,如果你要在现场应用中使用这些,你需要避免 安全问题的详细验证。如果您发送带有特殊字符或html标签的文本更好使用POST方法或至少哈希代码和崇敬。您的网址:

http://url.com/image.php?msg=<?php echo base64_encode('your text\n test');> 

而且在image.php

$text = isset($_GET['msg']) ? $_GET['msg'] : 'default text'; 
$text = base64_decode($_GET['msg']); 
+0

非常有趣!现在它使用单斜杠'\ n'输出文本,但图像在图像中显示此文本而不是创建换行符。任何想法为什么? – mattneedshelp 2012-03-29 13:55:04

+0

更新,请检查 – safarov 2012-03-29 14:00:53

+0

我不明白在哪里把新的代码?我还需要那个'魔术引号'的代码吗?它似乎编码我的网址,但显示图像编码,而不是解码。 – mattneedshelp 2012-03-29 14:12:57

-2

做ütryed使用<br/>代替\n

http://php.net/nl2br

哪儿来ü输出的文本?在HTML?

g.r.

+0

'nl2br'显示html标记'
'代替'\ n'。我想知道如何编写这个URL,这样'\ n'就可以工作,不会显示两个斜杠'\\ n' – mattneedshelp 2012-03-29 13:42:29

+0

想法是你可以在GET之后使用nl2b。 你也可以做一个简单的preg_replace http://php.net/manual/de/function.preg-replace.php 所以你可以搜索'\\ n'并用'\ n'代替它。 – Ace 2012-03-29 20:20:54

+0

@Ace需要? – mattneedshelp 2012-03-30 08:26:26

0

使用PHP_EOL像这样:

header('Content-Type: image/jpeg'); 
$im = imagecreatefromjpeg('000.jpg'); 
$text = "Hello my name is degar007".PHP_EOL."and I am glad to see you"; 
imagettftext($im, 10, 0, 100, 100, 0x000000, 'DejaVuSans.ttf', $text); 
imageJpeg($im); 
imagedestroy($im); 

喝彩!