为了账号安全,请及时绑定邮箱和手机立即绑定

从上传的图像创建缩略图

从上传的图像创建缩略图

PHP
扬帆大鱼 2019-11-12 13:14:21
我想从用户上传的图像创建缩略图,以使图像看起来不被挤压。但也想要原始图像的副本。因此,我希望原始图像将原始图像发送到我的服务器,并创建一个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.phpif(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>";            }        }    }将不胜感激任何帮助或指导。谢谢
查看完整描述

3 回答

?
慕码人8056858

TA贡献1803条经验 获得超6个赞

我猜您已经知道了这一点。但是我看到您将图像存储为“ longblobs”,这使我认为您正在存储图片的整个二进制内容。

我希望您已经意识到,仅将文件名存储在数据库中,然后使用该信息将图片从“上传”文件夹或类似文件中抓取,将更加有意义。

提示-不要保存文件路径..仅保存文件名..根据需要在代码中添加路径信息。这样一来,您便拥有了最大的自由度。如果需要更改文件夹结构,则可以在代码中完成,而无需更改数据库记录。


查看完整回答
反对 回复 2019-11-12
?
青春有我

TA贡献1784条经验 获得超8个赞

您可以使用最简单的方法


<?php

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);

}


$src="1494684586337H.jpg";

$dest="new.jpg";

$desired_width="200";

make_thumb($src, $dest, $desired_width);

?>


查看完整回答
反对 回复 2019-11-12
  • 3 回答
  • 0 关注
  • 561 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信