Welcome Guest ( Log In | Register )



2 Pages V   1 2 >  
Reply to this topicStart new topic
> Basic Tutorial: PHP GD, basic tuts
r3d
post Oct 24 2004, 12:52 PM
Post #1


death
Group Icon

Group: Members
Posts: 268
Joined: 8-September 04
Member No.: 384



With php gd is the image function library of php. The functions include in this library enable php users to creating image up to manipulating photos. PHP gd can create with file extension of jpg, gif, swf, tiff and jpeg2000.

Installation see http://www.php.net/manual/en/ref.image.php. Astahost hosting service has enable gd library, so won’t need any extra works except in coding.

Lets get started, creating images from php
set the canvas:
CODE

  $img = imagecreate(250, 80)

imagecreatethis function create and set a blank canvas with the wide of 250 pixels and height of 80 pixels.

setting the basic colors:
CODE

  $black = imagecolorallocate($img, 0, 0, 0)
  $white = imagecolorallocate($img, 255, 255, 255)
  $red = imagecolorallocate($img, 255, 0, 0)
  $green = imagecolorallocate($img, 0, 0, 255)

imagecolorallocate $img represent the canvas. the next three value is color value in rgb(0-255), you also set the color in hex (0x00 – 0xff).

drawing a something:
CODE

  imagerectangle($img, 10, 10, 240, 70, $white)

imagerectangle this function create a rectangle a width of 230[240(x2) – 10(x1)] and a height of 60[70(y2) – 10(y1)]. $img the canvas, the first two value sets the starting point(x1, y1) . and last two number is the ending(x2, y2). And the last value is the color.
CODE

  imagefilledrectangle($img, 20, 20, 60, 60, $red)

imagefilledrectangle this function create a filled rectangle with a color red. All values is the same as the imagerectangle function.
CODE

  imagefilledellipse($img, 90, 40, 40, 40, $blue)

imagefilledellipse this one creates an ellipse(circular objects). $img is the canvas, the first two value(cx, cy) set the origin(center) of the ellipse. The third value set the width(horizontal width) and fifth one is the height(vertical width). Last but not the least the color value.
CODE

  $corners = array(
  0 => 190,
  1 => 60,
  2 => 210,
  3 => 20,
  4 => 230,
  5 => 60,
  );
  imagefilledpolygon($img, $corners, 3, $white);

imagefilledellipse and the last drawing function I discuss is a polygon function, in the example there’s only 3 corners. The first value is the canvas, the second one is the corners in array, the value 3 is the number of vertices, and the last one is color.

Showing the graphics:
CODE

  header("Content-type: image/jpeg")
  imagejpeg($img)


header imagejpeg To output the image you must first send the appropriate header, in this example I use jpg extension and the content type is set to “image/jpeg”. And call the imagejpeg function to create the images and shown up to the browser. For other file type such as png(image/png), gif(image/gif), windows bitmap(image/vnd.wap.wbmp), check php manual for details.

And finally, use the imagedestroy function just to clear up the memory used by the imagecreate functions.

Final code should look like this.
CODE

<?php

$img = imagecreate(250,80);

$black = imagecolorallocate($img, 0, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
$red   = imagecolorallocate($img, 255, 0, 0);
$green = imagecolorallocate($img, 0, 255, 0);
$blue  = imagecolorallocate($img, 0, 0, 255);

$corners = array(
0 => 190,
1 => 60,
2 => 210,
3 => 20,
4 => 230,
5 => 60,
);

imagerectangle($img, 10, 10, 240, 70, $white);
imagefilledrectangle($img, 20, 20, 60, 60, $red);
imagefilledellipse($img, 90, 40, 40, 40, $blue);
imagefilledellipse($img, 150, 40, 70, 40, $green);
imagefilledpolygon($img, $corners, 3, $white);

header ("Content-type: image/jpeg");
imagejpeg($img);
imagedestroy($img);
?>

enjoy cool.gif
Go to the top of the page
 
+Quote Post
squirel
post Oct 24 2004, 02:43 PM
Post #2


Member [ Level 1 ]
Group Icon

Group: [HOSTED]
Posts: 41
Joined: 12-October 04
Member No.: 1,150



Very nice tutorial, well written and very descriptive. I had no clue you could do this with php. I guess you learn something new every day.

Time to go play with this stuff.
Go to the top of the page
 
+Quote Post
marijnnn
post Oct 24 2004, 04:04 PM
Post #3


Premium Member
Group Icon

Group: [HOSTED]
Posts: 336
Joined: 22-September 04
Member No.: 798



well, to give you an idea what you could use this for: thumbnails
i'm a lazy kid so i upload my pictures with ftp
then some guy/girl visits my site and my picture page
he opens the directory with the new pictures
the script checks all the pictures and checks if they all have a thumbnail
(if a filed is called blabla.jpg, the thumb is called .blabla.jpg, so it's invisible too)
if there isn't, i make one with gd!!! it's great. i never have to make pages anymore, never make thumbs,...

gd rocks quite a lot!
Go to the top of the page
 
+Quote Post
marijnnn
post Oct 24 2004, 04:28 PM
Post #4


Premium Member
Group Icon

Group: [HOSTED]
Posts: 336
Joined: 22-September 04
Member No.: 798



hm, apearently it's not working atm. it worked before, but today it's all fucked up.
php says it doesn't know the function imagecreatefromjpeg... help!
Go to the top of the page
 
+Quote Post
neno.tu
post Oct 24 2004, 04:43 PM
Post #5


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 24
Joined: 22-October 04
From: Caracas, Venezuela
Member No.: 1,241



Maybe the GD library is not enabled...

Wait for tomorrow and see what happends...

By the way... I made a picture gallery with PHP+GD...

If anyone would like to I can make a tutorial to create thumbnails and stuff...

Also an advice:

Don't use:

CODE
imagecreate($width,$height)


That could make your pictures look preaty bad...

Use:

CODE
imagecreatetruecolor($width,$height)


That makes it look exactly like is and you won't lose the image quality...

If you can't use the GD library you could use ImageMagic too.. is a library that can do the same and more...

Hope it helps...
Go to the top of the page
 
+Quote Post
marijnnn
post Oct 24 2004, 05:15 PM
Post #6


Premium Member
Group Icon

Group: [HOSTED]
Posts: 336
Joined: 22-September 04
Member No.: 798



yeah, but all the extensions are disabled atm
gd, exif,...
i am using truecolor, don't worry wink.gif
it works fine on my own computer, but it's not working with astahost. but i've heard/read that they are having problems, so i'll just wait till those are solved...
Go to the top of the page
 
+Quote Post
r3d
post Oct 25 2004, 04:43 AM
Post #7


death
Group Icon

Group: Members
Posts: 268
Joined: 8-September 04
Member No.: 384



outch huh.gif
astahost hosting has no gd support for the moment, may be... this is part of the problem . and the tutorial won't work at this time. plz be patient while the admin is fixing this smile.gif
Go to the top of the page
 
+Quote Post
avalon
post Oct 28 2004, 07:17 PM
Post #8


Advanced Member
Group Icon

Group: Members
Posts: 160
Joined: 27-October 04
Member No.: 1,260



QUOTE(r3d @ Oct 24 2004, 08:52 PM)
Final code should look like this.
CODE

<?php

$img = imagecreate(250,80);

$black = imagecolorallocate($img, 0, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
$red   = imagecolorallocate($img, 255, 0, 0);
$green = imagecolorallocate($img, 0, 255, 0);
$blue  = imagecolorallocate($img, 0, 0, 255);

$corners = array(
0 => 190,
1 => 60,
2 => 210,
3 => 20,
4 => 230,
5 => 60,
);

imagerectangle($img, 10, 10, 240, 70, $white);
imagefilledrectangle($img, 20, 20, 60, 60, $red);
imagefilledellipse($img, 90, 40, 40, 40, $blue);
imagefilledellipse($img, 150, 40, 70, 40, $green);
imagefilledpolygon($img, $corners, 3, $white);

header ("Content-type: image/jpeg");
imagejpeg($img);
imagedestroy($img);
?>

enjoy  cool.gif
*



Is there a way to save it somewhere on the server as a jpg, gif or png with a filename before imagedestroy($img)?

Go to the top of the page
 
+Quote Post
avalon
post Oct 28 2004, 07:20 PM
Post #9


Advanced Member
Group Icon

Group: Members
Posts: 160
Joined: 27-October 04
Member No.: 1,260



QUOTE(r3d @ Oct 25 2004, 12:43 PM)
outch  huh.gif
astahost hosting has no gd support for the moment, may be... this is part of the problem . and the tutorial won't work at this time. plz be patient while the admin is fixing this smile.gif
*



Can we request for it to be installed.
gd and magick is fabulous for auto image generation.
Freetype is oftenly required too, you will be happy to find out what it can do.
Go to the top of the page
 
+Quote Post
r3d
post Oct 28 2004, 07:33 PM
Post #10


death
Group Icon

Group: Members
Posts: 268
Joined: 8-September 04
Member No.: 384



please don't double post, use the edit button smile.gif

about the gd it's allready installed. and about your first question no, it must be save as php or it will not be parse, just a suggestion u can do something like this image.php?id=image_name with the gd wink.gif
Go to the top of the page
 
+Quote Post

2 Pages V   1 2 >
Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Basic Tips and Tricks in HTML(15)
  2. Basic css code(2)
  3. Basic C++ Language(19)
  4. Visual Basic Help(7)
  5. Visual Basic: Replace Explained!(4)
  6. Visual Basic: Unload Your Application Correctly!(1)
  7. Visual Basic: Random Strings!(10)
  8. Making A Nice Looking Signature In Photoshop Cs(17)
  9. Visual Basic 6 + Crystal Reports 9(6)
  10. Performing Dos Operations From Visual Basic(1)
  11. Finding The Current Line Number In A Text Box(1)
  12. Visual Basic.NET Help Needed.(8)
  13. Hamachi - Your Next Best Friend(2)
  14. C++: Basic Classes(5)
  15. Asterisknow Pbx (voip Telephony)(1)
  1. Phpbb - Installation Tutorial ( For Newbies Based On Astahost Cpane)l(5)
  2. Installed Internet Explorer 7?, Visual Basic Now Broken?(3)
  3. Html Basic Tutorial(9)
  4. Basic Forensics: Winhex(1)
  5. Lesson1 :introduction To Visual Basic(2)
  6. Some Usefull Linux Basic Commands And Utilities. Please Add To This List If You Know One.(0)
  7. Linux Basic Command - For Storing Compilation Error To File(1)
  8. Basic Css(6)
  9. An Absolute Basic Guide To Algorithms For Dummies(0)
  10. Basic Html Tutorial(1)
  11. Linux Partitioning Guide (new Users)(1)
  12. Basic C Language, Functions(5)
  13. Basic C++ Coding(9)


 



- Lo-Fi Version Time is now: 23rd November 2008 - 03:36 PM