FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » simple image in PHP
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: simple image in PHP [message #175543 is a reply to message #175541] Thu, 06 October 2011 01:29 Go to previous message
Michael Joel is currently offline  Michael Joel
Messages: 42
Registered: October 2011
Karma:
Member
On Wed, 5 Oct 2011 15:54:03 -0700 (PDT), bob <bob(at)coolgroups(dot)com>
wrote:

> I want to create a simple image in PHP and save it to the current
> directory. Anyone know how to do this?

This can be simple or complicated depending on what you want to do.
There are many image functions that let you do almost anything. You
can draw the image from scratch if you want.

Here is a simple example using a pre made jpeg pic and adding text
over it. Then saving it as jpg:

// HERE WE CREATE A "CANVAS" USING THE PREMADE IMG AS A BACKGROUND
$im=imagecreatefromjpeg("graphics/my_jpg_background.jpeg");

// NOW CREATE A PALLET OF WHITE AND BLACK
$wht=ImageColorAllocate($im, 255,255,255);
$blk=ImageColorAllocate($im, 0,0,0);

// PLACE SOME TEXT ON THE CANVAS, OVER THE BG IMG
// TTF stands for True Type Font - use must use a .TTF font with this
function.
//
// Explanation of the ImageTTFText:
// ImageTTFText( prm1, prm2, prm3, prm4, prm5, prm6, prm7, prm8);
// prm1 = img resource handle (the handle we created above with
imagecreatefromjpeg.
// prm2 = size of text (in pixels)
// prm3 = angle of text (0 would be left to right)
// prm4 = left start position of text (pixels)
// prm5 = top position of text (pixels)
// prm6 = text color (from our pallet)
// prm7 = name of font file to use (this must have been uploaded to
the server... be sure to use correct spelling/caps - ex. "Times.TTF"
// prm8 = the text to draw onto the canvas.

ImageTTFText($im, 22, 0, 75, 25, $blk, "Times.ttf", "Some text");

// Here I a, drawing to my canvas the words "Some Text". It will be 22
pixels tall (cap letters). 0 degrees angle. start at 75 pixels from
canvas' left edge. 25 pixels down from canvas' top edge. The text will
be black.



// Here we will output
// HEADER will designate the output as a jpeg
Header("Content-type: image/jpeg");


// Next img is "generated"
// note that you don't have to actually SAVE an img to use it.
// if we simply link the HTML img src= to the PHP code that creates an
img. The code will act as an img though it is created "on the fly".

// By providing a file name we can save the img to a file.
// The last parameter is the quality of the image (compression). 100 =
best, 0=worst (small file)
ImageJpeg($im, "myJpeg.jpg", 100);

// Here the resource handle is released back so memory is reclaimed.
ImageDestroy($im);



There is also png and other img type functions.
http://www.php.net/docs.php
You can download a help file for PHP.

I have no doubt that if I have missed something, wrong about
something, made a typo, or presented something wrong - someone will
point it out :)

Mike
[Message index]
 
Read Message
Read Message
Previous Topic: PDFLib
Next Topic: comparing dates?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 09 10:39:53 GMT 2024

Total time taken to generate the page: 0.04739 seconds