Jump to content
xisto Community
Sign in to follow this  
r3d1405241470

Basic Tutorial: PHP GD basic tuts

Recommended Posts

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://php.net/manual/en/ref.image.php. Xisto 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 :)

Share this post


Link to post
Share on other sites

well, to give you an idea what you could use this for: thumbnailsi'm a lazy kid so i upload my pictures with ftpthen some guy/girl visits my site and my picture pagehe opens the directory with the new picturesthe 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!

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

yeah, but all the extensions are disabled atmgd, exif,...i am using truecolor, don't worry :)it works fine on my own computer, but it's not working with Xisto. but i've heard/read that they are having problems, so i'll just wait till those are solved...

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

outch  :)

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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 trick2. gd is installed on Xisto. no problem. you can use it as you wish3. 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!

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.