duanjiu1894 2010-12-05 19:01
浏览 42
已采纳

允许用户上传其个人资料的图片

Oddly enough, I can't find any info on this topic: user profile pictures.

Is there a PHP solution for the following?

  • Allows users to upload a pic from their hard drive for storage on our server
  • Shrinks the image if it's too big; expands it if it's too small (height/width)

Is this easy to build yourself? Or is there already a library for it?

  • 写回答

3条回答 默认 最新

  • dsjgk330337 2010-12-05 19:11
    关注

    To upload and store the image, I would recommend generating a random filename for the picture, instead of keeping the original file name. This will prevent conflicts and also add a measure of security. I would suggest a pretty long name; just random numbers and letters.

    Then, store the random file name in the database record along with your user info. This way, you won't have to worry about file names and user names getting out of sync, and, as I said earlier, someone won't be able to guess that Joe Schmoe's profile picture is stored as JoeSchmoe.jpg.

    For when you get to the image-resizing part, use this function below I modified (from the PHP user comments). In your case, "images/smallerpicture.jpeg" would probably be replaced by "images/<some random name here>.jpeg".

    Example:

    scaleImage("images/bigpicture.jpeg", 100, 100, "images/smallerpicture.jpeg");
    

    Function:

    function scaleImage($source, $max_width, $max_height, $destination) {
        list($width, $height) = getimagesize($source);
        if ($width > 150 || $height > 150) {
            $ratioh = $max_height / $height;
            $ratiow = $max_width / $width;
            $ratio = min($ratioh, $ratiow);
            // New dimensions
            $newwidth = intval($ratio * $width);
            $newheight = intval($ratio * $height);
    
            $newImage = imagecreatetruecolor($newwidth, $newheight);
    
            $exts = array("gif", "jpg", "jpeg", "png");
            $pathInfo = pathinfo($source);
            $ext = trim(strtolower($pathInfo["extension"]));
    
            $sourceImage = null;
    
            // Generate source image depending on file type
            switch ($ext) {
            case "jpg":
            case "jpeg":
                $sourceImage = imagecreatefromjpeg($source);
                break;
            case "gif":
                $sourceImage = imagecreatefromgif($source);
                break;
            case "png":
                $sourceImage = imagecreatefrompng($source);
                break;
            }
    
            imagecopyresampled($newImage, $sourceImage, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    
            // Output file depending on type
            switch ($ext) {
            case "jpg":
            case "jpeg":
                imagejpeg($newImage, $destination);
                break;
            case "gif":
                imagegif($newImage, $destination);
                break;
            case "png":
                imagepng($newImage, $destination);
                break;
            }
        }
    }
    

    It supports gifs, jpgs, and pngs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题