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