Not seeing images

mallett76

Member
Prior, when I ran the below code, I was erroring out. Upon further trouble shooting, I discovered that I was erroring out, because the path to the image was wrong. I updated the path to the image, and I am no longer erroring out. It is running. The problem now is, is that when I run the code, I am not seeing the images.
Any suggestions would be appreciated. I am running it on xampp - my end goal is to view thumb images
Code
Beginning of example.php code, which is called first
PHP:
<?php
// example.php
#require_once $_SERVER['DOCUMENT_ROOT'].'/common/configs/config_templates.inc.php';
require "thumbimage.class.php";
/* $objThumbImage = new ThumbImage("/web/uploads/orig.jpg");
$objThumbImage->createThumb("/web/uploads/thumb.jpg", 125); */
$objThumbImage = new ThumbImage("web/uploads/orig.jpeg"); // IMPORTANT - THIS LINE WORKS FINE
$objThumbImage->createThumb("web/uploads/thumb.jpeg", 125);
?>

End of example.php code, which is called first

Beginning of thumbimage.class.php
PHP:
<?php
// thumbimage.class.php
class ThumbImage
{
    private $source;
    public function __construct($sourceImagePath)
    {
        $this->source = $sourceImagePath;
    }
    public function createThumb($destImagePath, $thumbWidth=100)
    {
        $sourceImage = imagecreatefromjpeg($this->source);
        $orgWidth = imagesx($sourceImage);
        $orgHeight = imagesy($sourceImage);
        $thumbHeight = floor($orgHeight * ($thumbWidth / $orgWidth));
        $destImage = imagecreatetruecolor($thumbWidth, $thumbHeight);
        imagecopyresampled($destImage, $sourceImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $orgWidth, $orgHeight);
        imagejpeg($destImage, $destImagePath);
        imagedestroy($sourceImage);
        imagedestroy($destImage);
    }
}
?>
End of thumbimage.class.php
 
Last edited by a moderator:
  1. File Paths: Ensure that the paths to the source and destination images are correct. In your example, you're using relative paths (web/uploads/orig.jpeg and web/uploads/thumb.jpeg). Double-check that these paths are correct relative to the location of your PHP files.

PHP:
<?php
// example.php
require "thumbimage.class.php";

try {
    $objThumbImage = new ThumbImage("web/uploads/orig.jpeg");
    $objThumbImage->createThumb("web/uploads/thumb.jpeg", 125);
    echo "Thumbnail created successfully!";
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}

?>

<?php
// thumbimage.class.php
class ThumbImage
{
    private $source;
    
    public function __construct($sourceImagePath)
    {
        $this->source = $sourceImagePath;
    }
    
    public function createThumb($destImagePath, $thumbWidth = 100)
    {
        $sourceImage = imagecreatefromjpeg($this->source);
        if (!$sourceImage) {
            throw new Exception("Failed to create source image from: " . $this->source);
        }
        
        $orgWidth = imagesx($sourceImage);
        $orgHeight = imagesy($sourceImage);
        $thumbHeight = floor($orgHeight * ($thumbWidth / $orgWidth));
        $destImage = imagecreatetruecolor($thumbWidth, $thumbHeight);
        if (!$destImage) {
            throw new Exception("Failed to create destination image.");
        }
        
        if (!imagecopyresampled($destImage, $sourceImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $orgWidth, $orgHeight)) {
            throw new Exception("Failed to copy and resize image.");
        }
        
        if (!imagejpeg($destImage, $destImagePath)) {
            throw new Exception("Failed to save thumbnail image to: " . $destImagePath);
        }
        
        imagedestroy($sourceImage);
        imagedestroy($destImage);
    }
}
?>

i hope This code includes error handling using try-catch blocks to catch any exceptions that might occur during image processing. It will provide you with more detailed error messages to help diagnose the issue.

Best Regard
Danish hafeez | QA Assistant
ICTInnovations
 
Hi,
So I ran the following code:
PHP:
<?php
// example.php 
#require_once $_SERVER['DOCUMENT_ROOT'].'/common/configs/config_templates.inc.php';
//////require "thumbimage.class.php";
/* $objThumbImage = new ThumbImage("/web/uploads/orig.jpg");
$objThumbImage->createThumb("/web/uploads/thumb.jpg", 125); */
/////$objThumbImage = new ThumbImage("web/uploads/orig.jpeg"); // IMPORTANT - THIS LINE WORKS FINE
/////$objThumbImage->createThumb("web/uploads/thumb.jpeg", 125);

require "thumbimage.class.php";

try 
{
    $objThumbImage = new ThumbImage("web/uploads/orig.jpeg");
    $objThumbImage->createThumb("web/uploads/thumb.jpeg", 125);
    echo "Thumbnail created successfully!";
} 
catch (Exception $e) 
{
    echo "Error: " . $e->getMessage();
}

?>

PHP:
<?php
// thumbimage.class.php 
/* class ThumbImage
{
    private $source;
    public function __construct($sourceImagePath)
    {
        $this->source = $sourceImagePath;
    }

    public function createThumb($destImagePath, $thumbWidth=100)
    {
        $sourceImage = imagecreatefromjpeg($this->source);
        $orgWidth = imagesx($sourceImage);
        $orgHeight = imagesy($sourceImage);
        $thumbHeight = floor($orgHeight * ($thumbWidth / $orgWidth));
        $destImage = imagecreatetruecolor($thumbWidth, $thumbHeight);
        imagecopyresampled($destImage, $sourceImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $orgWidth, $orgHeight);
        imagejpeg($destImage, $destImagePath);
        imagedestroy($sourceImage);
        imagedestroy($destImage);
    }
} */

