【问题标题】:can't output the thumbnail for profile page无法输出个人资料页面的缩略图
【发布时间】:2014-05-01 13:33:30
【问题描述】:

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

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

它显示图片:

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

profile.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);
 

?> 

它给了我这些错误帮助!

警告:imagecreatefromjpeg('images/IMAG0342.jpg'):无法打开流:第 60 行的 C:\xampp\htdocs\newt.php 中没有这样的文件或目录

警告:imagesx() 期望参数 1 是资源,布尔值在 C:\xampp\htdocs\newt.php 第 61 行给出

警告: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 中给出的布尔值

【问题讨论】:

    标签: php mysql mysqli


    【解决方案1】:

    这一行是错误的:

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

    您在图像周围加上单引号,因此 php 实际上是在寻找目录 'images 和您的文件名,并在末尾加上引号。

    应该是:

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

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

    【讨论】:

    • 它仍然无法工作它正在显示上传表单但没有显示任何图像
    • @user3518459 你能缩小问题的范围并把它放在一个新的问题中吗?这个(和我的回答)似乎集中在你收到的警告上。
    猜你喜欢
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 2014-10-09
    • 2015-10-08
    • 2011-12-02
    • 2023-03-09
    • 2013-06-07
    • 2011-10-01
    相关资源
    最近更新 更多