ImageString()
Moderator: General Moderators
ImageString()
How to make an image string according to it's file name?
As example, I've an image file :
devnetwork.jpg
and I wanna make a "devnetwork" String on the bottom of the image
is it possible?
how about searching it's file directory?
thanks anyway
As example, I've an image file :
devnetwork.jpg
and I wanna make a "devnetwork" String on the bottom of the image
is it possible?
how about searching it's file directory?
thanks anyway
Re: ImageString()
So you have an image and you want to edit the image to add text?
Re: ImageString()
yuppy, exactly.yacahuma wrote:So you have an image and you want to edit the image to add text?
I wanna add text like it's file name ^^
Re: ImageString()
Take a look at this. I think this is what you need , you just need to modify the code. This is use for captcha images. But look at the code. It gets an image and the add text. You do have to modify it. for reading jpeg,is a different function(i think)
Very important , you need access to the font. If not you are not going to see anything.
Very important , you need access to the font. If not you are not going to see anything.
Code: Select all
class SecImgNumber {
var $string = '';
function display()
{
$referenceid = md5(mktime()*rand());
//Select Font
$font = "secimages/century.ttf";
//Select random background image
$bgurl = 2;//rand(1, 3);
$im = ImageCreateFromPNG("secimages/bg".$bgurl.".png");
//Generate the random string
//$chars = array("a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H","i","I","j","J","k",
//"K","l","L","m","M","n","N","o","O","p","P","q","Q","r","R","s","S","t","T","u","U","v",
//"V","w","W","x","X","y","Y","z","Z","1","2","3","4","5","6","7","8","9");
$chars = array("A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","W","X","Y","Z",
"1","2","3","4","5","6","7","8","9");
$length = 4;
$textstr = "";
for ($i=0; $i<$length; $i++) {
$textstr .= $chars[rand(0, count($chars)-1)];
}
//Create random size, angle, and dark color
$size = 16;//rand(12, 16);
$angle = rand(-5, 5);
$color = ImageColorAllocate($im, rand(0, 100), rand(0, 100), rand(0, 100));
//Determine text size, and use dimensions to generate x & y coordinates
$textsize = imagettfbbox($size, $angle, $font, $textstr);
$twidth = abs($textsize[2]-$textsize[0]);
$theight = abs($textsize[5]-$textsize[3]);
$x = (imagesx($im)/2)-($twidth/2)+(rand(-20, 20));
$y = (imagesy($im))-($theight/2);
//Add text to image
ImageTTFText($im, $size, $angle, $x, $y, $color, $font, $textstr);
$this->string= $textstr;
//Output PNG Image
header("Content-Type: image/png");
ImagePNG($im);
//Destroy the image to free memory
imagedestroy($im);
}
function getString() {
return $this->string;
}
}
Re: ImageString()
yeah i c
nice post ^^
btw, could anyone told me how to access it's file name?
stuck on thiz
nice post ^^
btw, could anyone told me how to access it's file name?
stuck on thiz
Re: ImageString()
I don't really understand what the problem is with the filename. Surely you have to know what the filename is in order to open the image to write on it? How are you planning to tell the script which file to open if you're not using the filename?
Re: ImageString()
Right I understand what you're saying, try this code but modify it obviously
I just slightly modified the code I use to create my CAPTCHA image. If you put this script in to a seperate PHP file on its own (lets say imagecreate.php), when you want to call the image you can simply type in to your web page <img src="imagecreate.png">, for example,
Basic HTML Page
This will display the image $filename with your input text string!
For the function imagestring(); here is a definition of what all the parameters mean:
imagestring ( resource, font, xposition, yposition, string, textcolor )
x and y positions are just where the code is written on the image you create, the above 5,5,8 is designed for a 60x30 captcha image so is relatively near the top.
Hope that helps you!
NOTE: In order to use this code your PHP extensions MUST include the GD2 library!
Code: Select all
$filename = 'THE FILE YOU WANT TO ADD STRING TO';
$image = imagecreatefromPNG($filename);
$textColor = imagecolorallocate ($image, 0, 0, 0);
imagestring ($image, 5, 5, 8, $filename, $textColor);
imagePNG($image);
imagedestroy($image);
Basic HTML Page
Code: Select all
<html>
<head>
<title>My Image</title>
</head>
<body>
<center><img src="imagecreate.php" width="60" height="30" /></center>
</body>
</html>For the function imagestring(); here is a definition of what all the parameters mean:
imagestring ( resource, font, xposition, yposition, string, textcolor )
x and y positions are just where the code is written on the image you create, the above 5,5,8 is designed for a 60x30 captcha image so is relatively near the top.
Hope that helps you!
NOTE: In order to use this code your PHP extensions MUST include the GD2 library!
Re: ImageString()
Also, yacahuma, here is the method I use to generate a captcha image instead, thought it might be useful to you. It compresses the amount of code and complication needed, the only thing is you need to call a seperate script in order to make it work properly like i described above.
Then to compare the user input captcha to the one your script made just MD5 it then compare to the session variable. Just seems a lot easier to type instead of all the "a" "A" "b" "B" etc
Code: Select all
$alphanum = "abcdefghijklmnopqrstuvwxyz0123456789";
$rand = substr(str_shuffle($alphanum), 0, 5);
$bgNum = rand(1, 5);
$image = imagecreatefromjpeg("cbg$bgNum.jpg");
$textColor = imagecolorallocate ($image, 0, 0, 0);
imagestring ($image, 5, 5, 8, $rand, $textColor);
$_SESSION['cap'] = md5($rand);
// Send several headers to ensure the image is not cached (taken directly from PHP manual)
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
// Send the content type header so the image is displayed properly, send image to browser then destroy to free memory
header('Content-type: image/jpeg');
imagejpeg($image);
imagedestroy($image);