
|
View Full Version : [PHP/GD] Little help please...
JustinSmall 08-22-2008, 11:35 AM I don't want you to (re)write the whole script for me. :P
Just need a little help.
So it's displaying text on a newly generated image... fun fun.
The problem comes in when I try to make it save it :(
Here's what I got...
<?
$image = imagecreate(200, 40);
$background = imagecolorallocate($image, 27, 46, 67);
$color = imagecolorallocate($image, 255, 255, 255);
imagestring($image, 3, 28, 12, "Throwing down some GD", $color);
Header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
?>
The above code works, the problem comes into play when I do this code.
<?
$image = imagecreate(200, 40);
$background = imagecolorallocate($image, 27, 46, 67);
$color = imagecolorallocate($image, 255, 255, 255);
imagestring($image, 3, 28, 12, "Throwing down some GD", $color);
Header("Content-type: image/png");
imagepng($image, "new.png");
imagedestroy($image);
?>
The image will come into play saying http://icandydesigns.net/imagetest.php
which is also the link for the image test lol, it does save the image with the text 'Throwing down some GD' on the server... so its not that part thats messing up,its the display part.
:P I can't figure it out...
and question two, how do I make the text cursive, I've been digging for days but yet to find it.
etogre 08-22-2008, 12:54 PM Not sure what the problem is... the image is displaying on imagetest.php and new.png exists on your server.
To make the font cursive you are going to have to load a .ttf font file stored on your server.
JustinSmall 08-22-2008, 02:50 PM The image is displaying because I have it set back to:
<?
$image = imagecreate(200, 40);
$background = imagecolorallocate($image, 27, 46, 67);
$color = imagecolorallocate($image, 255, 255, 255);
imagestring($image, 3, 28, 12, "Throwing down some GD", $color);
Header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
?>
which was teh working code... :) the reason it was on the server is bc I didn't delete it ;) now I deleted the image, it's not back :P
Ok, how do I include the TFF file + run it? and will it matter if the person using it doesn't have the font file?
===========EDIT==================
This will eventually be turned into a class :) just so ya'll know :P
kencox 08-22-2008, 02:58 PM Im sure you have done your reading but this might help. http://us3.php.net/imagepng
JustinSmall 08-22-2008, 03:17 PM I dug around there, the one thing I thought could apply to me is…
imagepng($image,"new.png",0,NULL);
but it didn't work :(
when I have it as…
imagepng($image,"new.png",0,NULL);
… without the header, it display blank
when I have it as…
imagepng($image,"new.png");
… it just displays the url and I right click... it doesn't show a type for the image :(
very very odd
JustinSmall 08-22-2008, 03:56 PM Alright, we'll I've been digging around google and I saw 100's of different but the same ways of doing this…
one I found very interesting, but I can't remember the exact url.
But it said 'show or save' and I was thinking… "Theres a difference?"
So that's when I thought… "Why am I displaying and saving at the same time, when I really don't need to display and save at the same time."
So I thought of the easiest thing in the world to do…
Just echo the save file and remove the header, because the header is whats forcing it to display something (even if what it's displaying isn't what you want displayed)
So I came up with this:
TO DISPLAY:
<?
$image = @imagecreate(200, 40) or die("Could not intitiate new GD image!");
$background = imagecolorallocate($image, 27, 46, 67);
$color = imagecolorallocate($image, 255, 255, 255);
imagestring($image, 3, 28, 12, "Throwing down some GD", $color);
Header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
?>
TO SAVE:
<?
$save = 'new-image.png';
$image = @imagecreate(200, 40) or die("Could not intitiate new GD image!");
$background = imagecolorallocate($image, 27, 46, 67);
$color = imagecolorallocate($image, 255, 255, 255);
imagestring($image, 3, 28, 12, "Throwing down some GD", $color);
imagepng($image, $save);
imagedestroy($image);
?>
TO SAVE AND DISPLAY:
<?
$save = 'new-image.png';
$image = @imagecreate(200, 40) or die("Could not intitiate new GD image!");
$background = imagecolorallocate($image, 27, 46, 67);
$color = imagecolorallocate($image, 255, 255, 255);
imagestring($image, 3, 28, 12, "Throwing down some GD", $color);
Header("Content-type: image/png");
imagepng($image, $save);
imagedestroy($image);
echo "<img src=' " . $save . "'></a>";
?>
— EDIT —
Now of course you would have to modify this to suit your needs :) but it's all fine and dandy for easy stuff...
As I stated before, I will build a class for this — it will probably be tomorrow — and post it here.
If anyone beats me to the punch, that's ok with me :) Only reason I'm going to post it here is to help others!
JustinSmall 08-22-2008, 11:49 PM To keep my word, here I am, I'm posting a little class that I made.
Now this is not how I'm going to be using the class myself, mine is a little more difficult for the reason that I need it.
But let's break it down a little bit more…
Common questions when talking about PHP GD (The use of generating dynamic images in PHP)
How hard is GD?
— I wouldn't recommend it to beginners… you will want to do stuff that you will have to ask a million questions how and not end up learning anything!
— I would personally recommend it to an intermediate level person (or higher) as they will know some of the common references that would be used in GD.
What is some practical use in GD?
— One of the most common uses that I know of is the use of Watermarks… once you really know GD and understand how it truelly works, then you can do a nifty little thing called Watermarking. Watermarking is an image is transparent over an image (alpha'd) to where the user can see through the image, but is noticeable enough to keep someone from ripping it. These are commonly used on imaging sites, such as the bottom right of the image would say 'MyImageSite.com'...
— Another key reason to use GD is to stop people from Spamming/Bot'ing your site… most people are probably sitting there thinking, "What the hell is this dude talking about?" Basically what it does is take a default image, then adds text to it like we did, but uses a pre-defined image! This way when bots or spammers try to sign-up, post comments, or whatever they are trying to do… they will have to pass a verification image based on your GD! They won't be able to successfully do this as it is hard coded into your php (unless you use $_GET… which is not recommend) and you will be that much more secure.
Why should I learn GD?
— You should not only learn GD to better your PHP skills, but to be able to do such a more variety of things. Not only can you make fun little gizmos with it, but you can also enhance security as I had stated above.
Where can I learn about this GD?
— You will learn a little from what I had posted, but you probably won't understand it. If you aren't new to the block on PHP then I would recommend the PHP-GD manual located here: http://us2.php.net/gd … but you also can use google to find vast amounts of tutorials.
-----------------------------------
Now here we go, I'm going to post the class.
Again I will state, this class isn't what I'm completely going to use it for and might contain some little details that people will want to nit pick at... but it works.
To start off with, lets make our class..
gd.php
<?
class CreateGD {
protected $name;
protected $width;
protected $height;
protected $text;
protected $b1;
protected $b2;
protected $b3;
protected $c1;
protected $c2;
protected $c3;
protected $font;
protected $top;
protected $left;
public function __construct($width, $height, $text, $b1, $b2, $b3, $c1, $c2, $c3, $font, $top, $left){
$this->image = @imagecreate($width, $height) or die("Could not intitiate new GD image!");
$background = imagecolorallocate($this->image, $b1, $b2, $b3);
$color = imagecolorallocate($this->image, $c1, $c2, $c3);
imagestring($this->image, $font, $top, $left, $text, $color);
}
public function Save($name){
imagepng($this->image, "". $name .".png");
}
public function SaveDisplay($name){
imagepng($this->image, "". $name .".png");
echo "<img src=' ". $name .".png'></a>";
}
public function Display(){
Header("Content-type: image/png");
imagepng($this->image);
}
public function DestroyImage(){
imagedestroy($this->image);
}
}
?>
I var'd the vars to be pretty self-explanatory…
Now for the display pager…
display.php
<?
require('gd.php');
$create_image = new CreateGD('160', '40', 'Designers Blog', '27', '46', '67', '255', '255', '255', '3', '28', '12');
$create_image->SaveDisplay('new-gd');
$create_image->DestroyImage();
?>
You will notice the long strand in CreateGD... this controls the width, height, message, colors, and ect…
These will allow you to completely control your GD imaging (when it comes to creating images, not editing existing)
Anyways, there are 4 usable functions…
Display — Displays Image (doesn't save)
Save — Saves Image (doesn't display)
SaveDisplay — Saves and Display Image
DestroyImage — Destroys the Image after completing the desired effect
How to use these functions
$create_image->Display();
$create_image->Save(SAVE-NAME-HERE);
$create_image->Display(SAVE-NAME-HERE);
$create_image->DestroyImage();
You will want to use $create_image->DestroyImage(); after your function always!!!
Do not use more than one of the first three functions… this will cause an error (untested, no need to test)…
Simple and sweet, the way I like it...
|