// thumbimage.class.php
class ThumbImage
{
    private $source;
    
    public function __construct($sourceImagePath)
    {
        $this->source = $sourceImagePath;
    }
    
    public function createThumb($destImagePath, $thumbWidth = 100)
    {
        $sourceImage = imagecreatefromjpeg($this->source);
        if (!$sourceImage) 
        {
            throw new Exception("Failed to create source image from: " . $this->source);
        }
        
        $orgWidth = imagesx($sourceImage);
        $orgHeight = imagesy($sourceImage);
        $thumbHeight = floor($orgHeight * ($thumbWidth / $orgWidth));
        $destImage = imagecreatetruecolor($thumbWidth, $thumbHeight);
        if (!$destImage) 
        {
            throw new Exception("Failed to create destination image.");
        }
        
        if (!imagecopyresampled($destImage, $sourceImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $orgWidth, $orgHeight)) 
        {
            throw new Exception("Failed to copy and resize image.");
        }
        
        if (!imagejpeg($destImage, $destImagePath)) 
        {
            throw new Exception("Failed to save thumbnail image to: " . $destImagePath);
        }
        
        imagedestroy($sourceImage);
        imagedestroy($destImage);
    }
}

?>

And, when it finished running, it did not error, it said "thumbnail successfully created", but I'm not seeing the image. Shouldn't I be seeing an image?
 
Last edited by a moderator:
Here’s a revised version of your script that includes some of these checks:


PHP:
<?php
// Enable error reporting for debugging
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require "thumbimage.class.php";

try
{
    $sourcePath = "web/uploads/orig.jpeg";
    $thumbPath = "web/uploads/thumb.jpeg";
    
    // Check if source file exists
    if (!file_exists($sourcePath)) {
        throw new Exception("Source image does not exist: " . $sourcePath);
    }

    $objThumbImage = new ThumbImage($sourcePath);
    $objThumbImage->createThumb($thumbPath, 125);

    // Check if thumbnail was created successfully
    if (!file_exists($thumbPath)) {
        throw new Exception("Thumbnail was not created at: " . $thumbPath);
    }

    echo "Thumbnail created successfully!";
    echo '<br>Thumbnail path: ' . realpath($thumbPath) . '<br>';
    echo '<img src="'.$thumbPath.'" alt="Thumbnail">';
}
catch (Exception $e)
{
    echo "Error: " . $e->getMessage();
}
?>

This script adds error reporting, checks for the existence of the source image, and displays the thumbnail if it is successfully created. Make sure the paths and file permissions are correct.

Best Regard
danish hafeez | QA Assistant
ICTInnovations
 
I found some code that worked, it was the following code:
PHP:
<?php


// Path to the original image
//commented out on 9/7/24 $sourcePath = "C:/xampp/htdocs/images/original.jpg";
$sourcePath = "C:/xamppNew/htdocs/photo/Images/cal.jpeg";

// Path where the thumbnail will be saved
//commented out on 9/7/24 $thumbnailPath = "C:/xampp/htdocs/images/thumbnail.jpg";
$thumbnailPath = "C:/xamppNew/htdocs/photo/Images/california.jpeg";

// Desired thumbnail dimensions
$thumbnailWidth = 200;  // Width of the thumbnail
$thumbnailHeight = 150; // Height of the thumbnail

// Load the original image
$sourceImage = imagecreatefromjpeg($sourcePath);

if ($sourceImage === false) {
    die("Error: Could not load image.");
}

// Get the original image dimensions
$sourceWidth = imagesx($sourceImage);
$sourceHeight = imagesy($sourceImage);

// Create a blank image for the thumbnail
$thumbnailImage = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight);

// Resize the original image and copy it into the thumbnail
imagecopyresampled($thumbnailImage, $sourceImage, 0, 0, 0, 0, $thumbnailWidth, $thumbnailHeight, $sourceWidth, $sourceHeight);

// Save the thumbnail to a file
if (imagejpeg($thumbnailImage, $thumbnailPath)) {
    echo "Thumbnail created successfully!";
} else {
    echo "Failed to create thumbnail.";
}
//Added on 9/15/24 to display the image
//commented out on 9/29/24 echo "<img src='/photo/Images/california_thumb.jpeg' alt='Thumbnail Image'>";
//commented out on 9/22/24 echo "<img src='/photo/Images/california_thumb.jpeg' alt='Thumbnail Image'>";
//C:\xamppNew\htdocs\photo\Images\california.jpeg
echo "<img src='/photo/Images/california.jpeg' alt='Thumbnail Image'>";

// Clean up
imagedestroy($sourceImage);
imagedestroy($thumbnailImage);

?>
 
Last edited by a moderator:
You should replace the routes:
C:/xamppNew/htdocs/photo/
for this
$_SERVER['DOCUMENT_ROOT'] . '/Images/cal.jpeg'
and do the same for the save path
where Images I imagine is the folder where the images will be
It is good practice to use the $_SERVER['DOCUMENT_ROOT'] for the routes, and you only modify the end
 
  1. Image Path Issues: The paths provided for the source and destination images may not be correct. The code uses relative paths, which can lead to issues if the script is executed from a different directory.
  2. Image Format: The source image is specified as a .jpeg file, but the imagecreatefromjpeg function is used, which only supports .jpg and .jpeg formats. If the file is not a valid JPEG, it will fail.
  3. Error Handling: There is no error handling in the code to check if the image was successfully created or if the paths are valid.
 
Back
Top