Cable Forum

Cable Forum (https://www.cableforum.uk/board/index.php)
-   Internet Discussion (https://www.cableforum.uk/board/forumdisplay.php?f=25)
-   -   Image manipulation (https://www.cableforum.uk/board/showthread.php?t=13001)

Tricky 29-05-2004 18:28

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...

punky 29-05-2004 19:50

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

Tricky 29-05-2004 21:14

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!!!

punky 29-05-2004 21:27

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 :)

Bifta 29-05-2004 22:04

Re: Image manipulation
 
I-web has the latest GD library, I use it regularly, not sure about Freetype though.

punky 29-05-2004 22:15

Re: Image manipulation
 
If it is running, it should say, under the GD section of phpinfo():

GD Support enabled
FreeType Support enabled

Tricky 30-05-2004 14:10

Re: Image manipulation
 
Now running on both I-WEB servers - Top marks to KP.

Bifta 30-05-2004 14:37

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?)

willie 30-05-2004 15:56

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

punky 30-05-2004 15:58

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>


Richard M 30-05-2004 16:23

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".

Bifta 30-05-2004 16:25

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.

punky 30-05-2004 16:44

Re: Image manipulation
 
You can turn it off using:

http://uk.php.net/manual/en/function.imageantialias.php

just use imageantialias($im,false);

Bifta 30-05-2004 16:54

Re: Image manipulation
 
Quote:

Originally Posted by punky
You can turn it off using:

http://uk.php.net/manual/en/function.imageantialias.php

just use imageantialias($im,false);

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.

justme 16-06-2004 01:01

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?!


All times are GMT. The time now is 12:39.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
All Posts and Content are © Cable Forum