当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Error When using imagettftext() with '&' in text and pass it in url and add it on image

问题描述:

I am using below code to write text and change color,size,font and add it on image.

<?php

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

$im = imagecreatefrompng($imgPath);

$color1 = imagecolorallocate($im, $rgb1[0], $rgb1[1], $rgb1[2]);

$nameFont = FONTS_PATH . $namefontFilePath;

$text = 'Testing...'; // Working fine

imagettftext($im, 20, 0, 10, 20, $color1, $nameFont, $text);

$newtext = 'Testing & Demo'; // Not Working

imagettftext($im, 20, 0, 10, 20, $color1, $nameFont, $newtext);

imagepng($im);

imagedestroy($im);

?>

1) When I am using '&' it will not giving desire output.

Wrong Output:

test &amp; Demo

2) When i am passing name in url and get it in $_GET['name'] at that time it will giving below output on image.

Page URL : "http://localhost/imagetest.php?name=Test+%26amp%3B+Demo"

$text = $_GET['name'];

imagettftext($im, 20, 0, 10, 20, $color1, $nameFont, $text);

Wrong Output:

test

//Only test will appear and it will remove text from `&`.

Correct output for both:

test & Demo

I have tried htmlentities(),trim(),urlencode() but didnt get desire output.

Please help me to solve it. Thanks in advance!

分享给朋友:
您可能感兴趣的文章:
随机阅读: