Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
  #1  
Old 06-01-2009, 02:54 AM
Travis641's Avatar
Travis641 Travis641 is offline
 
Join Date: Nov 2001
Location: USA
Posts: 68
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Displaying Custom Profile Fields on an Image

Hello, I'm trying to set up a signature image creator for users on my website. What I want to do is have the php script, pull a profile field from the database and insert the text onto the image. I found a script to make images, however after modifying it a great deal, I still can't get this to work.

I'm able to get the page itself to display the profile field, however the image refuses to show it. The image also refuses to acknowledge I'm even logged in.

I tried
Code:
$text = $vbulletin->userinfo['field17'];
but I'm not sure that's right regardless.

This is the code for the image script:

Code:
<?php
function measure_string($s,$font,$fh,$ta)
{
    // error_log("measure_string: font ." . $font . ".", 0);
    list($blx,$bly,$brx,$bry,$trx,$try,$tlx,$tly) = imagettfbbox($fh,$ta,$font,$s);
    return max($trx,$brx) - min($tlx,$blx);
}

function measure_char($c,$font,$fh,$ta=0)
{
    $s1 = "mm";
    $m1 = measure_string($s1,$font,$fh,$ta);
    $m2 = measure_string("m" . $c . "m",$font,$fh,$ta);
    return $m2 - $m1;
}

/*
  * Given a string of text, return an array of lines
  * wrapped to fit within line_width
  */
function wrap_text($text, $font, $fontheight, $line_width)
{

    $text_angle = 0;
    $a_chars = array();
    $linelen = 0;
    $line_start = 0;
    $lastpos = 0;    
    $line = "";
    $len = strlen($text);
    for ($i = 0; $i < $len; $i++) {
        if (ctype_space($text[$i]) || ($text[$i] == "\n") || ($i == $len -1)) { // end of a word?
            if (measure_string($line, $font,$fontheight, $text_angle) >= $line_width) { // backup
                if ($lastpos > $line_start) {
                    $text[$lastpos] = "\n";
                    $line_start = $lastpos;
                    $line = substr($text,$lastpos+1, $i - $lastpos); // what if this $line is too long?
                } else {
                    // handle case of no word breaks in line > width
                    // options: punt or insert newline
                    // choice: punt
                    // this code isn't right. maybe the right thing is to just insert a newline _here_.
                    while (($text[$i] != '\n') && !ctype_space($text[$i]) && ($i < $len)) {
                        $i++;
                        $line_start = $lastpos;
                        $line = "";
                    }
                } // end if-else lastpos > line_start
            } else {
                $lastpos = $i; // not too long yet: mark end of last word seen
            }
        }
        if ($text[$i] == '\n') {
            $line_start = $lastpos;
            $line = "";
        } else {
            $line = $line . $text[$i]; // could use substr to get same effect
        }
    
    }

    $arr = explode("\n", $text);
    return $arr;
}

function wrapped_text_image($text="", $font='./fonts/verdana.ttf', $fontheight=16, $color=0xffff00, $bgcolor=0x808080, $width=480, $shadow="no", $border="yes", $bordercolor=0xdedede, $transbg="no" )
{
//    $old_level = error_reporting(6143); //E_ALL

    error_log("Wrapping text '$text'");
    
    $height = 30;
    $textangle = 0;
    
    $v_pad = 6; // pad at top and bottom of image
    $h_pad = 15; // pad at left and right, original is 5

    $line_pad = 6; // pad with each line

    $image = null;

    /* measure a character */
    $textangle = 0;
    list($pos_blx, $pos_bly, $pos_brx, $pos_bry, $pos_trx, $pos_try, $pos_tlx,
        $pos_tly) = imagettfbbox($fontheight, $textangle, $font, "Mg");

    $em_height = $pos_bly - $pos_tly;

    $line_height = $em_height + $line_pad;
    $line_width = $width - $h_pad * 2;

    $arr = wrap_text($text, $font, $fontheight, $line_width);

    $height = $v_pad * 2 + sizeof($arr) * $line_height + 35; //35 to add extra space at bottom!

    /*
    // if only one line, calculate width, otherwise use passed-in width
    if (sizeof($arr) == 1)    // remove it to create fixed witdh even if just 1 line!!
    {
        list($blx, $bly, $brx, $bry, $trx, $try, $tlx, $tly) = 
            imagettfbbox($fontheight, $textangle, $font, $text);
        $width = $brx - $blx + $h_pad*2;
    }
    */

    $image = imagecreate($width, $height);
    $red = ($bgcolor & 0xff0000) >> 16;
    $green = ($bgcolor & 0x00ff00) >> 8;
    $blue = $bgcolor & 0x0000ff;

    if($transbg == "yes"){
        imagecolorallocatealpha($image, $red, $green, $blue, 127); // background color + 127 is transparent!!
    } else {
        imagecolorallocate($image, $red, $green, $blue); // background color
    }

    $red = ($color & 0xff0000) >> 16;
    $green = ($color & 0x00ff00) >> 8;
    $blue = $color & 0x0000ff;

    $textcolor = imagecolorallocate($image, $red, $green, $blue);
    
    //text shadow
    $shadowcolor = imagecolorallocate($image, 193, 193, 193);
    
    if($border == "yes"){
        //border color
        $red = ($bordercolor & 0xff0000) >> 16;
        $green = ($bordercolor & 0x00ff00) >> 8;
        $blue = $bordercolor & 0x0000ff;
        $bordercolors = imagecolorallocate($image, $red, $green, $blue);
        //$bordercolors2 = imagecolorallocate($image, 222, 222, 128);
        
        //border
        $x = 0;
        $y = 0;
        $w = $width - 1;
        $h = $height - 1;
        imageline($image, $x,$y,$x,$y+$h,$bordercolors); //left
        imageline($image, $x,$y,$x+$w,$y,$bordercolors); //top
        imageline($image, $x+$w,$y,$x+$w,$y+$h,$bordercolors); //right
        imageline($image, $x,$y+$h,$x+$w,$y+$h,$bordercolors); //bottom
        //end border
    }

    $line_x = $h_pad;
    $line_y = $line_height /* +  $line_pad*/ + 10; // 10 is extra padding

    foreach ( $arr as $s )
    {
        if($shadow == "yes") { //shadow generated by double image with different axis and color
        imagettftext($image, $fontheight, $textangle, $line_x + 2, $line_y + 2, $shadowcolor, $font, $s);
        }
        
        imagettftext($image, $fontheight, $textangle, $line_x, $line_y, $textcolor, $font, $s);
        $line_y = $line_y + $line_height; 
    }
    
    // put some stuff in to make it hard to ocr the text
//    $w = 0;
//    for ($w = $width, $h = $height; $w > 0 && $h > 0; $w = $w - $width / 5, $h = $h - $height / 5)
//        imageellipse($image, $width/2, $height/2, $w, $h, $textcolor);

//    error_reporting($old_level);

    return $image;
}

