image upload and resize question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DH

    image upload and resize question

    I have a "file upload form" that works OK, but I have been unsuccessful
    in my attempt to also resize the uploaded .JPG (if it is too wide),
    over-writing the original .JPG, and then create and save a thumbnail.jpg
    .... all at the same time. Links to a working example would be
    appreciated. Thanks.
  • googlegroups@paul13.com

    #2
    Re: image upload and resize question

    could you show the html form, and the php code that deals with the file
    to save it and tries to resize it?


    DH wrote:[color=blue]
    > I have a "file upload form" that works OK, but I have been[/color]
    unsuccessful[color=blue]
    > in my attempt to also resize the uploaded .JPG (if it is too wide),
    > over-writing the original .JPG, and then create and save a[/color]
    thumbnail.jpg[color=blue]
    > ... all at the same time. Links to a working example would be
    > appreciated. Thanks.[/color]

    Comment

    • Geoff Berrow

      #3
      Re: image upload and resize question

      I noticed that Message-ID: <7cGdnVcyXcwmHJ TfRVn-vQ@comcast.com> from DH
      contained the following:
      [color=blue]
      >I have a "file upload form" that works OK, but I have been unsuccessful
      >in my attempt to also resize the uploaded .JPG (if it is too wide),
      >over-writing the original .JPG, and then create and save a thumbnail.jpg
      >... all at the same time. Links to a working example would be
      >appreciated.[/color]

      I've got some code that needs tidying up, but you are welcome to have a
      look.



      $thumbpath = "images/thumb/";
      $mainpath="imag es/main/";
      $max_size = 1000000;

      if (!isset($_FILES['userfile'])) exit;


      if (is_uploaded_fi le($_FILES['userfile']['tmp_name'])) {

      if ($_FILES['userfile']['size']>$max_size)
      { echo "The file is too big<br>\n"; exit; }

      if($_FILES['userfile']['type']=="image/gif"){
      $createfunction ="imagecreatefr omgif";}
      else{$createfun ction="imagecre atefromjpeg";}

      if (($_FILES['userfile']['type']=="image/gif") ||
      ($_FILES['userfile']['type'])=="image/pjpeg" ||
      ($_FILES['userfile']['type']=="image/jpeg")) {

      //check if file already uploaded
      //if (file_exists($t humbpath ."s_". $_FILES['userfile']['name'])) { echo
      "The file already exists<br>\n"; exit; }

      //make a copy of original file
      //$res = copy($_FILES['userfile']['tmp_name'], $path .
      //$_FILES['userfile']['name']);

      //create thumbnail image
      $src_img = $createfunction ($_FILES['userfile']['tmp_name']);
      $new_w = 120;
      $new_h = imagesy($src_im g)/(imagesx($src_i mg)/$new_w);
      $dst_img = imagecreatetrue color($new_w,$n ew_h);
      imagecopyresamp led($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h,
      imagesx($src_im g), imagesy($src_im g));
      imagejpeg($dst_ img, $thumbpath ."s_".
      $_FILES['userfile']['name'], 100);

      //create big image
      if(imagesx($src _img)>450){
      $new_w = 450;
      $new_h = imagesy($src_im g)/(imagesx($src_i mg)/$new_w);
      $dst_img = imagecreatetrue color($new_w,$n ew_h);
      $res=imagecopyr esampled($dst_i mg, $src_img, 0, 0, 0, 0, $new_w,
      $new_h, imagesx($src_im g), imagesy($src_im g));
      imagejpeg($dst_ img, $path ."b_".
      $_FILES['userfile']['name'], 50);
      $notify = "<span class=\"indent1 0bold\">Main image resampled</span>";
      }
      else{
      $res = copy($_FILES['userfile']['tmp_name'], $mainpath
      .."b_".$_FILE S['userfile']['name']);
      $notify = "<span class=\"indent1 0bold\">Main image not
      resampled</span>";
      }

      if (!$res) { echo "<span class=\"indent1 0bold\">Upload
      failed!</span><br>\n"; exit; } else { echo "<span
      class=\"indent1 0bold\">Upload sucessful!</span><br><br>\n ";

      }

      $filename=$_FIL ES['userfile']['name'];
      printf("<span class=\"indent1 0bold\">Main :
      </span>%sb_%s<br> \n",$path,$file name);
      printf("<span class=\"indent1 0bold\">Thumbna il :
      </span>%ss_%s<br> \n",$thumbpath, $filename);
      //echo "<span class=\"indent1 0bold\">Main File Name:
      </span>".$_FILES['userfile']['name']."<br>\n";
      echo "<span class=\"indent1 0bold\">Origina l File Size: </span>
      ".$_FILES['userfile']['size']." bytes<br>\n";
      echo "<span class=\"indent1 0bold\">File Type:
      </span>".$_FILES['userfile']['type']."<br>\n";
      echo "$notify.<br>\n ";
      } else { echo "<span class=\"indent1 0bold\">Please use a .jpg or a .gif file</span><br>\n"; exit; }

      //printf("<img src=\"%s%s\">", $path,$filename );
      //printf("<img src=\"%sb_%s\"a lt=\"%s\">",$pa th,$filename,$m ainurl_alt);
      printf("<span class=\"indent1 0bold\">Image Thumbnail:
      </span><br><br><s pan class=\"indent1 0bold\"><img
      src=\"%ss_%s\"a lt=\"%s\"></span><br>",$thu mbpath,$filenam e,$thumburl_alt );
      imagedestroy($s rc_img);
      imagedestroy($d st_img);
      }
      //end of image upload

      --
      Geoff Berrow (put thecat out to email)
      It's only Usenet, no one dies.
      My opinions, not the committee's, mine.
      Simple RFDs http://www.ckdog.co.uk/rfdmaker/

      Comment

      • Geoff Berrow

        #4
        Re: image upload and resize question

        I noticed that Message-ID: <30fj01tc9o4anl jstjhlnn8sr914b at8vu@4ax.com>
        from Geoff Berrow contained the following:
        [color=blue]
        >I've got some code that needs tidying up, but you are welcome to have a
        >look.[/color]

        For instance s/$path/$mainpath

        --
        Geoff Berrow (put thecat out to email)
        It's only Usenet, no one dies.
        My opinions, not the committee's, mine.
        Simple RFDs http://www.ckdog.co.uk/rfdmaker/

        Comment

        • DH

          #5
          Re: image upload and resize question

          Geoff Berrow wrote:[color=blue]
          > I noticed that Message-ID: <7cGdnVcyXcwmHJ TfRVn-vQ@comcast.com> from DH
          > contained the following:
          >
          >[color=green]
          >>I have a "file upload form" that works OK, but I have been unsuccessful
          >>in my attempt to also resize the uploaded .JPG (if it is too wide),
          >>over-writing the original .JPG, and then create and save a thumbnail.jpg
          >>... all at the same time. Links to a working example would be
          >>appreciated .[/color]
          >
          >
          > I've got some code that needs tidying up, but you are welcome to have a
          > look.
          >
          >
          >
          > $thumbpath = "images/thumb/";
          > $mainpath="imag es/main/";
          > $max_size = 1000000;
          >
          > if (!isset($_FILES['userfile'])) exit;
          >
          >
          > if (is_uploaded_fi le($_FILES['userfile']['tmp_name'])) {
          >
          > if ($_FILES['userfile']['size']>$max_size)
          > { echo "The file is too big<br>\n"; exit; }
          >
          > if($_FILES['userfile']['type']=="image/gif"){
          > $createfunction ="imagecreatefr omgif";}
          > else{$createfun ction="imagecre atefromjpeg";}
          >
          > if (($_FILES['userfile']['type']=="image/gif") ||
          > ($_FILES['userfile']['type'])=="image/pjpeg" ||
          > ($_FILES['userfile']['type']=="image/jpeg")) {
          >
          > //check if file already uploaded
          > //if (file_exists($t humbpath ."s_". $_FILES['userfile']['name'])) { echo
          > "The file already exists<br>\n"; exit; }
          >
          > //make a copy of original file
          > //$res = copy($_FILES['userfile']['tmp_name'], $path .
          > //$_FILES['userfile']['name']);
          >
          > //create thumbnail image
          > $src_img = $createfunction ($_FILES['userfile']['tmp_name']);
          > $new_w = 120;
          > $new_h = imagesy($src_im g)/(imagesx($src_i mg)/$new_w);
          > $dst_img = imagecreatetrue color($new_w,$n ew_h);
          > imagecopyresamp led($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h,
          > imagesx($src_im g), imagesy($src_im g));
          > imagejpeg($dst_ img, $thumbpath ."s_".
          > $_FILES['userfile']['name'], 100);
          >
          > //create big image
          > if(imagesx($src _img)>450){
          > $new_w = 450;
          > $new_h = imagesy($src_im g)/(imagesx($src_i mg)/$new_w);
          > $dst_img = imagecreatetrue color($new_w,$n ew_h);
          > $res=imagecopyr esampled($dst_i mg, $src_img, 0, 0, 0, 0, $new_w,
          > $new_h, imagesx($src_im g), imagesy($src_im g));
          > imagejpeg($dst_ img, $path ."b_".
          > $_FILES['userfile']['name'], 50);
          > $notify = "<span class=\"indent1 0bold\">Main image resampled</span>";
          > }
          > else{
          > $res = copy($_FILES['userfile']['tmp_name'], $mainpath
          > .."b_".$_FILE S['userfile']['name']);
          > $notify = "<span class=\"indent1 0bold\">Main image not
          > resampled</span>";
          > }
          >
          > if (!$res) { echo "<span class=\"indent1 0bold\">Upload
          > failed!</span><br>\n"; exit; } else { echo "<span
          > class=\"indent1 0bold\">Upload sucessful!</span><br><br>\n ";
          >
          > }
          >
          > $filename=$_FIL ES['userfile']['name'];
          > printf("<span class=\"indent1 0bold\">Main :
          > </span>%sb_%s<br> \n",$path,$file name);
          > printf("<span class=\"indent1 0bold\">Thumbna il :
          > </span>%ss_%s<br> \n",$thumbpath, $filename);
          > //echo "<span class=\"indent1 0bold\">Main File Name:
          > </span>".$_FILES['userfile']['name']."<br>\n";
          > echo "<span class=\"indent1 0bold\">Origina l File Size: </span>
          > ".$_FILES['userfile']['size']." bytes<br>\n";
          > echo "<span class=\"indent1 0bold\">File Type:
          > </span>".$_FILES['userfile']['type']."<br>\n";
          > echo "$notify.<br>\n ";
          > } else { echo "<span class=\"indent1 0bold\">Please use a .jpg or a .gif file</span><br>\n"; exit; }
          >
          > //printf("<img src=\"%s%s\">", $path,$filename );
          > //printf("<img src=\"%sb_%s\"a lt=\"%s\">",$pa th,$filename,$m ainurl_alt);
          > printf("<span class=\"indent1 0bold\">Image Thumbnail:
          > </span><br><br><s pan class=\"indent1 0bold\"><img
          > src=\"%ss_%s\"a lt=\"%s\"></span><br>",$thu mbpath,$filenam e,$thumburl_alt );
          > imagedestroy($s rc_img);
          > imagedestroy($d st_img);
          > }
          > //end of image upload
          >[/color]


          The code that I've come up with now, by combining an upload script from

          along with Geoff Berrow's input is rather lengthy and is posted here:


          The script needs additional work, in terms of deleting the original
          image after sucessfully creating a properly sized image and thumbnail.
          Also exploding the filename on '.' and appending _new and _thumb
          (instead of prepending 'new_' and 'thumb_').



          Comment

          Working...