Re: PHP + Upload + picture script



For the uploading part, here's some basic code:
<?php
if(@$_POST['MAX_FILE_SIZE']){ // If it is actually uploading
something...
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
//SET YOUR OWN UPLOAD DIR (absolute)
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)){
echo "File was successfully uploaded!\n You can access the file at:
http://".ROOT_DOMAIN.$uploaddir.basename($_FILES['userfile']['name'])."\n";
} else {
echo "Error! File was not uploaded!\n";
};
};
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?admin=<?php echo
ADMIN_PAGE; ?>" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="300000" />
<input type="file" name="userfile" /><br />
<input type="submit" value="Upload" />
</form>

You can find a basic image resizing script @ php.net under GD2
functions -

<?php
function CreateThumb($file,$maxwdt,$maxhgt, $dest) {
list($owdt,$ohgt,$otype)=@getimagesize($file);

switch($otype) {
case 1: $newimg=imagecreatefromgif($file); break;
case 2: $newimg=imagecreatefromjpeg($file); break;
case 3: $newimg=imagecreatefrompng($file); break;
default: echo "Unkown filetype (file $file, typ $otype)"; return;
}

if($newimg) {
if($owdt>1500 || $ohgt>1200)
list($owdt, $ohgt) = Resample($newimg, $owdt, $ohgt,
1024,768,0);

Resample($newimg, $owdt, $ohgt, $maxwdt, $maxhgt);

if(!$dest) return $newimg;

if(!is_dir(dirname($dest)))
mkdir(dirname($dest));

switch($otype) {
case 1: imagegif($newimg,dest); break;
case 2: imagejpeg($newimg,$dest,90); break;
case 3: imagepng($newimg,$dest); break;
}

imagedestroy($newimg);

chmod($dest,0644);
}
}

function Resample(&$img, $owdt, $ohgt, $maxwdt, $maxhgt, $quality=1) {
if(!$maxwdt) $divwdt=0;
else $divwdt=Max(1,$owdt/$maxwdt);

if(!$maxhgt) $divhgt=0;
else $divhgt=Max(1,$ohgt/$maxhgt);

if($divwdt>=$divhgt) {
$newwdt=$maxwdt;
$newhgt=round($ohgt/$divwdt);
} else {
$newhgt=$maxhgt;
$newwdt=round($owdt/$divhgt);
}

$tn=imagecreatetruecolor($newwdt,$newhgt);
if($quality)

imagecopyresampled($tn,$img,0,0,0,0,$newwdt,$newhgt,$owdt,$ohgt);

else
imagecopyresized($tn,$img,0,0,0,0,$newwdt,$newhgt,$owdt,$ohgt);

imagedestroy($img);

$img = $tn;

return array($newwdt, $newhgt);
}
?>

just use the function create thumb to set the dimensions and the
destination file

*There is a script that will work out the % size for you on php.net, if
you need %s*

On Oct 30, 5:19 pm, "jason.ta...@xxxxxxxxx" <jason.ta...@xxxxxxxxx>
wrote:
I was wondering if anyone does any uploading picture scripts or how to
do it where i could upload a picture and use php to change the picture
size to what i want it to be. Anyone know where i could learn how to do
this or have any script examples? i would really appreciate it alot.

thanks for you time

jason

.



Relevant Pages

  • Re: file upload problem
    ... I am trying to learn about uploading files with php. ... I made a simple form script and and file up load script. ... The form script gets the file name correctly and seems to pass it to the upload script correctly. ... if I echo ini_getI see ...
    (comp.lang.php)
  • Re: file upload problem
    ... I am trying to learn about uploading files with php. ... I made a simple form script and and file up load script. ... The form script gets the file name correctly and seems to pass it to the upload script correctly. ... if I echo ini_getI see ...
    (comp.lang.php)
  • Re: move_uploaded_file fail to open stream in Debian Linux?
    ... these are very simple script to upload file by http to the server, this work well under Window, both in IIS and Apache but when run under Debian Linux, Apache then php give error that it failed to open stream due to permission denied and therefore couldn't move the file ... echo "File is valid, and was successfully uploaded. ... I check both /tmp and /upload directories, they all have read and write permission to everybodies. ...
    (php.general)
  • Re: file upload problem
    ... I am trying to learn about uploading files with php. ... I made a simple form script and and file up load script. ... The form script gets the file name correctly and seems to pass it to the upload script correctly. ... if I echo ini_getI see ...
    (comp.lang.php)
  • Re: file upload problem
    ... I am trying to learn about uploading files with php. ... I made a simple form script and and file up load script. ... The form script gets the file name correctly and seems to pass it to the upload script correctly. ... if I echo ini_getI see ...
    (comp.lang.php)