如果我这样做,我无法显示缩略图:
echo "<img width='300'height='300' src='images/".$row['image']."' alt='Profile Picture'>";
显示图片:
但是当我这样做时它不起作用
<?php
require_once('includes/config.inc.php');
require_once('includes/functions.inc.php');
include("includes/html_codes.php");
session_start();
$username = $_SESSION["username"];
if ($_SESSION['logged_in'] == false) {
// If user is already logged in, redirect to main page
redirect('lo.php');
}
?>
<?php
if(isset($_POST['submit'])){
$temporary_name=$_FILES['file']['tmp_name'];
move_uploaded_file($_FILES['file']['tmp_name'],"images/".$_FILES['file']['name']);
$mysqli = new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$q = mysqli_query($mysqli,"UPDATE users SET image = '".$_FILES['file']['name']."'WHERE username = '".$_SESSION['username']."'");
}
?>
<!DOCTYPE html>
<html>
<head>
<head>
<title>Profile</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/main.css"/>
<link rel="stylesheet" href="css/form.css"/>
<link rel="stylesheet" href="css/register.css"/>
<body>
<header>
<?php topBarl(); ?>
</header>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="submit">
</form>
<?php
$mysqli = new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$q = mysqli_query($mysqli,"SELECT * FROM users WHERE username = '$username'");
$row = mysqli_fetch_assoc($q);
$width= 275;
$height= 275;
$orig_image = imagecreatefromjpeg("'images/".$row['image']."'");
if (imagesx($orig_image) > imagesy($orig_image)) {
$y = 0;
$x = (imagesx($orig_image) - imagesy($orig_image)) / 2;
$smallestSide = imagesy($orig_image);
}
else {
$x = 0;
$y = (imagesy($orig_image) - imagesx($orig_image)) / 2;
$smallestSide = imagesx($orig_image);
}
$image_p = imagecreatetruecolor($width, $height);
Imagecopyresampled($image_p,$orig_image,0,0,$x,$y,$width,$height,$smallestSide,$smallestSide);
?>
它给了我这些错误帮助!
警告:imagecreatefromjpeg('images / IMAG0342.jpg'):无法打开流:第60行的C:\ xampp \ htdocs \ newt.php中没有此类文件或目录
警告:imagesx()期望参数1为资源,第61行的C:\ xampp \ htdocs \ newt.php中给出布尔值
警告:imagesy()要求参数1为资源,第61行的C:\ xampp \ htdocs \ newt.php中给出布尔值
警告:imagesy()要求参数1为资源,第68行的C:\ xampp \ htdocs \ newt.php中给出布尔值
警告:imagesx()要求参数1为资源,第68行的C:\ xampp \ htdocs \ newt.php中给出布尔值
警告:imagesx()期望参数1为资源,第69行的C:\ xampp \ htdocs \ newt.php中给出布尔值
警告:imagecopyresampled()要求参数2为资源,第77行的C:\ xampp \ htdocs \ newt.php中给出布尔值
答案 0 :(得分:3)
这一行错了:
$orig_image = imagecreatefromjpeg("'images/".$row['image']."'");
你在你的图像周围加上单引号,所以php实际上是在寻找目录'images
和你的文件名,最后是引号。
应该是:
$orig_image = imagecreatefromjpeg("images/".$row['image']);
由于图像未成功创建,因此需要图像的所有行都会发出警告。