How to handle equals char when laying text over images with GD and imagecreatefrompng

Posted on 2006-05-14
Last Modified: 2008-03-06
Hey there, I have the following:

$str1 = "=I=Test";

And some image code:

$dest_img = @imagecreatefrompng("minisigback.png");
imagettftext($dest_img, 20, 0, 16, 24, $black, $font_file, $str1);

Problem is it fails (not surprisingly) with the = char when found in $str1. I thought of using addslashes, but = is not a trigger for addslashes. Any idea?
Question by:Dodger42

    Author Comment

    OK I found the problem and fixed that first part, but now I have the following problem. Because I want to save the image as $str1.png it fails due to the equals char. I made a workaround:


    that gives me a unique string that is acceptable as filename, buts its long and hard to remember.

    Any better ideas?
    LVL 5

    Accepted Solution

    $str1 = preg_replace('/[^a-z0-9]/i','',$str1); //Removes all but a-z A-Z 0-9

    Author Comment

    Hmm ok. I just used that, and although it isn't perfect, the output filename is much more legible than the md5. Will leave this open for a little longer, but this will work for me if need be.

    LVL 40

    Expert Comment

    Replace = with _ maybe? Why does the filename have to contain an = sign?
    LVL 2

    Expert Comment

    to just remove the = use:
    $str1 = string_replace("=", "", $str1);

    Author Comment

    Sorry I didnt explain the problem well enough. $str1 actually derives from a log file of a game. It may contain all sorts of strange characters and I cannot change that. The script will generate an image file. For ease of use, that image file would be $str1.png (intuitive). It probably a limitation of windows filenames and php that i cant work around too easily.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now