如何获取不同格式的图像

我有这个来获取用户图像,它是为 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.

  

}


临摹微笑
浏览 164回答 2
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 imageif(isset($_POST["submit"])) {&nbsp; &nbsp; $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);&nbsp; &nbsp; if($check !== false) {&nbsp; &nbsp; &nbsp; &nbsp; echo "File is an image - " . $check["mime"] . ".";&nbsp; &nbsp; &nbsp; &nbsp; $uploadOk = 1;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; echo "File is not an image.";&nbsp; &nbsp; &nbsp; &nbsp; $uploadOk = 0;&nbsp; &nbsp; }}// Check if file already existsif (file_exists($target_file)) {&nbsp; &nbsp; echo "Sorry, file already exists.";&nbsp; &nbsp; $uploadOk = 0;}// Check file sizeif ($_FILES["fileToUpload"]["size"] > 500000) {&nbsp; &nbsp; echo "Sorry, your file is too large.";&nbsp; &nbsp; $uploadOk = 0;}// Allow certain file formatsif($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"&& $imageFileType != "gif" ) {&nbsp; &nbsp; echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";&nbsp; &nbsp; $uploadOk = 0;}// Check if $uploadOk is set to 0 by an errorif ($uploadOk == 0) {&nbsp; &nbsp; echo "Sorry, your file was not uploaded.";// if everything is ok, try to upload file} else {&nbsp; &nbsp; if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {&nbsp; &nbsp; &nbsp; &nbsp; echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; echo "Sorry, there was an error uploading your file.";&nbsp; &nbsp; }}?>你应该像这样给出图像类型if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"&nbsp; &nbsp; && $imageFileType != "gif" ) {&nbsp; &nbsp; &nbsp; &nbsp; echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";&nbsp; &nbsp; &nbsp; &nbsp; $uploadOk = 0;&nbsp; &nbsp; }

慕桂英546537

你只是喜欢这个例子$sql = "SELECT picture FROM table_name where id=1";$result = $conn->query($sql);if ($result->num_rows == 1) {&nbsp; &nbsp; // output data of each row&nbsp; &nbsp; $row = $result->fetch_assoc();&nbsp; &nbsp;$user_picture=$row["picture"];&nbsp;$imgurl =$_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["HTTP_HOST"] . "/files/pictures/".$user_picture."";
打开App,查看更多内容
随时随地看视频慕课网APP