FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP Image functions





The-Master
OK, I have made this script so far for images. I know it is simple but I wanted to know if there was a way to make there be options for the text such as colour, font, text location and the image it rights on. Thanks Very Happy
ninjakannon
I'm afraid I can't help you here, although I expect (but am not sure) that you can change at least some of the things you ask for.

Anyway, the point in me posting: I've often wondered how you do this with php, and have never found out. Would you mind telling me how to do it? Or telling me your code. Although I have no purpose for it yet I'm really interested in how you manage to do that and would love to know and expand my knowledge of php. Smile
sonam
I don't know how is looking your php script but I think it is possible to change some thing.

Sonam
The-Master
My script looks like this:

It is quite simple and I created it with my fairly basic knowedge of php.

Code:
<?php
header("Content-type: image/gif");

$fontsize = 18;
if(@$_GET['fontsize']) {
   $fontsize = $_GET['fontsize'];
}
$font = 'Halo.ttf';
$text = @$_GET['text'];
$size = imagettfbbox($fontsize, 0, $font, $text);
$width = $size[2] + $size[0] + 8;
$height = abs($size[1]) + abs($size[7]);
$im = imagecreate($width, $height);
$colourBlack = imagecolorallocate($im, 255, 255, 255);
imagecolortransparent($im, $colourBlack);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefttext($im, $fontsize, 0, 0, abs($size[5]), $white, $font, $text);
imagegif($im);
imagedestroy($im);
?>


So is there any way to alter it to get it to allow custom images, font colour, font or text location.

Thanks Very Happy
ninjakannon
Thanks for the code.

I was gonna try and play around with it a bit, but I realise that I don't have the font file (which I could sort by finding one on my computer and using that instead). But there are other errors that I get which I don't know how to solve quickly so I can't try and figure out how to change things.

Here are my errors:
Code:
Warning: imagettfbbox() [function.imagettfbbox]: Could not find/open font in /home/username/domains/site.com/public_html/file.php on line 10

Warning: imagecreate() [function.imagecreate]: Invalid image dimensions in /home/username/domains/site.com/public_html/file.php on line 13

Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/username/domains/site.com/public_html/file.php on line 14

Warning: imagecolortransparent(): supplied argument is not a valid Image resource in /home/username/domains/site.com/public_html/file.php on line 15

Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/username/domains/site.com/public_html/file.php on line 16

Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/username/domains/site.com/public_html/file.php on line 17

Warning: imagefttext() expects parameter 1 to be resource, boolean given in /home/username/domains/site.com/public_html/file.php on line 18

Warning: imagegif(): supplied argument is not a valid Image resource in /home/username/domains/site.com/public_html/file.php on line 19

Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/username/domains/site.com/public_html/file.php on line 20

How can I fix these so that I can experiment with your code?

I hope someone - if not me - can help you on this.
PseudoKnight
Surely you didn't "make" that script if you don't know how to get a variable for text sizes and such. The answer is right there in your code.

Code:
$text = @$_GET['text'];


Do the same for the other variables. So, for instance, if you wanted a form to send a different text size to the script, change appropriate line to:

Code:
$fontsize = @$_GET['fontsize'];


And add a 'fontsize' field to your form.
ninjakannon
What does putting the "@" character in front of the $_GET do?
The-Master
PseudoKnight wrote:
Surely you didn't "make" that script if you don't know how to get a variable for text sizes and such. The answer is right there in your code.

Code:
$text = @$_GET['text'];


Do the same for the other variables. So, for instance, if you wanted a form to send a different text size to the script, change appropriate line to:

Code:
$fontsize = @$_GET['fontsize'];


And add a 'fontsize' field to your form.
I did make this script and I have tried the get 'fontsize' function before, but how do I get both the text and fontsize to work at the same time as the 'text' function.

How do I use it at the same time. I thought it could be like this but it didn't work:
Code:
http://www.dougie.frih.net/PHP/Uploaded%20Font%202.php?text=Mickey?fontsize=25

But clearly that does not work and ninjakannon you need to upload the .ttf to your site as it says in the error.
ninjakannon
The-Master, I've got somewhere with this now. I've made my own script.

Here's an example of it, change the variables in its url to change the image displayed

Here's the code for it:
Code:

<?php

// Recieve vars and up them into variables
$t = $_GET['text']; // Text string
$h = $_GET['height']; // Height of background
$w = $_GET['width']; // Width of background
$x = $_GET['x']; // X position of text
$y = $_GET['y']; // Y position of text

// Create an image at the size defined in the vars
$im = @imagecreate($w, $h);

// Set background colour to green
$bc = imagecolorallocate($im, 0, 255, 0);

// Set text colour to red
$tc = imagecolorallocate($im, 255, 0, 0);

// Write the text string into an image
//(resource image, font, x, y, string, text colour)
imagestring($im, 5, $x, $y,  $t, $tc);

// Output Image
header("Content-type: image/png");
imagepng($im);

imagedestroy($im);

?>


In this version I haven't changed the text size or set a custom font. This is simply because I don't know how to. Confused

The font is set as number 5, as you can see; you can change that number to switch the font - but only built in fonts can be used (there are 5 of these I think).

If you - or anyone else - finds out how to change the font type and size please tell, we'd be very happy to know. Very Happy

I hope this helps The-Master. Wink
The-Master
ninjakannon wrote:
The-Master, I've got somewhere with this now. I've made my own script.

I hope this helps The-Master. Wink
Yeah it does thanks very much! I now see how it works with the & in the url. Thanks very much!
ninjakannon
No problem. Very Happy

I don't quite understand how you've set the font and changed its size in your code. Would you mind giving me a quick explanation? Thanks. Wink
The-Master
OK. With the size I made it so that If the fontsize appears in the url then it uses that and if not then the size is 18
Code:

$fontsize = 18;
if(@$_GET['fontsize']) {
   $fontsize = $_GET['fontsize'];

Then I referenced here:
Code:
$size = imagettfbbox($fontsize, 0, $font, $text);

And for the font I uploaded the font to the directory that the script is in and just imput its name here
Code:
$font = 'Halo.ttf';
and put it in above.

Hope that is easy enough to understand. I might edit tomorrow to make it more understandable as I am going to sleep now.
ninjakannon
I've made one that can change the font and font size now.
I've learned quite a bit from this, so thanks! Wink

Here's the file, you can use two fonts: "arial" and "comic".

And here's the code:
Code:

<?php

// Recieve vars and up them into variables
// If they don't exist: use defaults
// Text string
if(@$_GET['text']) {
   $t = $_GET['text'];
} else {
   $t = 'undefined text to display';
}
// Height of background
if(@$_GET['height']) {
   $h = $_GET['height'];
} else {
   $h = '100';
}
// Width of background
if(@$_GET['width']) {
   $w = $_GET['width'];
} else {
   $w = '200';
}
// X position of text
if(@$_GET['x']) {
   $x = $_GET['x'];
} else {
   $x = $w/2;
}
// Y position of text
if(@$_GET['y']) {
   $y = $_GET['y'];
} else {
   $y = $h/2;
}
// Size of the font
if(@$_GET['fontsize']) {
   $fs = $_GET['fontsize'];
} else {
   $sf = '10';
}
// The font to be used
if(@$_GET['font']) {
   $f = $_GET['font'];
} else {
   $f = 'arial.ttf';
}

// Create an image at the size defined in the vars
$im = @imagecreate($w, $h);

// Set background colour to green
$bc = imagecolorallocate($im, 0, 255, 0);

// Set text colour to red
$tc = imagecolorallocate($im, 255, 0, 0);

// Write the text string into an image
imagettftext($im, $fs, 0, $x, $y, $tc, $f, $t);

// Output Image
header("Content-type: image/png");
imagepng($im);

imagedestroy($im);

?>

I could also make the background colour and font colour variables, but for now I don't think there's any need; we both know how that would be done.
Related topics
PHP Image suddenly doesn't work?
php image exif functions
PHP image
PHP image uploads with progress bar AND resizing? [Resolved]
Is this allowed?
PHP 5.x print functions ? [ printer ]
PHP Image Questions
Text - Image
Select a url for an image in image functions
Opening any image with PHP. Couldn't find anywhere.
Pls help, onmouseover popup image frm database
php extensions config
Limiting php file functions with .htaccess
xMPACT! PHP Image Viewer 1.0
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.