image creation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Coca-Bear
Forum Newbie
Posts: 16
Joined: Wed Sep 18, 2002 9:55 pm
Location: SA
Contact:

image creation

Post by Coca-Bear »

hi all again, i have a little prob. :p

i'm trying to learn imagecreate()

i know my webhoster supports gd library cause it says in phpinfo();

GD Support enabled
GD Version 1.6.2 or higher
FreeType Support enabled
FreeType Linkage with freetype
JPG Support enabled
PNG Support enabled
WBMP Support enabled

but when i try this(from a tutorial)

Code: Select all

<HTML>
<HEAD>
<TITLE> Images Creation </TITLE>
</HEAD>
<BODY>

<?php
Header("Content-Type: image/jpg");
$im = ImageCreate(100, 100);
$black = ImageColorAllocate($im, 0, 0, 0);
$white = ImageColorAllocate($im, 255, 255, 255);
ImageLine($im, 1, 1, 99, 99, $white);
Imagejpeg($im);
ImageDestroy($im);
?>

</BODY>
</HTML>
when i load up image.php it asks for me to download the file? i can view other php. files prefectly. but not this one.

when i tried include it sometimes ask me to download the file. or comes up with alot of weird characters.

help? :p

i'm using f2o as my hoster. and they have php 4.2.2.

Thanks

Coca-Bear
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try changing this:

Code: Select all

image/jpg
to this

Code: Select all

image/jpeg
Mac
Coca-Bear
Forum Newbie
Posts: 16
Joined: Wed Sep 18, 2002 9:55 pm
Location: SA
Contact:

Post by Coca-Bear »

ok i tried that mac. the whole page loads. but no pic is showing. no X or anything.

i tried an even simplier ImageCreate()

Code: Select all

<?
Header("Content-Type: image/jpg");
$im = ImageCreate(500, 75);
$red = ImageColorAllocate($im, 255, 100, 0);
ImageFill($im, 100, 100, $red);
ImageJPG($im);
?>
again loads. but no pic.

Thanks

Coca-Bear
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try removing that HTML stuff you've got so that all you have in the page is:

Code: Select all

<?php 
Header("Content-Type: image/jpeg"); 
$im = ImageCreate(100, 100); 
$black = ImageColorAllocate($im, 0, 0, 0); 
$white = ImageColorAllocate($im, 255, 255, 255); 
ImageLine($im, 1, 1, 99, 99, $white); 
Imagejpeg($im); 
ImageDestroy($im); 
?>
This works as it should for me.

Mac
Coca-Bear
Forum Newbie
Posts: 16
Joined: Wed Sep 18, 2002 9:55 pm
Location: SA
Contact:

Post by Coca-Bear »

ok that worked. :p

Thanks for the help

Coca-Bear
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

so that only works on a page with no html?

meaning that if i wanted to put that image within a page i would need to save the resulting picture somewhere?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You can put the image into your page like you would a normal image:

Code: Select all

<img src="test.php" alt="PHP generated image" />
But if you're going to display the same image a lot it would make sense to take the load off the server and save the resulting picture as a normal jpeg (or whatever) and use that instead of dynamically generating it each time.

Mac
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

one http-request is exactly that, _one_ document.
If you have an picture 'within' your html-document that are two http-requests. First the html-document (the browser parses this, finds the <img>-element and requests) then the image 'document'.

If you open the "source view" for a HTML-doc in your browser you won't see any picture data, only the <img>-element having a src-attribute that contains the url for the document that contains the data. [still ignoring <img src="data:image/gif;base64,..... syntax ;) ]
Post Reply