Jump to content


Photo

using class


  • Please log in to reply
1 reply to this topic

#1 ess14

ess14
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts

Posted 28 September 2006 - 02:14 AM

i really suck... can someone tell me how i would implement this class? what would my form look like and how do i make it all work? thanks in advance. i could learn alot from this.

<?php
class picture
{
       var $save_dir;                    //where file will be saved
       var $filename="spacer.gif";        //default file name initially
       var $error_message="";            //string to be output if neccesary
       var $width;                        //height of final image
       var $height;                      //width of final image

       function picture($save_directory, $file_array, $max_width, $max_height)
       {
               $this->save_dir = $save_directory;               
               $this->width =    $max_width;
               $this->height =  $max_height;

               //--change filename to time - make it unique
               $temp_filename = $file_array['name'];
               $ext = explode('.',$temp_filename);
               $ext = $ext[count($ext)-1];
               $temp_filename = time().".".$ext;

               //--check that it's a jpeg or gif
               if (preg_match('/^(gif|jpe?g)$/',$ext)) {
                       // resize in proportion
                       list($width_orig, $height_orig) = getimagesize($file_array['tmp_name']);
                       if ($this->width && ($width_orig < $height_orig)) {
                               $this->width = ($this->height / $height_orig) * $width_orig;
                       } else {
                               $this->height = ($this->width / $width_orig) * $height_orig;
                       }

                       $image_p = imagecreatetruecolor($this->width, $this->height);                       

                       //handle gifs and jpegs separately
                       if($ext=='gif'){
                           $image = imagecreatefromgif($file_array['tmp_name']);                           
                           imagecopyresampled($image_p, $image, 0, 0, 0, 0, $this->width, $this->height, $width_orig, $height_orig);
                           imagegif($image_p, $this->save_dir.$temp_filename, 80);
                       }
                       else
                       {
                           $image = imagecreatefromjpeg($file_array['tmp_name']);                           
                           imagecopyresampled($image_p, $image, 0, 0, 0, 0, $this->width, $this->height, $width_orig, $height_orig);                           
                           imagejpeg($image_p, $this->save_dir.$temp_filename, 80);
                       }

                       imagedestroy($image_p);
                       imagedestroy($image);

                       $this->filename=$temp_filename;

               }else{
                       $this->error_message.="<br> file is not a jpeg or gif picture <br>";
               }
       }
}
?>


and this is my form  :(

<?php
$save_directory = "users/";
$max_width = 40;
$max_height = 40;
	$fileIt = $myclass->picture($save_directory, $max_width, $max_height);
	?>
</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><form enctype="multipart/form-data" action="<? $fileIt; ?>" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
  
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form></td>
    <td>&nbsp;</td>
  </tr>
</table>

im getting the error that its missing 4 arguments from the picture();
please tell me how stupid i am and what i can do to make this work.
thanks.

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 28 September 2006 - 07:35 AM

try
<?php
include 'picture.class.php';

if (isset($_POST['submit'])) {
    $save_directory = "users/";
    $max_width = 40;
    $max_height = 40;
    
	$fileIt = new picture($save_directory, $_FILES['userfile'], $max_width, $max_height);
}
?>
</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><form enctype="multipart/form-data" action="" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
  
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form></td>
    <td>&nbsp;</td>
  </tr>
</table>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users