Issue
Hopefully someone will be able to tell me how to keep user quotes when generating an image. What I mean is if a user submits words like:
don't
or
John said "hello"
how can I keep them in the generated image. Currently it turns to
don\t
Here is my code:
<?php
function formattextimg( $text, $width = 960, $color = array( 0, 0, 0 ),$bgcolor = array( 255, 255, 255 ), $font = 2 ) {
$width = ( ( isset( $width ) && is_numeric( $width ) ) ? ( ( $width >= 100 ) ? (int)$width : 100 ) : 960 );
$text = str_replace( array( '<b>', '</b>', '<strong>', '</strong>' ), '|', $text );
$text_b = preg_split( '/\|/', $text, -1, PREG_SPLIT_OFFSET_CAPTURE );
foreach($text_b as $k => $tb){if($k%2!=0){$textbold[($tb[1]-1)]=$tb[0];}}
$text = str_replace('|','',$text);
for( $i = 0; $i < strlen( $text ); $i++ ) {
if( $string_c >= ( $width - ( (int)(imagefontwidth( $font ) / 2 ) ) ) ) {
$space = strrpos( $string, ' ' );
$string_sub = substr( $string, 0, $space );
$i = $i - ( strlen( $string ) - $space ) + 1;
$strings[] = $string_sub;
$string = '';
$string_c = 0;
}
$string .= $text{$i};
$string_c += imagefontwidth( $font );
}
$strings[] = $string;
$im = imagecreatetruecolor( $width, ( imagefontheight( $font ) * ( count( $strings ) ) ) );
imagesavealpha( $im, true );
$trans = imagecolorallocate( $im, $bgcolor[0], $bgcolor[1], $bgcolor[2] );
imagefill( $im, 0, 0, $trans );
$color = imagecolorallocate( $im, $color[0], $color[1], $color[2] );
$black = imagecolorallocate( $im, 0, 0, 0 );
foreach( $strings as $pos => $string ) {
imagestring( $im, $font, 5, ( $pos * imagefontheight( $font ) ), $string, $color );
}
$len = 0;
foreach( $strings as $pos => $string ) {
$len += ( strlen( $string ) + 1 );
if( count( $textbold ) > 0 ) {
foreach( $textbold as $cpos => $word ) {
if( $cpos <= $len ) {
$wpos = strpos( $string, $word );
if( $wpos !== false ) {
imagestring( $im, $font,
( $wpos * imagefontwidth( $font ) )+1,
( ( $pos ) * imagefontheight( $font ) ),
$word, $color
);
unset( $textbold[$cpos] );
}
}
}
}
}
return $im;
}
header( 'Content-type: image/png' );
imagepng( formattextimg( $text,300,array( 255, 255, 255 ),array( 128, 128, 128 ),16 ) );
exit;
?>
If that is not possible can someone please tell me how to remove the:
'
and
"
I tried:
$text=$_REQUEST['text'];
$text=str_replace('"', "", $text);
$text=str_replace("'", "", $text);
But it did not work.
thanks
Solution
This is caused by magic_quotes_gpc being enabled on the server.
You can reverse the effect of magic_quotes_gpc
by calling stripslashes
if get_magic_quotes_gpc
(manual) returns true:
$text = $_REQUEST['text'];
if (get_magic_quotes_gpc())
{
$text = stripslashes($text);
}
It's important that you actually check the value of get_magic_quotes before calling stripslashes, as this feature has been deprecated in PHP 5.3 and removed altogether from PHP 5.4.
Answered By - MatsLindh Answer Checked By - Candace Johnson (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.