ImageCreate

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
andyd34
Forum Newbie
Posts: 6
Joined: Sat May 10, 2008 7:42 pm

ImageCreate

Post by andyd34 »

I am tring to make a filled triangle, I have already made a lined triangle but for the life of can't can't figure our a way to fill it dynamically. Can anyone help, please


image.php

Code: Select all

 
    define("WIDTH", 50);
    define("HEIGHT", 50);
 
    $img = imagecreate(WIDTH, HEIGHT);
    $bkgColor = imagecolorallocate($img, $fgr, $fgg, $fgb);
    $frgColor = imagecolorallocate($img, 0, 0, 0);
 
    $points = array(0, 0, 0, HEIGHT, 0, 0, WIDTH-50, HEIGHT-1, WIDTH-1, 0); 
 
    imagefilledpolygon($img, $points, 5, $frgColor);
 
    header("Content-type: image/png");
    imagepng($img);
 

usage.php

Code: Select all

 
<?
$fgr= 198;
$fgg=226;
$fgb=255;
?>
<img src="temp1.php" />
 
Last edited by andyd34 on Tue Aug 26, 2008 9:29 am, edited 2 times in total.
andyd34
Forum Newbie
Posts: 6
Joined: Sat May 10, 2008 7:42 pm

Re: ImageCreate

Post by andyd34 »

Sorry, thought i'd sorted it but ended up with a 50px x 50px black box
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: ImageCreate [RESOLVED]

Post by Ziq »

To fill any figure use imagefill() function

Your example:

Code: Select all

 
<?
define("WIDTH", 50);
define("HEIGHT", 50);
$img = imagecreate(WIDTH, HEIGHT);
$white = imagecolorallocate($img, 0xFF, 0xFF, 0xFF);
$black = Imagecolorallocate($img, 0, 0, 0);
$points = array(0, 0, 0, HEIGHT, WIDTH, 0);
imagepolygon($img, $points, 3, $black);
imagefill($img, 1, 30, $black);
header("Content-type: image/png");
imagepng($img);
?>
 
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: ImageCreate

Post by Ziq »

This method incorrect

Code: Select all

 
<?
$fgr= 198;
$fgg=226;
$fgb=255;
?>
<img src="temp1.php" />
 
Use $_GET parameter for image.

Code: Select all

<img src="index.php?fgr=255&fgg=125&fgb=255" />
andyd34
Forum Newbie
Posts: 6
Joined: Sat May 10, 2008 7:42 pm

Re: ImageCreate

Post by andyd34 »

Sorted it

Code: Select all

 
<img src="index.php?fgr=<?=$fgr?>&fgg=?=$fgg?>&fgb=?=$fgb?>" />
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: ImageCreate

Post by onion2k »

You're relying on the PHP server being set up in a very specific, and very bad, way there. You're using both short tags and register_globals. If your script works on your production server you should really worry about the security of your website.
andyd34
Forum Newbie
Posts: 6
Joined: Sat May 10, 2008 7:42 pm

Re: ImageCreate

Post by andyd34 »

This isn't the actual script more of a how do I, here is the page its used on

Code: Select all

 
<?
$referer = 'edtColours.php';
if(!isset($_COOKIE['user'])) { header('location: login.php?referer='.$referer); }
 
include('../inc/functions.php');
 
 
if(!$_POST['submit'])
{ 
    include('../inc/colors.php');
} 
else
{ 
    $wsBg = $_POST['wsBg'];
    $contbgCol = $_POST['contbgCol'];
    $hedTxt = $_POST['hedTxt'];
    $pgTxt = $_POST['pgTxt'];
    $bkCol = $_POST['bkCol']; 
    $frCol = $_POST['frCol'];
    $AvTabCol = $_POST['AvTabCol'];
    $AvTabBord = $_POST['AvTabBord'];
    $AvTabHeadTxt = $_POST['AvTabHeadTxt'];
    $AvTabTxt = $_POST['AvTabTxt'];
    $AvTabLexTxt = $_POST['AvTabLexTxt'];
}
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?=$siteTitle?></title>
<script src="../inc/jscolor.js" type="text/javascript"></script> 
<link href="style/default.css" rel="stylesheet" type="text/css" />
</head>
 
<body>
<!-- Main Container Set at 800px wide -->
<div id="container">
    <!-- Navigation menu to modify edit navmnu.php -->
    <? include('navmnu.php'); ?>
    <!-- Contents window editable here -->
    <div id="contents">
    <h1>Edit Site Look</h1>
    <p>
 
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
    <table width="100%">
        <tr>
            <th colspan="2" style="font-size:18px" align="left">General Colours</th>
        </tr>
        <tr>
            <td>Wesbite Background</td>
            <td><input type="text" name="wsBg" class="color" style="border:solid 0px" size="5" value="<?=$wsBg?>" /></td>
        </tr>
        <tr>
            <td>Contents Background</td>
            <td><input type="text" name="contbgCol" class="color" style="border:solid 0px" size="5" value="<?=$contbgCol?>" /></td>
        </tr>
        <tr>
            <td>Heading Text</td>
            <td><input type="text" name="hedTxt" class="color" style="border:solid 0px" size="5" value="<?=$hedTxt?>" /></td>
        </tr>
        <tr>
            <td>Page Text</td>
            <td><input type="text" name="pgTxt" class="color" style="border:solid 0px" size="5" value="<?=$pgTxt?>" /></td>
        </tr>
        <tr>
            <td>Booked</td>
            <td><input type="text" name="bkCol" class="color" style="border:solid 0px" size="5" value="<?=$bkCol?>" /></td>
        </tr>
        <tr>
            <td>Free</td>
            <td><input type="text" name="frCol" class="color" style="border:solid 0px" size="5" value="<?=$frCol?>" /></td>
        </tr>
        <tr>
            <td>Table</td>
            <td><input type="text" name="AvTabCol" class="color" style="border:solid 0px" size="5" value="<?=$AvTabCol?>" /></td>
        </tr>
        <tr>
            <td>Table borbers</td>
            <td><input type="text" name="AvTabBord" class="color" style="border:solid 0px" size="5" value="<?=$AvTabBord?>" /></td>
        </tr>
        <tr>
            <td>Haed text</td>
            <td><input type="text" name="AvTabHeadTxt" class="color" style="border:solid 0px" size="5" value="<?=$AvTabHeadTxt?>" /></td>
        </tr>
        <tr>
            <td>Table text</td>
            <td><input type="text" name="AvTabTxt" class="color" style="border:solid 0px" size="5" value="<?=$AvTabTxt?>" /></td>
        </tr>
        <tr>
            <td>Legend text</td>
            <td><input type="text" name="AvTabLexTxt" class="color" style="border:solid 0px" size="5" value="<?=$AvTabLexTxt?>" /></td>
        </tr>
        <tr>
            <th colspan="2"><input type="submit" name="submit" /></th>
        </tr>
    </table>
 
</form>
 
<?
 
$rgbF = hexToRGB($frCol);
$rgbFr =  "fgr=".$rgbF["R"]."&fgg=".$rgbF["G"]."&fgb=".$rgbF["B"];
$rgbB = hexToRGB($bkCol);
$rgbBk = "bkr=".$rgbB["R"]."&bkg=".$rgbB["G"]."&bkb=".$rgbB["B"];
if($submit) {
                
                $fp = fopen("../inc/colors.php", "w");
                
                $str = "<"."?
                \$wsBg = \"$wsBg\";
                \$contbgCol = \"$contbgCol\";
                \$hedTxt = \"$hedTxt\";
                \$pgTxt = \"$pgTxt\";
                \$bkCol = \"$bkCol\";
                \$frCol = \"$frCol\";
                \$AvTabCol = \"$AvTabCol\";
                \$AvTabBord = \"$AvTabBord\";
                \$AvTabHeadTxt = \"$AvTabHeadTxt\";
                \$AvTabTxt = \"$AvTabTxt\";
                \$AvTabLexTxt = \"$AvTabLexTxt\";
                ?".">";
                
                $saved = fwrite($fp,$str);
                if($saved) { echo '<p align="center"><h1 style="color:#000099">Settings Saved</h1></p>'; } else { echo '<p align="center"><h1 style="color:#000099">Settings Not Saved</h1></p>'; }
                fclose($fp);
                
                }
 
?>
 
    </p>
    </div>
</div>
</body>
</html>
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: ImageCreate

Post by onion2k »

You're still using short tags, and you're also using $_SERVER['PHP_SELF'] for a form action which is rather dangerous.

Read this http://blog.phpdoc.info/archives/13-guid.html
andyd34
Forum Newbie
Posts: 6
Joined: Sat May 10, 2008 7:42 pm

Re: ImageCreate

Post by andyd34 »

I do see your point but as its in a secure area and only I have access to the file I don't thimk this will be an issue. Its just to amke my life a bit easier
Post Reply