You are here: Re: PHP + Upload + picture script « PHP Programming Language « IT news, forums, messages
Re: PHP + Upload + picture script

Posted by josh.23.french on 10/30/06 22:40

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...@gmail.com" <jason.ta...@gmail.com>
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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация