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
// 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
// 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
 
  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 // 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 // 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?
 
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
 
Back
Top