David
David

Reputation: 409

Uploading image and thumbnail together but the thumbnail is not generating

Im trying to add to my existing code a way to generate a thumbnail from a full size image that's being uploaded. I did some searching around here and found a function to accomplish exactly what i was looking for but my thumbnails aren't being generated. The way I have it set up is that the full size images go to the images/lg/ folder and the thumbnails will go to the images folder. However only the large images are going to their respective folders with the thumbnails not being generated.

<?php 
include_once 'includes/dbh.inc.php';
function make_thumb($src, $dest, $desired_width) {

    /* read the source image */
    $source_image = imagecreatefromjpeg($src);
    $width = imagesx($source_image);
    $height = imagesy($source_image);

    /* find the "desired height" of this thumbnail, relative to the desired width  */
    $desired_height = floor($height * ($desired_width / $width));

    /* create a new, "virtual" image */
    $virtual_image = imagecreatetruecolor($desired_width, $desired_height);

    /* copy source image at a resized size */
    imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);

    /* create the physical thumbnail image to its destination */
    imagejpeg($virtual_image, $dest);
}
if (isset($_GET['id'])){
if (isset($_POST['submit'])) {
    $file = $_FILES['file'];

    $fileName = $_FILES['file']['name'];
    $fileType = $_FILES['file']['type'];
    $fileTmpName = $_FILES['file']['tmp_name'];
    $fileSize = $_FILES['file']['size'];
    $fileError = $_FILES['file']['error'];

    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));

    $allowed = array('jpg', 'jpeg', 'png');
    if (in_array($fileActualExt, $allowed))
        if($fileError === 0){
            if ($fileSize < 5000000) {
                $fileNameNew = "car".$_GET['id'].".".$fileActualExt;
                $fileDestination = 'images/lg/'.$fileNameNew;
                move_uploaded_file($fileTmpName, $fileDestination); //files going to the 'images/lg' folder
                $sql = "UPDATE rollingstock SET stockImgStatus=? WHERE stockID=?";
                $stmt = $mysqli->prepare($sql);
                $stockImgStatus = 1;
                if (file_exists($fileDestination)) { /*if the file exisits make a thumbnail */
                $src = $fileDestination;
                $dest = 'images/'.$fileNameNew;
                $desired_width = "395"; 
                make_thumb($src, $dest, $desired_width); //Thumbnails going to the 'imgages' folder
                }
                if ($stmt &&
                    $stmt -> bind_param('ii', $stockImgStatus, $_GET['id'])&&
                    $stmt -> execute()&&
                    $stmt -> affected_rows === 1
                ) {
                    header("Location: list.php?list=all"); 
                }else{
                    echo "Not Updated";
                }
                
            }else {
                echo "Your file is too big!";
            }
        }else{
            echo "There was an error uploading for file!";
        }
}else{
    echo "You cannot upload files of this type!";
}
}

Upvotes: 1

Views: 434

Answers (2)

shahiq572
shahiq572

Reputation: 141

I checked your code and overall its fine. There is only one minor mistake in the make_thumb() function, where you have used imagecreatefromjpeg() which works for .jpeg and .jpg image types but not for .png images which you also accept.

You can simply add a check in your make_thumb() function to check for file type and call the appropriate method, something like as mentioned in this post.

I am also adding a modified snippet from the linked post:

switch ( strtolower( pathinfo( $src, PATHINFO_EXTENSION ))) {
        case 'jpeg':
        case 'jpg':
            $source_image = imagecreatefromjpeg($src);
        break;

        case 'png':
            $source_image = imagecreatefrompng($src);
        break;

        case 'gif':
            $source_image = imagecreatefromgif($src);
        break;

        default:
            throw new InvalidArgumentException('File "'.$src.'" is not valid jpg, png image.');
        break;
    }

Upvotes: 2

Micky
Micky

Reputation: 107

I'm able to generate both large image and thumbnail with provided code. Please check if your image folder has write permission.

Upvotes: -1

Related Questions