andy's php projects
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Script and site usage:
All of the scripts on this site are provided free for non-commerical, personal or non-profit use. If you find these scripts useful to you, appreciate the free support, or are just an all round nice person, then why not donate a little dosh? If you wish to use any of these in a commercial system, please refer to the payment plan.


TEXT TO IMAGE

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    php.amnuts.com Forum Index -> Drop Shadowed Thumbnail Creator
View previous topic :: View next topic  
Author Message
mmjaeger
Light Poster


Joined: 22 Oct 2004
Posts: 25

PostPosted: Mon Nov 01, 2004 10:21 am    Post subject: TEXT TO IMAGE Reply with quote

does anybody have an idea how the following function could be integrated into the drop shadow class - thanks in advance for your input

Code:
<?php
// Set the static variables (discussed in steps 2 and 3)
//$font_file = $_SERVER['DOCUMENT_ROOT']."/path/verdana.ttf";
$font_file = "arial.ttf";
$font_size = 8;
$angle = 0;
$text = "© net4visions.com";

$img = 'test1.jpg';
// The dummy image hack (discussed in step 1)
$im_size = getimagesize($img);
$image_width = $im_size[0];
$image_height = $im_size[1];

$im = imagecreatetruecolor($image_width, $image_height);
$white = imagecolorallocate($im, 255, 255, 255);

$im2 = imagecreatefromjpeg($img);

//imagecopy($im, $im2, 0, 0, 0, 0, $image_width, $image_height);
imagecopyresampled($im, $im2, 0, 0, 0, 0, $image_width, $image_height, $image_width, $image_height);
imagedestroy($im2);

// Position the text (discussed in step 3)
$bounding_box = imagettfbbox($font_size, 0, $font_file, $text);
$x_start = $image_width - $bounding_box[2] - $bounding_box[0] - 10;
$y_start = $image_height - 10;

// Write the text to the image
imagettftext($im, $font_size, $angle, $x_start, $y_start, $white, $font_file, $text);

// Create and then destroy the image (discussed in steps 5 and 6)
header("Content-type: image/jpeg");
imagejpeg($im);
imagedestroy($im);
?>
Back to top
View user's profile Send private message
amnuts
Site Admin


Joined: 01 Sep 2002
Posts: 662
Location: East Sussex, England

PostPosted: Mon Nov 01, 2004 12:17 pm    Post subject: Reply with quote

Might be fairly straight-forward...

Put all of that code into a new method into the class.

Remove the lines:

Code:
$im = imagecreatetruecolor($image_width, $image_height);'


and

Code:
// Create and then destroy the image (discussed in steps 5 and 6)
header("Content-type: image/jpeg");
imagejpeg($im);
imagedestroy($im);


Replace instances of the $im variable with $this->_imgFinal (or $this->_imgShadow).

There ya go. Seems about right to me, but you may need to tweak a little.

Andy
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
mmjaeger
Light Poster


Joined: 22 Oct 2004
Posts: 25

PostPosted: Mon Nov 01, 2004 12:55 pm    Post subject: text to image Reply with quote

I had some difficulties with this part in particular:

$bounding_box = imagettfbbox($font_size, 0, $font_file, $text);
$x_start = $image_width - $bounding_box[2] - $bounding_box[0] - 10;
$y_start = $image_height - 10;
Back to top
View user's profile Send private message
amnuts
Site Admin


Joined: 01 Sep 2002
Posts: 662
Location: East Sussex, England

PostPosted: Tue Nov 02, 2004 3:37 am    Post subject: Re: text to image Reply with quote

mmjaeger wrote:
I had some difficulties with this part in particular:

$bounding_box = imagettfbbox($font_size, 0, $font_file, $text);
$x_start = $image_width - $bounding_box[2] - $bounding_box[0] - 10;
$y_start = $image_height - 10;


Had difficulties as in you've now fixed the problem? Or are still trying to fix it? Any error messages?
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
mmjaeger
Light Poster


Joined: 22 Oct 2004
Posts: 25

PostPosted: Tue Nov 02, 2004 4:18 am    Post subject: Re: text to image Reply with quote

amnuts wrote:
mmjaeger wrote:
I had some difficulties with this part in particular:

$bounding_box = imagettfbbox($font_size, 0, $font_file, $text);
$x_start = $image_width - $bounding_box[2] - $bounding_box[0] - 10;
$y_start = $image_height - 10;


Had difficulties as in you've now fixed the problem? Or are still trying to fix it? Any error messages?


This is how I finally got it to work:

Code:
     function addTtf ($text, $bgcolor) {
       $font_file = "fonts/arial.ttf";
      $font_size = 8;
      $angle = 0;      
      $iw = @ImageSX($this->_imgFinal);
      $ih = @ImageSY($this->_imgFinal);

       $black = imagecolorallocate($this->_imgFinal, 0, 0, 0);
       $white = imagecolorallocate($this->_imgFinal, 255, 255, 255);
      
      // pre-process the image
      $background = $this->_htmlHexToBinArray($bgcolor);
       $wdtcolor = imagecolorallocate($this->_imgFinal, $background[0], $background[1], $background[2]);

      // Position the text
      $bounding_box = imagettfbbox($font_size, 0, $font_file, $text);
      $xs = $iw - $bounding_box[2] - $bounding_box[0] - 10;
      $ys = $ih - 10;

      // Write the text to the image
      imagettftext($this->_imgFinal, $font_size, $angle, $xs, $ys, $wdtcolor, $font_file, $text);
    }   



if you see any room for optimization, please let me know.

thanks for your help.

by the way, I hope you're going to answer my other questions in your forum as well - I'm currently a little stucked.

Thanks again
Back to top
View user's profile Send private message
amnuts
Site Admin


Joined: 01 Sep 2002
Posts: 662
Location: East Sussex, England

PostPosted: Tue Nov 02, 2004 5:17 am    Post subject: Re: text to image Reply with quote

mmjaeger wrote:
if you see any room for optimization, please let me know.


Are these two lines needed?

Code:
       $black = imagecolorallocate($this->_imgFinal, 0, 0, 0);
       $white = imagecolorallocate($this->_imgFinal, 255, 255, 255);


I don't see you applying the colours anywhere, but I haven't really used the ttf functions so they may be required for something or other. If not, drop them and save yourself a couple executions.

mmjaeger wrote:
by the way, I hope you're going to answer my other questions in your forum as well - I'm currently a little stucked.


As and when I can... This board and the free code is a hobby for me. I certainly don't get paid for the help I try to give! When I get a bunch of questions posted on the board _and_ emailed to me, I have to try to juggle that around my existing priorities (like my 9-5 job and the freelance work I try to do to pay the bills).

Incidently; you've asked questions about most of the image classes on this site, and others... Are you planning something big with them? Would be interesting to find out how people use the classes.

Regards,

Andy
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    php.amnuts.com Forum Index -> Drop Shadowed Thumbnail Creator All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group