Question

I can't display the thumbnail if I do something like this:

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

It displays the picture:

but when I do this it doesn't work help

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

?> 

its gives me these errors help!

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

Was it helpful?

Solution

This line is wrong:

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

You are putting single quotes around your image so php is actually looking for the directory 'images and your file name with a quote at the end.

It should just be:

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

And because the image is not successfully created, all lines after that that need the image give you a warning.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top