Working with GD

Tutorials for php.

Working with GD

Postby Mortisimus on Mon Aug 25, 2008 6:36 pm

Ok, first thing with Gd is you need to check if you have it enabled. So we will create a new file called phpinfo.php. In this file, put the following code.

Code: Select all
<?php
phpinfo();
?>


Now, fire up a browser and go to the location of this file. Scroll down the page until you see the heading "gd". Now if it says:

GD Support : Enabled

Image

Then congratulations, gd is installed for you! If not, speak to the owners of your server or see this article: http://uk.php.net/manual/en/image.installation.php.

Now on to the tutorial. Firstly, a very important thing to know is the imagecreate() function, all this does is creates a blank canvas for an images with the specified width and height.

Code: Select all
imagecreate($width, $height);


This is usually assigned to a variable so that we can eventually return the image as a valid image type, for example:
Code: Select all
$image = imagecreate(400, 100);

header("Content-type: image/png");
imagepng($image);


You must add the header to return what content type the file is, otherwise your browser will not read it as an image but as a php file.
You can use the following functions for returning canvas's as images: imagepng(), image2wbmp(), imagejpeg(), imagewbmp(), imagegd() and imagegd2().

Also, if you want to apply colours to your created image, you will need to use the function imagecolorallocate(). The syntax for this function is as follows:
imagecolorallocate(resource $image , int $red , int $green , int $blue)

So an example image could be as follows:

Code: Select all
<?php
$image = imagecreate(400, 100);
imagecolorallocate($image, 0xFF, 0, 0);

header("Content-type: image/png");
imagepng($image);
?>


This would return a red rectangle.

Strings
Ok. Now we know how to create an image, we can start writing strings into it. For this, we will use the imagestring() function. The syntax is as follows:
Code: Select all
imagestring (resource $image , int $font , int $x , int $y , string $string , int $color)


So, we have created the rectangle so lets put a test string into it. The $font parameter is the number of the gd font type. This goes from 1-5. We also need to create a color for our font to be, for this, we can also use imagecolorallocate. The $x and $y parameters are measurements for the string to start from point 0. So if $x was 20, the string would start 20 pixels into the image. Lets create a basic image with a string.

Code: Select all
<?php
//create image and set a bg colour
$image = imagecreate(400, 100);
imagecolorallocate($image, 0xFF, 0, 0);

//create string variables and write the string
$fontColor = imagecolorallocate($image, 255, 255, 255);
$startX     = 25;
$startY     = 25;
$string      = "Hello, world!";

imagestring($image, 1, $startX, $startY, $string, $fontColor);

header("Content-type: image/png");
imagepng($image);
?>


For this example, I have used font number 1 and used a white font colour.

Thanks for reading.

For more information on this subject, see: http://www.php.net/gd.
User avatar
Mortisimus
Site Admin
 
Posts: 21
Joined: Mon Aug 25, 2008 6:04 pm

Re: Working with GD

Postby rxGolem on Tue Aug 26, 2008 12:57 pm

Thanks! This is really useful. ;)
rxGolem
Newbie
 
Posts: 1
Joined: Tue Aug 26, 2008 12:57 pm

Re: Working with GD

Postby almsamim on Tue Aug 26, 2008 4:39 pm

thanks very , very , very useful :arrow:
almsamim
Newbie
 
Posts: 1
Joined: Tue Aug 26, 2008 4:35 pm

Re: Working with GD

Postby Mortisimus on Tue Aug 26, 2008 7:54 pm

No problem guys!
User avatar
Mortisimus
Site Admin
 
Posts: 21
Joined: Mon Aug 25, 2008 6:04 pm

Dow struggles higher

Postby sefskyclegync on Thu Nov 27, 2008 6:38 am

Hello

NEW YORK (CNNMoney.com) -- The Nasdaq slipped Tuesday, but the Dow and S&P 500 ended higher for the third straight session as investors welcomed the government's latest efforts to jumpstart lending, but remained cautious.

Treasury prices surged, lowering the corresponding yields. The dollar tumbled versus major currencie and oil prices slumped.

The Dow Jones industrial average (INDU) gained 0.4%, ending higher for the third session in a row. The last time the Dow gained for three straight sessions was in late August.

The Standard & Poor's 500 (SPX) index added 0.7%. The Nasdaq composite (COMP) lost 0.5%.

Stocks rose in the morning after the government announced a pair of new programs that will provide roughly $800 billion to increase the availability of consumer and mortgage lending. (Full story)

But the advance petered out and stocks plunged through the afternoon as investors sorted through weak economic readings and opted to cash out of some of the recent technology winners. The market ended in mixed territory.

"It's positive that the federal government is being proactive and the bond market certainly loves it today, because it's explicit demand for bonds," said Brian Battle, vice president at Performance Trust Capital Partners. "But the stock market is less certain."

He said stock investors may also be taking the scope of Tuesday's $800 billion announcement as a sign that things are actually as bad as some fear. The GDP and housing market reports added to those concerns.

There may also be frustration that despite the massive amounts of money being put to work by the government, the tangible improvement has been minimal, said Robert Loest, portfolio manager at Integrity Funds.

"All of this money is being used in an ad-hoc fashion that doesn't seem to be helping the job market or stabilizing the financial system," Loest said.

"I think we are creating a huge debt load and we're not really getting anywhere," he said.

On the plus side, he thinks the market is setting itself up for a short-term technical rally, due to both the psychology having gotten so bad recently and the enormous amount of cash on the sidelines.

Wednesday brings a buffet of economic reports, including readings on jobless claims, income and spending, manufacturing, housing and consumer sentiment.

Stocks surged Monday in a broad rally as Citigroup (C, Fortune 500)'s massive rescue package and President-elect Barack Obama's picks for his economic team pushed investors off the sidelines.

Including Friday's advance, the Dow gained 891.10 points for the period, its biggest two-session gain ever, according to Dow Jones. The two-day percentage gain of 11.8% was the biggest since October 1987. The S&P 500's rise of 13.2% was also its biggest two-session percentage gain since October 1987.

All financial markets are closed Thursday for Thanksgiving. Markets have a shortened session on Friday.

Economy: The U.S. economy saw its worst quarterly decline in seven years, the Commerce Department reported Tuesday. GDP declined at an annual rate of 0.5% in the third quarter versus an earlier reading of a decline of 0.3%. The drop in GDP was in line with expectations.

Although the U.S. is not officially considered to be in recession, it is generally believed to have been in recession since at least the start of the third quarter. The fourth quarter is expected to be the low point of the economic cycle and the first quarter of next year is expected to show weakness as well.

Another report showed the continued weakness of the housing market. The S&P Case-Shiller Home Price national index posted a 16.6% decline in the third quarter, the biggest drop on record.

But another report was a bit more upbeat, showing that consumer confidence recovered a bit in November from a record low hit in October.

Separately, the Federal Deposit Insurance Corp. (FDIC) said its watch list of problem banks spiked in the third quarter.

On the move: Big financial stocks continued to rise for a third session, including Citigroup (C, Fortune 500), JPMorgan Chase (JPM, Fortune 500), Morgan Stanley (MS, Fortune 500) and Goldman Sachs (GS, Fortune 500).

But many of the tech advancers of the last few sessions retreated, including Cisco Systems (CSCO, Fortune 500), Apple (AAPL, Fortune 500), Oracle (ORCL, Fortune 500) and Microsoft (MSFT, Fortune 500).

Late Monday, Hewlett-Packard (HPQ, Fortune 500) reported better-than-expected sales and revenue and forecast upbeat fiscal 2009 profit, matching its pre-announcement from a week earlier. However, analysts Tuesday questioned whether it would be able to maintain its sales pace considering the slowdown in tech spending. Shares fell 7%.

GM (GM, Fortune 500) shares continued to plunge amid worries about the ability of the company and the industry to stay afloat. (Full story)

In other news, BHP Billiton (BHP) is giving up its $68 billion all-stock hostile bid for fellow miner Rio Tinto (RTP), due to the downturn in commodities amid the economic slowdown. BHP shares gained 14.5%, while Rio shares fell 27%.

Market breadth was mixed and volume was moderate. On the New York Stock Exchange, winners beat losers two to one on volume of 1.88 billion shares. On the Nasdaq, decliners topped advancers five to four on volume of 2.52 billion shares.

Other markets: Asian markets rallied, with the Japanese Nikkei closing up 5.2%. European markets closed with moderate gains, with the London FTSE adding 0.4%.

U.S. light crude oil for January delivery fell $3.73 to settle at $50.77 a barrel on the New York Mercantile Exchange.

Gasoline prices continued to slump to 3-1/2 year lows, with gas down 2.3 cents to a national average of $1.885 a gallon, according to a survey of credit-card activity released Tuesday by AAA. Prices have been dropping for over two months. In that time, prices have lost $1.97 a gallon, or over 51%.

The dollar gained versus the euro and the yen.

Bonds: Treasury prices rallied, lowering the yield on the benchmark 10-year note to 3.10% from 3.34% Monday. Last week, the 2-year, 10-year and 30-year government bonds all hit the lowest levels since the Federal Reserve started keeping records in 1962.

The yield on the 3-month Treasury bill rose to 0.115% from 0.105% Tuesday, not far from 68-year lows of zero. The 3-month - seen as the safest place to put money in the short term - last hit these levels in September as investor panic peaked.

The low yield means nervous investors would rather preserve their money despite no interest rather than risk the stock market.

Lending rates rose a bit. The 3-month Libor rate rose to 2.2% from 2.17% Monday, while overnight Libor rose to 0.93% from 0.8% Monday, according to Dow Jones. Libor is a key bank lending rate.


Full story


Bye - aNNxoRRazXXO
sefskyclegync
Newbie
 
Posts: 1
Joined: Thu Nov 27, 2008 6:38 am
Location: Denmark


Return to PHP Tutorials

Who is online

Users browsing this forum: Google [Bot] and 1 guest

cron