PHP + Upload + picture script

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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


Re: PHP + Upload + picture script

For the uploading part, here's some basic code:
if(@$_POST['MAX_FILE_SIZE']){ // If it is actually uploading
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)){
        echo "File was successfully uploaded!\n You can access the file at:
    } 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" />

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

function CreateThumb($file,$maxwdt,$maxhgt, $dest) {

  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,

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

       if(!$dest) return $newimg;


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



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) {
  } else {





   $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, if
you need %s*

Quoted text here. Click to load it

Re: PHP + Upload + picture script

Quoted text here. Click to load it

Take a look on the site. You will need to register but
I've never had a problem from there.
There are some excellent and well documented class files that can be
used to handle image resize (amongst other things) and by reading over
the code you can learn how they work while solving a problem straight
Hope that helps

Re: PHP + Upload + picture script

aussiebob schreef:
Quoted text here. Click to load it

I agree ... I just posted a modified script in alt.php that I downloaded
from phpclasses that exactly  fits your needs

Re: PHP + Upload + picture script

Quoted text here. Click to load it

I developed a little script that I used only for jpegs so far.  I haven't  
tested it with other image types.  Here it is:

function thumbnail($img, $w_d, $h_d) {
 // $img is the image
 // $w_d is the desired width in pixels
 // $h_d is the desired height in pixels
 // function to get the width and height to display q thumbnail
 // Pass in the image location, the deisred width and the desired height
 // Ouput is an array of height and width to display that can be
 // exploded with the list commant
    list($w, $h, $type, $attr) = getimagesize($img);
   $calc_width = $h_d * $w / $h;
   $calc_height = $w_d * $h / $w;
   if ($calc_width <= $w_d) {
      $disp_width = $calc_width;
      $disp_height = $h_d;
   } else {
      $disp_width = $w_d;
      $disp_height = $calc_height;
   return array($disp_width, $disp_height);

I call it with:

list($disp_width, $disp_height) = thumbnail($the_picture_location, 500,  

and in the html area I have:

<img src="the-picture-location" width="<?php echo $disp_height;?>"  
height="<?php echo $disp_height;?>"
border="0" alt="something here" align="bottom" />

Hope that helps.


Re: PHP + Upload + picture script wrote:

Quoted text here. Click to load it

Actually, I developed just that type of function for a member system
where members would be able to upload a photo of themselves.

Here's the code snippet along with usage:

The function will accept JPEG, GIF and PNG files as input and by
default output a JPEG file.

Kim André Akerø
(remove NOSPAM to contact me directly)

Site Timeline