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

如何获取不同格式的图像

如何获取不同格式的图像

PHP
临摹微笑 2022-01-02 19:50:10
我有这个来获取用户图像,它是为 jpg 硬编码的,但有些用户上传了 png,有些用户上传了 gif,我如何使它适用于所有格式? function get_avatar($image, $user_id, $account)  {   $imgurl =$_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["HTTP_HOST"] . "/files/pictures/picture-" . ($user_id) . ".jpg";   if (!is_imgurl_good($imgurl)) {     $imgurl =$_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["HTTP_HOST"] . "/sites/all/themes/simple_custom/user.png";   }   return $imgurl; }function is_imgurl_good($imgurl) {if (@getimagesize($imgurl))  return true;//Check that if this returns false the previous function works  //return false; //Comment out the first line and uncomment this one to show the reverse case.  }
查看完整描述

2 回答

?
森林海

TA贡献2011条经验 获得超2个赞

是的,你可以限制


<?php

$target_dir = "uploads/";

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

$uploadOk = 1;

$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image

if(isset($_POST["submit"])) {

    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);

    if($check !== false) {

        echo "File is an image - " . $check["mime"] . ".";

        $uploadOk = 1;

    } else {

        echo "File is not an image.";

        $uploadOk = 0;

    }

}

// Check if file already exists

if (file_exists($target_file)) {

    echo "Sorry, file already exists.";

    $uploadOk = 0;

}

// Check file size

if ($_FILES["fileToUpload"]["size"] > 500000) {

    echo "Sorry, your file is too large.";

    $uploadOk = 0;

}

// Allow certain file formats

if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"

&& $imageFileType != "gif" ) {

    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";

    $uploadOk = 0;

}

// Check if $uploadOk is set to 0 by an error

if ($uploadOk == 0) {

    echo "Sorry, your file was not uploaded.";

// if everything is ok, try to upload file

} else {

    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {

        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";

    } else {

        echo "Sorry, there was an error uploading your file.";

    }

}

?>

你应该像这样给出图像类型


if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"

    && $imageFileType != "gif" ) {

        echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";

        $uploadOk = 0;

    }


查看完整回答
反对 回复 2022-01-02
?
慕桂英546537

TA贡献1848条经验 获得超10个赞

你只是喜欢这个例子


$sql = "SELECT picture FROM table_name where id=1";

$result = $conn->query($sql);


if ($result->num_rows == 1) {

    // output data of each row

    $row = $result->fetch_assoc();

   $user_picture=$row["picture"];



 $imgurl =$_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["HTTP_HOST"] . "/files/pictures/".$user_picture."";


查看完整回答
反对 回复 2022-01-02
  • 2 回答
  • 0 关注
  • 135 浏览

添加回答

举报

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