Web Hosting Talk







View Full Version : Integrate GD into PHP


Snitz
04-30-2007, 09:31 AM
I would like to know how to integrate the GD library into my php code.
I have in my admincp a place where to post news with pictures and everything, in the picture field file, I would like it to be using the GD library so that it creates thumbnails on the fly and to enlarge whenever I click on it.

This is the code!

Projects.php

<? require "top.php"; ?>
<h3>Projects</h3>
<?

switch ($action):
case "add":
$showing = "record";
break;
case "edit":
$strSQL="select * from projects where id=".$id;
$objRS=mysql_query($strSQL);
if ($row=mysql_fetch_object($objRS)){
$title = $row->title;
$para = $row->para;
$image = $row->image;
$align = $row->align;
$date = $row->date;

}
$showing = "record";
break;
case "editexe":
case "addexe":

$title=trime($HTTP_POST_VARS['title']);
$para=trime($HTTP_POST_VARS['para']);
$image=trime($HTTP_POST_VARS['image']);
$align=trime($HTTP_POST_VARS['align']);
$date=trime($HTTP_POST_VARS['date']). " ". trime($HTTP_POST_VARS['hours']) .":". trime($HTTP_POST_VARS['minutes']);
if ($result=file_upload('image','../img/',$msg)) $image=$result['name'];
/* if ($result=file_upload('image','../img/',$msg)) $image=$result['name'];*/
//Conditions
switch(true):
case strlen($title)=='': $msg="Title Cannot be Empty"; break;
//case strlen($para)=='': $msg="Paragraph Cannot be Empty"; break;
case strlen($date)=='': $msg="Date Cannot be Empty"; break;
endswitch;


//If conditions are fine
if ($msg==""):

$strSQL="projects set
title= '".sqlencode($title)."',
para= '".sqlencode($para)."',
image= '".sqlencode($image)."',
align= '".sqlencode($align)."',
date= '".sqlencode($date)."'";
if ($action=="editexe") $strSQL ="update ".$strSQL." where id=".$id; else $strSQL ="insert into ".$strSQL;
mysql_query($strSQL);
else:
$action = substr ($action,0,strlen($action)-3);
$showing = "record";
endif;
break;
case "delete":
foreach ($ids as $ids_item){
$strSQL="select image from projects where id = ".$ids_item;
$objRS=mysql_query($strSQL);
if ($row=mysql_fetch_object($objRS)):
if ($row->image!="") @unlink("../img/$row->image");
endif;
$strSQL="delete from projects where id = ".$ids_item;
mysql_query($strSQL);
}
endswitch;
?>
<br>

<table align=center class="title" cellspacing=0>
<tr>
<td align=center>
<?
switch ($showing):
case "record":
?>
<p class="alert"><? echo $msg; ?></p>
<form action="<? echo $filename; ?>" method="post" enctype="multipart/form-data" onsubmit="return submitForm();">
<table>
<tr>
<th align="right" valign="top">Title:</th>
<td><input name="title" type="text" value="<? echo textencode($title) ?>" size="50"></td>
</tr>
<tr>
<th align="right" valign="top">Paragraph:</th>
<td>

<script language="JavaScript" type="text/javascript">
<!--
function submitForm() {
//make sure hidden and iframe values are in sync before submitting form
//to sync only 1 rte, use updateRTE(rte)
//to sync all rtes, use updateRTEs
updateRTE('para');
//updateRTEs();

//change the following line to true to submit form
return true;
}

//Usage: initRTE(imagesPath, includesPath, cssFile, genXHTML)
initRTE("images/", "", "", true);
//-->
</script>
<noscript><p><b>Javascript must be enabled to use this form.</b></p></noscript>

<script language="JavaScript" type="text/javascript">
<!--
<?php
//format content for preloading
if (!(isset($_POST["para"]))) {
$content = $para;
$content = rteSafe($content);
} else {
//retrieve posted value
$content = rteSafe($_POST["para"]);
}
?>//Usage: writeRichText(fieldname, html, width, height, buttons, readOnly)
writeRichText('para', '<?=$content?>', 520, 200, true, false);
//-->
</script>

</td>
</tr>
<tr>
<th align="right">Image:</th>
<td><? file_field('image','../img/',50);?></td>
</tr>
<tr>
<th align="right">Align:</th>
<td><select name="align" id="align">
<option value="left" <? if ($align == 'left') { echo "selected";}?>>Left</option>
<option value="center" <? if ($align == 'center') { echo "selected";}?>>Center</option>
<option value="right" <? if ($align == 'right') { echo "selected";}?>>Right</option>
</select> <small>(If there's no image leave it as is)</small></td>
</tr>

<tr>
<th align="right">Date:</th>
<td><script>DateInput('date', true, 'YYYY-MM-DD' <?if ($date) { echo ", '" .textencode(substr("$date", 0, 10))."'";} ?>)</script>

<script>
function validateTheDate() {
var Today = new Date();
if (someDate_Object.picked.date < Today) alert('Cannot select a date in the past.');
else if (someDate_Object.picked.yearValue > 2020) alert('Cannot select dates beyond 2020.');
}
</script> </td>
</tr> <tr>
<th align="right">Time:</th>
<td><select name="hours">
<option value="00">00</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
</select>
:
<select name="minutes" id="minutes">
<option value="00">00</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
<option value="32">32</option>
<option value="33">33</option>
<option value="34">34</option>
<option value="35">35</option>
<option value="36">36</option>
<option value="37">37</option>
<option value="38">38</option>
<option value="39">39</option>
<option value="40">40</option>
<option value="41">41</option>
<option value="42">42</option>
<option value="43">43</option>
<option value="44">44</option>
<option value="45">45</option>
<option value="46">46</option>
<option value="47">47</option>
<option value="48">48</option>
<option value="49">49</option>
<option value="50">50</option>
<option value="51">51</option>
<option value="52">52</option>
<option value="53">53</option>
<option value="54">54</option>
<option value="55">55</option>
<option value="56">56</option>
<option value="57">57</option>
<option value="58">58</option>
<option value="59">59</option>
<option value="60">60</option>
</select></td>
</tr>
</table>
<input type="hidden" name="submit" value="Submit">
<input type="hidden" name="action" value="<? echo $action; ?>exe">
<input type="hidden" name="id" value="<? echo $id; ?>">
<input type="submit" value="Save">
</form>



<a href="<? echo $PHP_SELF; ?>" class=item>List All Records</a><br>
<?
break;
default:
$strSQL="select * from projects";
$objRS=mysql_query($strSQL);
?>
<form action="<? echo $filename; ?>" method="post" name="del">
<table cellspacing="1">
<tr>
<th><input type="checkbox" onclick="checkall()" name="main"></th>
<th>Title</th>
<th>Date</th>

<th></th>
</tr>
<? $counter=0;
while ($row=mysql_fetch_object($objRS)):
$color=($color=="color1"?"color2":"color1");?>
<tr class="<? echo $color?>">
<td><input type="checkbox" name="ids[]" value="<? echo $row->id; ?>"></td>
<td valign="top"><? echo $row->title; ?></td>
<td valign="top"><?=date("d F Y h:i a", strtotime($row->date))?></td>

<td><a href="<? echo $filename; ?>?action=edit&id=<? echo $row->id; ?>">Edit</a></td>
</tr>
<? endwhile; ?>

<tr>
<td colspan=10>
<input type="button" value="Inverse" onclick="invall()">
<input type="button" value="Delete" onclick="conf()">
<input type="button" value="Add" onclick="window.location='<? echo $filename; ?>?action=add'">
</td>
</tr>
</table>
<input type=hidden name="action" value="delete">
</form>
<script language="javascript">
function conf(){
if (confirm("Are you sure you want to delete this/these record(s)?"))
window.del.submit();
}

function checkall(){
i=0;
while (temp=del.elements[i++])
if (temp.name.substr(0,3)=="ids")
temp.checked=del.main.checked;
}

function invall(){
i=0;
del.main.checked=false;
while (temp=del.elements[i++])
if (temp.name.substr(0,3)=="ids")
temp.checked=!temp.checked;
}
</script>
<? endswitch; ?>
</td>
</tr>
</table>

<? require "bottom.php"; ?>


And these are the functions inside functions.php

function fname_split($file){
if (strstr($file,'.')){
preg_match('/(^.+)\.(.*$)/',$file,$matches);
list(,$basic_name,$ext_name)=$matches;
} else {
$basic_name=$file;
}
$basic_name=preg_replace('/(\[\d+\])+$/','',$basic_name);
return array($basic_name,$ext_name);
}

# July 25, 2003 n1
# Creates the field for edit-upload file
// add only upload
function file_field($myvar,$path,$maxlength){
global $$myvar;
if ($$myvar!='') echo "<a href=\"javascript:\" onclick=\"window.open('$path","${$myvar}','','width=500,height=400,scrollbars,resizable')\"><img src=\"images/view.gif\" border=\"0\"></a>\n";
echo "<input type=\"text\" name=\"$myvar\" value=\"",textencode($$myvar),"\" maxlength=\"$maxlength\">\n";
echo "<input type=\"hidden\" name=\"$myvar","old\" value=\"",textencode($$myvar),"\">\n";
echo "<input type=\"file\" name=\"$myvar","file\">\n";
}

# July 25, 2003 n1
# uploads a file
# - Needs: fname_split
// add delete file
function file_upload ($myvar,$path,&$status,$max_size=''){
global $HTTP_POST_FILES,$HTTP_POST_VARS;
$old_name=$HTTP_POST_VARS[$myvar.'old'];
$file_name=$HTTP_POST_VARS[$myvar];
$real_name=$HTTP_POST_FILES[$myvar.'file']['name'];
$temp_name=$HTTP_POST_FILES[$myvar.'file']['tmp_name'];
$file_size=$HTTP_POST_FILES[$myvar.'file']['size'];
if (!$real_name){
if ($file_name!=$old_name)
if (file_exists($path.$old_name) and !file_exists($path.$file_name) and $file_name!='')
rename($path.$old_name,$path.$file_name);
return false;
} elseif (!is_uploaded_file($temp_name)){
$status.="File \"$real_name\" is not uploaded!<br>";
return false;
} elseif ($max_size !='' and $file_size>$max_size){
$status.="File \"$real_name ($file_size bytes)\" is larger than the maximum allowed of $max_size bytes.<br>";
return false;
} else {
if (file_exists($path.$old_name)) @unlink($path.$old_name);
list($basic_name,$ext_name)=fname_split($real_name);
$destination_file=$real_name;
while (file_exists($path.$destination_file)) $destination_file=$basic_name.'['.++$i.']'.($ext_name!=''?".$ext_name":'');
$result['name']=$destination_file;
$result['size']=$file_size;
$result['ext_name']=$ext_name;
if (!move_uploaded_file($temp_name,$path.$destination_file)){
$status.="Error in moving the temp file \"$temp_name\" of \"$real_name ($file_size bytes)\"";
return false;
}
return $result;
}
}

I would really love the help, thanks!