Can you please let me know how I can Strikethrough a generated text on captcha image in PHP? I have a code to create text on an image as below

<?php
$img          = imagecreatetruecolor(200, 200);
$bgColor      = imagecolorallocate($img, 255, 255, 255);
$netColor     = imagecolorallocate($img, 185, 185, 185);
$txtColor     = imagecolorallocate($img, 0, 0, 0);
imagefill($img, 0, 0, $bgColor);
imagettftext($img, 22, 0, 50 ,45, $txtColor,"Bedizen.ttf","Expert" );   
header("Content-type:image/png");
imagepng($img);
imagedestroy($img);
?>

and I would like to make the text like Strikethrough. i know one way is adding a line on the image but just wondering if we can add style to the text in PHP or not?
Thanks for your time and comments.

Member Avatar for LastMitch

and I would like to make the text like Strikethrough. i know one way is adding a line on the image but just wondering if we can add style to the text in PHP or not?

You can do that on html but for captcha image. The asnwer is NO. It's an image.

These are php captcha:

http://www.phpcaptcha.org/try-securimage/

If you need a Strikethrough on the image then it has to be an image that you created a line for the background.

This is more like a design thing rather than a PHP issue.

Member Avatar for LastMitch

Maybe I ask what is the code to Strikethrough? Since I got downvote that means someone has an answer to that??

Care to share? If I find out did that I will downvote you too.

If I find out did that I will downvote you too.

You do? Didn't think you would, after experiencing the effects.

Anyway, what may be the cause, is that the question is about a strikethrough option for imagettftext. I haven't seen it done yet (nor any other styles), so I think drawing the line yourself is the only option.

Member Avatar for diafol

Having looked at gd functions recently, I can't remember seeing anything for strikethrough. I doubt whetehr imagemagick has either. This will be quite fiddly I think.

Since I got downvote that means someone has an answer to that?? ... If I find out did that I will downvote you too.

Let it go. Life's too short. Probably a fly-byer anyway.

I hope this helps you. I realized it this way. Look at addStrikethrough() function

    <?php
    $productPrice = $_SERVER["QUERY_STRING"];
    createPriceProductImage($productPrice);

    function createPriceProductImage($price) {
        header('Content-Type: image/png');

        if (empty($price)){
            $text = '';
        } else {
            $roundedPrice = round($price*1.08);
            $text = ''. $roundedPrice . ' руб.';
        }

        // Создание изображения
        $im = imagecreatetruecolor(180 , 40); 
        $white = imagecolorallocate($im, 255, 255, 255);
        $red = imagecolorallocate($im, 255, 0, 0);
        imagefilledrectangle($im, 0, 0, 180, 40, $white);
        $font = 'Roboto-Regular.ttf';
        imagettftext($im, 26, 0, 5, 32, $red, $font, $text);

        if (!empty($price)) {
            addStrikethrough($im, $font, getUnderline());
        }

        imagepng($im);
        imagedestroy($im);
    }

    function addStrikethrough($image, $font, $underline) {
        $grey = imagecolorallocate($image, 167, 167, 167);
        imagettftext($image, 26, 15, 0, 36, $grey, $font, $underline);
    }

    function getUnderline() {
        $underline = '&#95;'; // '&#95;' symbol equal '_'
        $length = 8;
        $complexUnderline = '';
        for ($i = 0; $i < $length; $i++) {
            $complexUnderline = $complexUnderline . $underline;
        }
        return $complexUnderline;
    }

    ?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.