how to create thumbnail

时间:2016-05-17 11:12:54

标签: php html

I have a code which saves an image into a folder and the database...I am looking for a program which allows us to upload the image with a fixed size(like a thumbnail)..Also....I need to save the uploaded image into another folder...can anyone please help me with this..I am new at this.

<?php
    include("connection.php");
    $email=$_SESSION['email'];
   if($email==NULL)
   {
     header("location:register.php");
   }
    else
   {
     if($_POST)
     {
         if(isset($_POST["submit"])) 
         {
             $target_dir = "uploads/";
             $thumb_dir="thumb/";
            $target_file = $target_dir . basename($_FILES["image_name"]["name"]);
            $uploadOk = 1;
            $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
            if($imageFileType==NULL)
            {
               header("location:gallery.php?status=c");
               $uploadOk = 0;
            }
            else
            {
                $uploadOk = 1;
            }
          // Allow certain file formats
            if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $imageFileType !=NULL)
            {

                header("Location: gallery.php?status=notimage");
                $uploadOk = 0;
                exit();

            }
        else if($_FILES["image_name"]["tmp_name"] !=NULL)
        {
             $RandomAccountNumber = uniqid();
            $target_new=$target_dir.$RandomAccountNumber.".".$imageFileType;
             $target_name=$RandomAccountNumber.".".$imageFileType;
             if (move_uploaded_file($_FILES["image_name"]["tmp_name"], $target_new)) 
             {
                  echo "The file ". basename( $_FILES["image_name"]["name"]). " has been uploaded.";
             } 
             else 
             {
                echo "Sorry, there was an error uploading your file.";
             }
              $query=mysql_query("insert into photos values('','$target_name','$email')");

             if(!$query)
             {
                header("Location: gallery.php?status=f");   
                exit(); 
             }

             else
             {
                header("Location:gallery.php?status=imageupload");  
                exit(); 
             }
      }
          }

       }
         else
        {
            ?><table  align="center">
            <form action="" method="post" enctype="multipart/form-data">
            <tr>
            <td>Select image to upload:</td>
            <td><div class="choose_file">
            <span>Choose File</span>
            <input type="file" name="image_name" id="image"/>
            </div></td> 
            </tr>
            <tr>
            <td><input type="submit"  value="Upload Image" class="button2" name="submit">
            </td></tr>
            </form>
            </table></div>
            <?php
             }
             ?>

1 个答案:

答案 0 :(得分:0)

You can call this function after the upload

function make_thumb($src, $dest, $desired_width) {

    /* read the source image */
    $source_image = imagecreatefromjpeg($src);
    $width = imagesx($source_image);
    $height = imagesy($source_image);

    /* find the "desired height" of this thumbnail, relative to the desired width  */
    $desired_height = floor($height * ($desired_width / $width));

    /* create a new, "virtual" image */
    $virtual_image = imagecreatetruecolor($desired_width, $desired_height);

    /* copy source image at a resized size */
    imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);

    /* create the physical thumbnail image to its destination */
    imagejpeg($virtual_image, $dest);
}