从上传的图像创建缩略图

我想从用户上传的图像创建缩略图,以使图像看起来不被挤压。但也想要原始图像的副本。因此,我希望原始图像将原始图像发送到我的服务器,并创建一个thumb版本并将其发送到我的服务器,以便我可以为每个上传自己的图片。


我的用户表有2个表


`user_pic` longblob NOT NULL,

`user_pic_small` longblob NOT NULL,

我对编码的图像方面并不感到热衷,但这是到目前为止。


Imageupload.php


> <form id="myForm" action="include/media.profileimage.upload.php"

> method="POST" enctype="multipart/form-data" target="ifr1">

>                   <input type = "file" name = "image_data" class = "input_text" style="width:800px;" >    

>                   <input type = "submit" name = "submit"   class = "btn_login" value = "Upload">

>         </form>

media.profileimage.upload.php


if(isset($_FILES['image_data'])){

       if(is_uploaded_file($_FILES['image_data']['tmp_name'])) {


            // prepare the image for insertion

                $imgData =addslashes (file_get_contents($_FILES['image_data']['tmp_name']));


            // get the image info..

              $size = getimagesize($_FILES['image_data']['tmp_name']);



            // our sql query

            $creator_id     =   $_SESSION['id'];

            $sql = "UPDATE users SET user_pic='".$imgData."' WHERE id=$creator_id";

            $sql2 = "INSERT INTO userphotos(photo_ownerid,photo_ispublic, photo_name, photo_caption, photo_imagedata) VALUES ($creator_id,1,'Profile Picture','Profile Picture','$imgData')";


            // insert the image

            if(!mysql_query($sql)) {

                echo "Fail. It broke.";

            }else{

            $c=mysql_query($sql2);


                echo "<script> parent.alert('Image Uploaded','',1000);</script>";

            }

        }

    }

将不胜感激任何帮助或指导。谢谢


扬帆大鱼
浏览 569回答 3
3回答

慕码人8056858

我猜您已经知道了这一点。但是我看到您将图像存储为“ longblobs”,这使我认为您正在存储图片的整个二进制内容。我希望您已经意识到,仅将文件名存储在数据库中,然后使用该信息将图片从“上传”文件夹或类似文件中抓取,将更加有意义。提示-不要保存文件路径..仅保存文件名..根据需要在代码中添加路径信息。这样一来,您便拥有了最大的自由度。如果需要更改文件夹结构,则可以在代码中完成,而无需更改数据库记录。

青春有我

您可以使用最简单的方法<?phpfunction make_thumb($src, $dest, $desired_width) {&nbsp; &nbsp; /* read the source image */&nbsp; &nbsp; $source_image = imagecreatefromjpeg($src);&nbsp; &nbsp; $width = imagesx($source_image);&nbsp; &nbsp; $height = imagesy($source_image);&nbsp; &nbsp; /* find the "desired height" of this thumbnail, relative to the desired width&nbsp; */&nbsp; &nbsp; $desired_height = floor($height * ($desired_width / $width));&nbsp; &nbsp; /* create a new, "virtual" image */&nbsp; &nbsp; $virtual_image = imagecreatetruecolor($desired_width, $desired_height);&nbsp; &nbsp; /* copy source image at a resized size */&nbsp; &nbsp; imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);&nbsp; &nbsp; /* create the physical thumbnail image to its destination */&nbsp; &nbsp; imagejpeg($virtual_image, $dest);}$src="1494684586337H.jpg";$dest="new.jpg";$desired_width="200";make_thumb($src, $dest, $desired_width);?>
打开App,查看更多内容
随时随地看视频慕课网APP