php - 无法输出个人资料页面的缩略图

标签 php mysql mysqli

如果我这样做,我将无法显示缩略图:

echo "<img width='300'height='300' src='images/".$row['image']."' alt='Profile Picture'>";

显示图片:

但是当我这样做时它不起作用帮助

配置文件.php

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

?> 

它给了我这些错误帮助!

Warning: imagecreatefromjpeg('images/IMAG0342.jpg'): failed to open stream: No such file or directory in C:\xampp\htdocs\newt.php on line 60

Warning: imagesx() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\newt.php on line 61

Warning: imagesy() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\newt.php on line 61

Warning: imagesy() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\newt.php on line 68

Warning: imagesx() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\newt.php on line 68

Warning: imagesx() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\newt.php on line 69

Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in C:\xampp\htdocs\newt.php on line 77

最佳答案

这一行是错误的:

$orig_image = imagecreatefromjpeg("'images/".$row['image']."'");

您在图像周围放置了单引号,因此 php 实际上是在查找目录 'images 和您的文件名,最后加上引号。

它应该只是:

$orig_image = imagecreatefromjpeg("images/".$row['image']);

并且因为图像没有成功创建,所有需要图像的行都会给你一个警告。

关于php - 无法输出个人资料页面的缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23408784/

相关文章:

mysql - 使用 Laravel 在同一行插入数据

mysql - MySQL更新表与用户所在的邮箱

php - 在插入数据 (PHP) 中调用 bool 值的成员函数 bind_param()

MySQL 不能在 WHERE 语句中使用别名?

php - 如何使用 MySQL 查询中的行创建 HTML 表格布局?

php - 如何使用 PHP 从 Mysql 数据库获取结果创建多项选择 Android ListView ?

php - 通过模态和PHP向数据库中插入数据

php 存储过程MySQL的行数

php - Zend 翻译路线

php - 为什么下面的代码打印出 10 而不是 null?