function show_image($image)
{
    header("Content-type: image/png");
    imagepng($image);
}

// generate and show the image, then destroy it.
function get_image($text, $font="bookos.ttf", $fontheight=16, $color=0, $bgcolor=0xffffff, $width=480 )
{
    $image = wrapped_text_image($text, $font, $fontheight, $color, $bgcolor, $width);
    if ($image) {
        show_image($image);
        imagedestroy($image);
        return true;
    } else {
        return false;
    }
}

?>
Any help is appreciated.
Reply With Quote
  #2  
Old 06-01-2009, 05:22 AM
Dismounted's Avatar
Dismounted Dismounted is offline
 
Join Date: Jun 2005
Location: Melbourne, Australia
Posts: 15,047
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You need to include vBulletin's global.php for any vBulletin variables to work.
Reply With Quote
  #3  
Old 06-01-2009, 06:49 AM
Travis641's Avatar
Travis641 Travis641 is offline
 
Join Date: Nov 2001
Location: USA
Posts: 68
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I have it included in the page that is calling the script itself, including it in the script returns the error "headers already sent, cookies cannot be sent" but it still refuses to call the profile field for anyone, except it will show mine for everyone if I use the code below.

Code:
$text = $vbulletin->db->query_first("SELECT field17 FROM " . TABLE_PREFIX . "userfield");
    $text = ($vbulletin->userinfo['field17']);
Anyone have any ideas?
Reply With Quote
  #4  
Old 06-01-2009, 02:34 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

This is field 17 for *you*, not someone else -
PHP Code:
$text $vbulletin->userinfo['field17']; 
Reply With Quote
  #5  
Old 06-01-2009, 07:17 PM
Travis641's Avatar
Travis641 Travis641 is offline
 
Join Date: Nov 2001
Location: USA
Posts: 68
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I'm not sure what you're trying to say.

I must correct myself in my last post, as it was 4am and I was tired of looking at the code. I meant to say that this code causes my field17 to show up for everyone that generates the form:

Code:
    $text = $vbulletin->db->query_first("SELECT field17 FROM " . TABLE_PREFIX . "userfield");
    $text = ($text['field17']);
Using the above code in place of the second line, simply returns a blank space.

I'm unable to call the profile field for the current logged in user, and instead the query seems to only show the field for the first userid in the database.

I guess what I should be asking is how to get the script to access the cookie of vbulletin so it can pull from the database for each user, if that makes any sense. I'm sure it's probably my query itself that's off, so that might be the main problem.
Reply With Quote
  #6  
Old 06-01-2009, 07:41 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Actually, that query just grabs field17 for some random user. Nowhere in that query do you say to grab it for a particular user - you have no WHERE statement in that query (like WHERE userid = 'whatever'). Are you trying to grab field17 for the person accessing the page or who?

I don't know anything about accessing cookies, sorry.
Reply With Quote
  #7  
Old 06-01-2009, 08:08 PM
Travis641's Avatar
Travis641 Travis641 is offline
 
Join Date: Nov 2001
Location: USA
Posts: 68
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I'm trying to grab it for the user who presses the "generate image" button on the form. So yes, to the person accessing the page.
Reply With Quote
  #8  
Old 06-01-2009, 08:12 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Then you need something like WHERE userid = $vbulletin->userinfo['userid'] in the query (correct punctuation not included).
Reply With Quote
  #9  
Old 06-01-2009, 09:09 PM
Travis641's Avatar
Travis641 Travis641 is offline
 
Join Date: Nov 2001
Location: USA
Posts: 68
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Ok, I was finally able to get the profile field on the image.

Thanks for your help.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:35 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04274 seconds
  • Memory Usage 2,248KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (4)bbcode_code
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (9)post_thanks_box
  • (9)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (9)post_thanks_postbit_info
  • (9)postbit
  • (9)postbit_onlinestatus
  • (9)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete