Hi all,
I have below the script which would allow me to upload an image and resize it.
There is something which doesn't work.
near the function add
can someone see whats wrong?
Thanks
Salvo
########### from start from here #######################
<form enctype=\"multipart/form-data\" method=\"post\" action=\"mod.php?mod=classifieds&op=Add\">
<table>
<tr><td>Category:</td>
<td><select name=\"cat\">";
echo "<option value=\"\">Select Category</option>\n";
$result=mysql_query("select cid, title, parentid from mod_classifieds_categories order by parentid,title");
while(list($cid2, $ctitle2, $parentid2) = mysql_fetch_row($result))
{
if ($parentid2!=0) $ctitle2=getparent($parentid2,$ctitle2);
echo "<option value=\"$cid2\">$ctitle2</option>\n";
}
echo "</select>
</td></tr>
<tr><td>Title:</td><td><input type=\"text\" name=\"title\" size=\"45\" maxlength=\"100\" /></td></tr>
<tr><td valign=\"top\">Ads Text:</td>
<td><textarea name=\"description\" cols=\"40\" rows=\"15\" wrap=\"virtual\"></textarea></td></tr>
<tr><td> </td><td>Maximum ads text: $adstext characters.</td></tr>
<tr><td>Website:</td><td><input type=\"text\" name=\"url\" size=\"45\" maxlength=\"100\" value=\"http://\" /></td></tr>
<tr><td>Expire:</td><td>";
prweek($mostweeks, $defaultweeks);
echo " Days</td></tr>";
echo " <tr>\n";
echo " <td width=\"130\" height=\"30\"></td>\n";
echo " <td width=\"370\" height=\"30\"><input type=\"file\" name=\"photo[]\"></td>\n";
echo " </tr>\n";
echo "
<tr><td colspan=2 align=center>
<input type='hidden' name='uname' value='$cookie[1]'>
<input type=\"hidden\" name=\"op\" value=\"Add\" />
<input type=\"submit\" value=\"Submit\" />
</td></tr>
</table>
</form>
########### from End here #######################
</td></tr>";
} else {
echo "
<tr>
<td>
<p>Only members can post ads. Feel free to <a href=\"user.php\">Register</a>.</td>
</tr>";
}
echo "
</table>";
}
function Add($cat, $title, $description, $url, $weeks, $uname)
{
include "config.php";
include "./mod/classifieds/classifieds_config.php";
$postdate = time();
$expiredate = $postdate + (3600 * 24 * $weeks);
$url = explode("http://", $url);
if ($url[1]=="")
{
$myurl = "";
} else {
$myurl ="$url[1]";
}
// Check if Category exist
if ($cat=="")
{
include("header.php");
echo "<br />";
$mainlink = 1;
menu($mainlink);
echo "<br /><br /><span class=\"onebiggerred\">Please select a category!</span><br />";
include("footer.php");
exit();
}
// Check if Title exist
if ($title=="")
{
include("header.php");
echo "<br />";
$mainlink = 1;
menu($mainlink);
echo "<br /><br /><span class=\"onebiggerred\">Where is your ads title?</span><br />";
include("footer.php");
exit();
}
// Check if ads text exist
if ($description=="")
{
include("header.php");
echo "<br />";
$mainlink = 1;
menu($mainlink);
echo "<br /><br /><span class=\"onebiggerred\">Write your ads text please...</span><br />";
include("footer.php");
exit();
}
// Check ads text length
if (strlen($description) > $adstext)
{
include("header.php");
echo "<br />";
$mainlink = 1;
menu($mainlink);
echo "<br /><br /><span class=\"onebiggerred\">Your text ads too long. Maximum: $adstext characters.</span><br />";
include("footer.php");
exit();
}
for( $i = 0; $i < count( $photo ); $i++ )
{
if( $photo[ $i ] != "" && $photo[ $i ] != "none" )
{
if( $photo_size[ $i ] > 500000 ) error( "Invalid image size" );
if( $photo_type[ $i ] == "image/pjpeg" || $photo_type[ $i ] == "image/jpeg" )
$newphoto[] = $photo[ $i ];
else
error( "Invalid image type, JPEG only" );
}
}
$title = stripslashes(FixQuotes($title));
$url = stripslashes(FixQuotes($url));
$description = stripslashes(FixQuotes($description));
mysql_query("insert into mod_classifieds values (NULL, '$cat', '$title', '$description', '$myurl', '$postdate', '$expiredate', '$uname')");
mysql_query("delete low_priority from mod_classifieds where(expiredate < '$postdate')");
$newid = mysql_insert_id();
for( $i = 0; $i < count( $newphoto ); $i++ )
{
mysql_query( "INSERT INTO photo ( lid_id ) VALUES ( $newid )" ) or error( mysql_error() );
$photoid = mysql_insert_id();
copy( $newphoto[ $i ], "{$IMAGE_DIR}/{$photoid}.jpg" );
makeThumb( $newphoto[ $i ], "{$IMAGE_DIR}/thumb/{$photoid}.jpg" );
unlink( $newphoto[ $i ] );
}
Header("Location: mod.php?mod=classifieds&op=viewlink&cid=$cat");
}
function makeThumb( $scrFile, $dstFile, $dstW=120, $dstH=100 )
{
$im = ImageCreateFromJPEG( $scrFile );
$srcW = ImageSX( $im );
$srcH = ImageSY( $im );
$ni = ImageCreate( $dstW, $dstH );
ImageCopyResized( $ni, $im, 0, 0, 0, 0, $dstW, $dstH, $srcW, $srcH );
ImageJPEG( $ni, $dstFile );
}