Jump to content

Archived

This topic is now archived and is closed to further replies.

benpaxton777

Help a Noobie!!! Image Resize, Upload, and Insert

Recommended Posts

As a Noobie to php, I am trying to resize/upload an image to a directory and insert other the file name along with other info like a name and bio to a database. I found a resize image class, but I don't know how to use it. Also since this will be apart of a CMS I will need to beable to delete and edit the image along with the other bio fields. Below is the form and resize class.
???

Upload Form with text fields and file form.



<form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="form1" id="form1">
              <table width="100%" border="0" cellspacing="0" cellpadding="1">
                <tr>
                  <td>Name</td>
                  <td><input type="text" name="textfield" /></td>
                </tr>
                <tr>
                  <td>Bio: </td>
                  <td><textarea name="Bio" id="Bio"></textarea></td>
                </tr>
                <tr>
                  <td>Image:
                  <input name="max_file_size" type="hidden" id="max_file_size" /></td>
                  <td><input type="file" name="file" /></td>
                </tr>
                <tr>
                  <td>`                  </td>
                  <td><input type="submit" name="Submit" value="Submit" /></td>
                </tr>
              </table>
              <input type="hidden" name="MM_insert" value="form1">
          </form>

Resize Class I found:

<?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>";
              }
      }
}
?>

Share this post


Link to post
Share on other sites
I personally would start out with just trying to move a file to a directory

[code]
<?php
if(move_uploaded_file($_FILES['upload']['tmp_name'], "somefolder/tostore/{$_FILES['upload']['name']") {
  // File moved succesfully
} else {
  // Error with moving file
}
?>[/code]

After you can succesfuly move a file, then learn to do the MySQL (or whatever database you use) queries to insert the information you need.  INSERT, UPDATE, DELETE will be the ones you'll need to look at.

Then look at tackling the class that handles the image uploading.

Now if you can do all that and I have completely mis-read your post I am sorry.

:)

Share this post


Link to post
Share on other sites
Yes I have uploaded images before but I have not worked with a class that resized an image. I have used the interaktonline extension to upload and resize images, but this extension doesn't allow a CMS to delete the images, and my trial period ended. Any help in developing a solution that can use the resize class above would be helpful.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.