Jump to content

CMS Image upload/Resize and Delete.


benpaxton777

Recommended Posts

I am developing a simple CMS where users can upload an image (jpg, gif, etc) that is resized along with a name and bio. I found a resize and upload class on php.net, but I can't find or figure out how to edit or delete the image along with the other fields. Any help would be great.


Regards,

Ben
Link to comment
Share on other sites

Good suggestion hitman.

Below is the class I am going to try to integrate. I did not write it and I haven't tested it, but I plan on using it to resize and upload the file. I am trying to resize and upload the image along with other fields such as the name and bio, than be able to download the image. I know I will have to add an Insert querry after this class to insert the other fields and file name. Also apart of the CMS will be the ability to edit and delete the image along with the other fields. Any suggestions on how I go about 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>";
              }
      }
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.