29-05-2004, 18:28
|
#1
|
|
Inactive
Join Date: Jun 2003
Location: I am house...
Services: $KY+HD - BT Infinity
Posts: 2,284
|
Image manipulation
Found this link on the web: http://www.ryano.net/iraq/
It allows you to enter some text that when submitted becomes part of the image (if you right click and save - the text is within the image). Anyone know how they do it or can point me in the direction of site that shows me how. I would like to create a similar function for one of my websites.
Thanks in advance...
|
|
|
29-05-2004, 19:50
|
#2
|
|
Inactive
Join Date: Jun 2003
Age: 45
Posts: 14,750
|
Re: Image manipulation
I've already written something similiar. I was writing it so I can have a random generated sig on this board. It basically works, but I still want to tweak the text position/alignment but anyway, here is the important code:
Code:
<?php
//define what pic you want to alter and text
$thepic = 'img.jpg';
$thestring= "text\ntext"; // \n for new line
//load the image (also you can use imagecreatefromgif)
$im = imagecreatefromjpeg($thepic);
//blue text
$textcolor = imagecolorallocate($im, 0, 0, 255);
//write text - the number params are: fontsize, angle to write, x position, y position
imagettftext($im, 12, 0, 0, 0, $textcolor,'arial.ttf',$thestring);
//output image to browser.
header("Content-type: image/jpg");
imagejpeg($im);
?>
Note: you can't output GIFs due to Compuserve copyright law. If you want to output transparency, use content-type: image/png, and imagepng. Also, the font has to be stored locally on the server. So just copy arial.ttf from c:\windows\fonts and upload it to your server.
To call it, include this in your HTML file:
Code:
<img src="imagemanipulated.php"></img>
If you want more info go to: http://uk.php.net/manual/en/function.imagettftext.php
HTH
|
|
|
29-05-2004, 21:14
|
#3
|
|
Inactive
Join Date: Jun 2003
Location: I am house...
Services: $KY+HD - BT Infinity
Posts: 2,284
|
Re: Image manipulation
Thanks for your help it would appear that I-WEB don't support TTF ...
"Call to undefined function: imagettftext()"
O well back to the drawing board!!!
|
|
|
29-05-2004, 21:27
|
#4
|
|
Inactive
Join Date: Jun 2003
Age: 45
Posts: 14,750
|
Re: Image manipulation
You need the GD and FreeType library. I'd ask them to add it, although they should have by default. Give KingPhoenix a nudge.
If he won't, I might have to start renting out my PHP server  On the brightside it might be 400k upload soon
|
|
|
29-05-2004, 22:04
|
#5
|
|
Inactive
Join Date: Jul 2003
Location: Eglinton, Co. Derry
Posts: 7,640
|
Re: Image manipulation
I-web has the latest GD library, I use it regularly, not sure about Freetype though.
|
|
|
29-05-2004, 22:15
|
#6
|
|
Inactive
Join Date: Jun 2003
Age: 45
Posts: 14,750
|
Re: Image manipulation
If it is running, it should say, under the GD section of phpinfo():
GD Support enabled
FreeType Support enabled
|
|
|
30-05-2004, 14:10
|
#7
|
|
Inactive
Join Date: Jun 2003
Location: I am house...
Services: $KY+HD - BT Infinity
Posts: 2,284
|
Re: Image manipulation
Now running on both I-WEB servers - Top marks to KP.
|
|
|
30-05-2004, 14:37
|
#8
|
|
Inactive
Join Date: Jul 2003
Location: Eglinton, Co. Derry
Posts: 7,640
|
Re: Image manipulation
Punky, that script works well, though, I've managed to **** it up already, I've got a form which you slap the text in and it uses GET to send the URL string e.g. www.blah.com/content=whatevertextstring then the php file I've altered to use:
$thestring = $_GET["content"];
It just seem's to put odd characters up, must be something to do with it not reading it as a text field or something.
(also, is there any way of centering the text horizontally on the image?)
|
|
|
30-05-2004, 15:56
|
#9
|
|
Inactive
Join Date: Jun 2003
Location: Ra Port
Age: 73
Services: VM 200mb
VM Phone M
Posts: 566
|
Re: Image manipulation
Quote:
|
Originally Posted by Tricky
Now running on both I-WEB servers - Top marks to KP.
|
They always where enabled on servers just need to check that they are latest version
|
|
|
30-05-2004, 15:58
|
#10
|
|
Inactive
Join Date: Jun 2003
Age: 45
Posts: 14,750
|
Re: Image manipulation
I don't know what you are doing wrong as it works here. If you get funny symbols in there, like squares, you may be using symbols that aren't in the font set. Make sure, for the moment, you only use alphanumeric characters.
Without looking at the code, it is hard to see what you are doing, but here is what you should have, if you want to set the text via a form:
Code:
<?php
if (isset($_GET["content"]))
{
Then your image munipulation code as before, then:
Code:
}
else
{
?>
<form action = "<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="content">
<input type="submit">
</form> <?php
}
?>
Run the file say file.php, and as there is nothing to get, it will show you the form. Once you click on it, it will reload itself, and as $_GET is set again, so it will display the image. I can't see whats wrong with your code, but doing that works for me. PM it over andi'll take a look if you still can't get it working.
For centring the text: This isn't the most concise way, but it is the way that makes it easiest to understand. Replace the imagettftext line with this:
Code:
//center text
//work out how big the text is
$bounds = imagettfbbox(12,0,'arial.ttf',$thestring);
//get width - right x coord - left x coord
$textwidth = $bounds[2] - $bounds[0];
//get offset
$textoffset = $textwidth / 2;
//calc start pos - half the image size minus offset - middle of image should be middle of text.
$textstartpos = (imagesx($im)/2) - $textoffset;
//write text - note 0 has become $textstartpos
imagettftext($im, 12, 0, $textstartpos, 20, $textcolor,'arial.ttf',$thestring);
HTH mate.
Just as a helpful reference, here is the whole of my code:
Code:
<?php
if (isset($_GET["content"]))
{
//define what pic you want to alter and text
$thepic = 'img.jpg';
$thestring= $_GET["content"];
//load the image (also you can use imagecreatefromgif)
$im = imagecreatefromjpeg($thepic);
//blue text
$textcolor = imagecolorallocate($im, 0, 0, 255);
$bg = imagecolorallocate($im, 255, 255, 255);
imagecolortransparent($im, $bg);
//center text
$bounds = imagettfbbox(12,0,'arial.ttf',$thestring);
//get width
$textwidth = $bounds[2] - $bounds[0];
//get offset
$textoffset = $textwidth / 2;
//calc start pos
$textstartpos = (imagesx($im)/2) - $textoffset;
//write text
imagettftext($im, 12, 0, $textstartpos, 20, $textcolor,'arial.ttf',$thestring);
//output image to browser.
header("Content-type: image/png");
imagepng($im);
}
else
{
?>
<form action = "<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="content">
<input type="submit">
</form> <?php
}
?>
Edit: If you want to call it from a separate file, use this code instead to call the image munipulation:
File: callimagemunip.php
Code:
<html>
<body>
<img src="imagemunip.php<?php echo "?content=". $_GET["content"]; ?>"></img>
<form action="callimagemunip.php">
<input type="text" name="content">
<input type="submit">
</form>
</body>
</html>
|
|
|
30-05-2004, 16:23
|
#11
|
|
Inactive
Join Date: Jun 2003
Location: Los Angeles, CA
Age: 47
Posts: 6,343
|
Re: Image manipulation
I'd use a slightly different version:
PHP Code:
<?php
if (!empty($_GET["content"]))
{
//define what pic you want to alter and text
$thepic = 'img.jpg';
$thestring= $_GET["content"];
//load the image (also you can use imagecreatefromgif)
$im = imagecreatefromjpeg($thepic);
//blue text
$textcolor = imagecolorallocate($im, 0, 0, 255);
$bg = imagecolorallocate($im, 255, 255, 255);
imagecolortransparent($im, $bg);
//center text
$bounds = imagettfbbox(12,0,'arial.ttf',$thestring);
//get width
$textwidth = $bounds[2] - $bounds[0];
//get offset
$textoffset = $textwidth / 2;
//calc start pos
$textstartpos = (imagesx($im)/2) - $textoffset;
//write text
imagettftext($im, 12, 0, $textstartpos, 20, $textcolor,'arial.ttf',$thestring);
//output image to browser.
header("Content-type: image/png");
imagepng($im);
}
else
{
echo "<form action=\"$_SERVER['PHP_SELF']\"><input type=\"text\" name=\"content\"><input type=\"submit\"></form>";
}
?>
The main changes being the !empty part and the bottom form HTML.
It says something like "if content is not empty then do this, otherwise echo the form out".
|
|
|
30-05-2004, 16:25
|
#12
|
|
Inactive
Join Date: Jul 2003
Location: Eglinton, Co. Derry
Posts: 7,640
|
Re: Image manipulation
Ah now that works ok, all I have to do now is stop it antialiasing so I can use 9 point verdana.
http://www.se-an.co.uk/test.php
I'm sure there's a setantialias command, but I can't find any reference to it from google.
|
|
|
30-05-2004, 16:54
|
#14
|
|
Inactive
Join Date: Jul 2003
Location: Eglinton, Co. Derry
Posts: 7,640
|
Re: Image manipulation
Quote:
|
Originally Posted by punky
|
Heh, I was was trying (no) and (0) and getting nowhere.
If you stick a "-" in from the the $textcolor it seems to take some of the antialiasing off.
|
|
|
16-06-2004, 01:01
|
#15
|
|
Inactive
Join Date: Jun 2004
Posts: 1
|
Re: Image manipulation
phpinfo indicates:
GD Support enabled
FreeType Support enabled
yet line 24 (using Punky's whole of code post)
$bounds = imagettfbbox(12,0,'arial.ttf',$thestring);
errors with
Warning: Could not find/open font in /home/my_account/public_html/pix/pix.php on line 24
and arial.ttf is in the same folder as pix.php
Any ideas why the error as I have tried it as
$bounds = imagettfbbox(12,0,'arial.ttf',$thestring);
and
$bounds = imagettfbbox(12,0,'/home/my_account/public_html/pix/arial.ttf',$thestring);
wuth the same error?!
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 13:58.
|