PHP + Upload + picture script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jason.tadeo@gmail.com

    #1

    PHP + Upload + picture script

    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

    jason

  • josh.23.french

    #2
    Re: PHP + Upload + picture script

    For the uploading part, here's some basic code:
    <?php
    if(@$_POST['MAX_FILE_SIZE']){ // If it is actually uploading
    something...
    $uploadfile = $uploaddir . basename($_FILE S['userfile']['name']);
    //SET YOUR OWN UPLOAD DIR (absolute)
    if (move_uploaded_ file($_FILES['userfile']['tmp_name'], $uploadfile)){
    echo "File was successfully uploaded!\n You can access the file at:
    http://".ROOT_DOMAIN.$ uploaddir.basen ame($_FILES['userfile']['name'])."\n";
    } else {
    echo "Error! File was not uploaded!\n";
    };
    };
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>?admin=<?php echo
    ADMIN_PAGE; ?>" method="POST" enctype="multip art/form-data">
    <input type="hidden" name="MAX_FILE_ SIZE" value="300000" />
    <input type="file" name="userfile" /><br />
    <input type="submit" value="Upload" />
    </form>

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

    <?php
    function CreateThumb($fi le,$maxwdt,$max hgt, $dest) {
    list($owdt,$ohg t,$otype)=@geti magesize($file) ;

    switch($otype) {
    case 1: $newimg=imagecr eatefromgif($fi le); break;
    case 2: $newimg=imagecr eatefromjpeg($f ile); break;
    case 3: $newimg=imagecr eatefrompng($fi le); break;
    default: echo "Unkown filetype (file $file, typ $otype)"; return;
    }

    if($newimg) {
    if($owdt>1500 || $ohgt>1200)
    list($owdt, $ohgt) = Resample($newim g, $owdt, $ohgt,
    1024,768,0);

    Resample($newim g, $owdt, $ohgt, $maxwdt, $maxhgt);

    if(!$dest) return $newimg;

    if(!is_dir(dirn ame($dest)))
    mkdir(dirname($ dest));

    switch($otype) {
    case 1: imagegif($newim g,dest); break;
    case 2: imagejpeg($newi mg,$dest,90); break;
    case 3: imagepng($newim g,$dest); break;
    }

    imagedestroy($n ewimg);

    chmod($dest,064 4);
    }
    }

    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>=$di vhgt) {
    $newwdt=$maxwdt ;
    $newhgt=round($ ohgt/$divwdt);
    } else {
    $newhgt=$maxhgt ;
    $newwdt=round($ owdt/$divhgt);
    }

    $tn=imagecreate truecolor($neww dt,$newhgt);
    if($quality)

    imagecopyresamp led($tn,$img,0, 0,0,0,$newwdt,$ newhgt,$owdt,$o hgt);

    else
    imagecopyresize d($tn,$img,0,0, 0,0,$newwdt,$ne whgt,$owdt,$ohg t);

    imagedestroy($i mg);

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

    On Oct 30, 5:19 pm, "jason.ta...@gm ail.com" <jason.ta...@gm ail.com>
    wrote:
    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
    >
    jason

    Comment

    • Shelly

      #3
      Re: PHP + Upload + picture script

      <jason.tadeo@gm ail.comwrote in message
      news:1162246760 .533931.252710@ m7g2000cwm.goog legroups.com...
      >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
      >
      jason
      >
      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($i mg);
      $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_wid th, $disp_height);
      }


      I call it with:

      list($disp_widt h, $disp_height) = thumbnail($the_ picture_locatio n, 500,
      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.

      Shelly




      Comment

      • aussiebob

        #4
        Re: PHP + Upload + picture script

        >
        On Oct 30, 5:19 pm, "jason.ta...@gm ail.com" <jason.ta...@gm ail.com>
        wrote:
        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

        jason

        Take a look on the phpclasses.org 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
        away.
        Hope that helps
        b

        Comment

        • Kim André Akerø

          #5
          Re: PHP + Upload + picture script

          jason.tadeo@gma il.com wrote:
          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.
          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ø
          - kimandre@NOSPAM betadome.com
          (remove NOSPAM to contact me directly)

          Comment

          • Arjen

            #6
            Re: PHP + Upload + picture script

            aussiebob schreef:
            >On Oct 30, 5:19 pm, "jason.ta...@gm ail.com" <jason.ta...@gm ail.com>
            >wrote:
            >>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
            >>>
            >>jason
            >
            >
            Take a look on the phpclasses.org 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
            away.
            Hope that helps
            b

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

            Comment

            Working...