dongzhi4239 2016-05-17 11:12
浏览 40

如何创建缩略图

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条回答 默认 最新

  • dongmiao4733 2016-05-17 11:20
    关注

    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);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)