PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 11, 2022

[FIXED] How to keep user submitted quotes as quotes in a picture

 October 11, 2022     gd, php, string     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing