Jump to content



Welcome to AstaHost - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!

Toggle shoutbox Shoutbox Open the Shoutbox in a popup

@  styleseye : (24 April 2014 - 02:29 PM) Https://www.facebook.com/l.php?u=Https://www.youtube.com/watch?v=Geh4Rs-Boh8&feature=Youtu.be&h=Taqexxpem
@  styleseye : (24 April 2014 - 02:28 PM) Https://www.facebook.com/l.php?u=Https://www.youtube.com/watch?v=Geh4Rs-Boh8&feature=Youtu.be&h=Taqexxpem
@  yordan : (21 April 2014 - 09:11 PM) Hey, Ritesh, Did You Hear Me?
@  yordan : (14 April 2014 - 05:28 PM) By The Way, This Could Be An Interesting Subject For A Topic, What About Posting This Question? Let's See If Other People Have The Same Feeling Concerning Bootlists!
@  yordan : (13 April 2014 - 09:36 AM) Boot Order : Cd, [Usb,] Hard Drive :D
@  yordan : (11 April 2014 - 07:23 PM) I Simply Let The Bios Do That
@  Ritesh : (11 April 2014 - 10:23 AM) Is It Possible To Launch Fedora Live Cd Or Installation Disk From Hard Drive On Windows Platform Using Grub Mbr File.
@  Ritesh : (11 April 2014 - 10:21 AM) No U Are Not.. Btw.. I Have Question For You.
@  yordan : (10 April 2014 - 08:02 AM) You Are Partially Right.
I Was Not.
Nevertheless, I Am Again :)
@  Ritesh : (09 April 2014 - 07:33 PM) :P
@  Ritesh : (09 April 2014 - 07:33 PM) I Think U R Not..
@  yordan : (09 April 2014 - 09:28 AM) I'm The Master Of The Shoutbox!
@  yordan : (05 April 2014 - 10:32 PM) He-He
@  Ritesh : (04 April 2014 - 06:59 PM) Ha Ha Ha ....
@  yordan : (04 April 2014 - 11:15 AM) Welcome Back, Starscream!
@  yordan : (03 April 2014 - 02:31 PM) And I Hope That He Will Come Back Soon :)
@  yordan : (01 April 2014 - 02:53 PM) Nice, Ritesh Came, I'm Not Home Alone Today.
@  Ritesh : (01 April 2014 - 08:51 AM) Oh!!! Poor Dear Yordan..
@  yordan : (31 March 2014 - 10:02 AM) I'm A Poor Lonesome Cow-Boy
@  yordan : (27 March 2014 - 02:22 PM) He Is Unpatient Due To His Patients!

Photo
- - - - -

Basic Tutorial: PHP GD basic tuts


17 replies to this topic

#1 r3d

r3d

    death

  • Members
  • 268 posts

Posted 24 October 2004 - 12:52 PM

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/m...n/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:
  $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:
  $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:
  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.
  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.
  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.
  $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:
  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.
<?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 :)

#2 squirel

squirel

    Member [ Level 1 ]

  • [HOSTED]
  • 41 posts

Posted 24 October 2004 - 02:43 PM

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.

#3 marijnnn

marijnnn

    Premium Member

  • [HOSTED]
  • 336 posts

Posted 24 October 2004 - 04:04 PM

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!

#4 marijnnn

marijnnn

    Premium Member

  • [HOSTED]
  • 336 posts

Posted 24 October 2004 - 04:28 PM

hm, apearently it's not working atm. it worked before, but today it's all ****ed up.
php says it doesn't know the function imagecreatefromjpeg... help!

#5 neno.tu

neno.tu

    Newbie [ Level 2 ]

  • Members
  • 24 posts
  • Location:Caracas, Venezuela

Posted 24 October 2004 - 04:43 PM

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:

imagecreate($width,$height)

That could make your pictures look preaty bad...

Use:

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

#6 marijnnn

marijnnn

    Premium Member

  • [HOSTED]
  • 336 posts

Posted 24 October 2004 - 05:15 PM

yeah, but all the extensions are disabled atm
gd, exif,...
i am using truecolor, don't worry :)
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...

#7 r3d

r3d

    death

  • Members
  • 268 posts

Posted 25 October 2004 - 04:43 AM

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

#8 avalon

avalon

    Advanced Member

  • Members
  • 160 posts
  • Interests:Arts, Philosophy, Archeology, Science, Psychology, ...

Posted 28 October 2004 - 07:17 PM

Final code should look like this.

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

<{POST_SNAPBACK}>


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

#9 avalon

avalon

    Advanced Member

  • Members
  • 160 posts
  • Interests:Arts, Philosophy, Archeology, Science, Psychology, ...

Posted 28 October 2004 - 07:20 PM

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

<{POST_SNAPBACK}>


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.

#10 r3d

r3d

    death

  • Members
  • 268 posts

Posted 28 October 2004 - 07:33 PM

please don't double post, use the edit button :)

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

#11 thedevil

thedevil

    Member - Active Contributor

  • Members
  • 82 posts

Posted 18 November 2004 - 03:47 AM

good tutorial but see I have a problem using GD. Coz I dont have the GD library. I visited the www.boutell.com and downloaded the GD Library but couldnot compile it for PHP. And they dont provide a DLL for PHP now...
Where Can I get the GD library? Refer me good sites for the same.

#12 marijnnn

marijnnn

    Premium Member

  • [HOSTED]
  • 336 posts

Posted 19 November 2004 - 10:42 AM

ok, a short sum up on some of the question.
saving as a jpeg: he tried but forgot the filename :P
imagejpeg ( $img, test.jpg , 78);
that'll do the trick
2. gd is installed on astahost. no problem. you can use it as you wish
3. installing gd on your own pc.
i guess you are using a windows machine as you mentioned dll. well, you have the dll, it's packed with php. what you need to do is find the lines that say which extensions to use.
unquote the extension that says gd.dll or gd2.dll
(you can do this by removing the ; in front of the line)
restart your iis or apache server.
done!

#13 r3d

r3d

    death

  • Members
  • 268 posts

Posted 19 November 2004 - 07:05 PM

php 4.9+ use gd2 which is bundled in the installation by default. but not enable by default. read marijnn's post and put the gd2.dll in windows folder also some dll is required like icon.dll you should put this dll in window folder too :P

#14 thedevil

thedevil

    Member - Active Contributor

  • Members
  • 82 posts

Posted 21 November 2004 - 08:55 AM

ok, a short sum up on some of the question.
saving as a jpeg: he tried but forgot the filename :P
imagejpeg ( $img, test.jpg , 78);
that'll do the trick
2. gd is installed on astahost. no problem. you can use it as you wish
3. installing gd on your own pc.
i guess you are using a windows machine as you mentioned dll. well, you have the dll, it's packed with php. what you need to do is find the lines that say which extensions to use.
unquote the extension that says gd.dll or gd2.dll
(you can do this by removing the ; in front of the line)
restart your iis or apache server.
done!

<{POST_SNAPBACK}>

Thanks I will Try that...

#15 vladimir

vladimir

    Member [ Level 2 ]

  • Members
  • 73 posts

Posted 28 December 2004 - 02:29 AM

What is actually the path for GD graphics library? I need it for my coppermine galery.

I would like to upload it manually because I'm using my own database...but I don't know the path for the GD. I have a backup of the mysql database I already used on a different server. It works fine, but just the thumbnails are not displayed.

Maybe somebody knows how to install/use GD for a coppermine picture galery? Thanks in advance for your help.

#16 Guest_iGuest-ableze_joepardy_*

Guest_iGuest-ableze_joepardy_*
  • Guests

Posted 19 August 2008 - 11:28 AM

How to call the image into other PHP page?
Basic Tutorial: PHP GD

Ok, I've done create a chart using GD.. And then I want to call that chart into my php page let say data.Php.. Then how to do that? I've tried using include function and even < img > tag but it just not work..

-question by ableze_joepardy

#17 ml01172

ml01172

    Newbie [ Level 1 ]

  • Members
  • 6 posts

Posted 08 October 2008 - 03:28 PM

What is actually the path for GD graphics library? I need it for my coppermine galery.

I would like to upload it manually because I'm using my own database...but I don't know the path for the GD. I have a backup of the mysql database I already used on a different server. It works fine, but just the thumbnails are not displayed.

Maybe somebody knows how to install/use GD for a coppermine picture galery? Thanks in advance for your help.


GD library isn't related to any of the user-libraries and scripts, like coppermine gallery etc., but strictly to PHP. You have to install or upgrade your PHP at the server so it contains GD.

Try doing this, for an example. Download PHP source from PHP.NET (try getting the version you already have installed) and, if you're on Linux, do the following:
1. Unpack the PHP package you got
2. Change directory into the created PHP directory.
3. Type:
./configure --with-gd
4. If configuration successfull, type:
make (not "make install", and not necessarily as root)

Once compilation process is over, look into the "modules" directory. A library with .so extension should be there so just copy it into your local "modules" directory for PHP (usually /usr/lib/php/modules or similar). Restart your web server.

Cheers

#18 Guest_(G)Steve_*

Guest_(G)Steve_*
  • Guests

Posted 06 January 2010 - 03:29 PM

Thanks.Basic Tutorial: PHP GD

This was very helpful for me in getting started with GD. I was wondering how to display a jpeg without having to save it as a jpeg file and this showed me. Also very simple examples of using the shape functions and how they relate to the "canvas". I can build on this in my applications. Thanks for taking the time to post this tutorial.

-reply by Steve





Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users