Results 1 to 5 of 5

Thread: Write text over (existing) image

  1. #1
    Join Date
    Sep 2010
    Location
    Hi Stalker.
    Posts
    148
    Thanks
    16
    Thanked 3 Times in 3 Posts

    Default Write text over (existing) image

    How could I use and/or transform this code to work with an existing image? I'd like for someone to be able to type in text and it would show up on an existing image.
    PHP Code:
    <?php
    // Create image handle
    $im imagecreatetruecolor(200200);

    // Allocate colors
    $black imagecolorallocate($im000);
    $white imagecolorallocate($im255255255);

    // Load the PostScript Font
    $font imagepsloadfont('font.pfm');

    // Write the font to the image
    imagepstext($im'Sample text is simple'$font12$black$white5050);

    // Output and free memory
    header('Content-type: image/png');

    imagepng($im);
    imagedestroy($im);
    ?>
    Please. Thanks!
    EDIT: Also, how can I change the font.pfm to a font.ttf? Thanks!

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Regarding the font, look here: http://www.php.net/manual/en/function.imagettftext.php
    Looks like you can remove the "load font" line and replace the existing line with that.


    To use an existing image, you will replace imagecreatetruecolor() with imagecreatefromjpeg(), or ...gif(), ...png(), etc.
    Here's the one for jpeg as an example:
    http://www.php.net/manual/en/functio...tefromjpeg.php


    I can also share with you some image functions that I use to simplify the process of working with images of various filetypes. If you always use the same filetype (such as jpeg) then this won't be important, but if you are using user-uploaded images, then this is a good way to approach it:
    PHP Code:
    function imagefiletype($file) { //use either to find out how to check the image or if you already know it's a valid image to find out what type it is
        
    $file basename($file);
        
    $ext strpos($file,'.')!==FALSE?substr($file,strrpos($file,'.',1)+1):$file//find extension: defined as part of name after the last .
        
    $ext strtolower($ext); //case insensitive extensions
        
    if ($ext=='jpg'||$ext=='jpeg'||$ext=='jpe') {
            return 
    'jpg';
        }
        else if (
    $ext=='gif') {
            return 
    'gif';
        }
        else if (
    $ext=='png') {
            return 
    'png';
        }
        return 
    FALSE;
    }

    function 
    imagecreatefromfile($file,$path='') {
        if (
    $path=='') { $path=$file; } //in almost all cases, except file uploads [where name!=tmp_path]
        
    $type imagefiletype($file);
        
    $im FALSE//default
        
    if ($type=='jpg') {
            
    $im imagecreatefromjpeg($path);
        }
        else if (
    $type=='gif') {
            
    $im imagecreatefromgif($path);
        }
        else if (
    $type=='png') {
            
    $im imagecreatefrompng($path);
        }
        return 
    $im;

    By the way, you can also use bmp format images if you google "imagecreatefrombmp php" since there are some functions out there that are written to deal with bmp images even though it's not supported natively. There aren't many other formats supported though.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Sep 2010
    Location
    Hi Stalker.
    Posts
    148
    Thanks
    16
    Thanked 3 Times in 3 Posts

    Default

    Thanks. But a couple things, a user will upload an image, but it will be on a certain part of the image. And, how can I use this? Sorry, I know .000000001% of any PHP. :P
    Last edited by [Nicolas]; 02-06-2011 at 02:08 AM.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Here's a function that can crop an image as desired:
    http://www.php.net/manual/en/function.imagecopy.php

    Start experimenting with PHP and you can learn it quickly.
    Be warned, though, that the GD image library is one of the hardest parts. However, having examples like that to start with will help. Don't give up on PHP if the image functions are too hard, since that's true for everyone using them
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Sep 2010
    Location
    Hi Stalker.
    Posts
    148
    Thanks
    16
    Thanked 3 Times in 3 Posts

    Default

    Thanks!!! I will start as soon as possible, I just found out my webhost DOES NOT support PHP anymore Let me google one real quick. But thanks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •