Inglonias
Inglonias

Reputation: 532

PHP - Image does not display on first page load, but does on second page load

I'm writing a PHP-based image viewer, and I've come across an odd issue. When I click an image to view it, the code says that it should show a resized version of the picture, which it has created and saved to the hard drive (so it doesn't need to remake the image more than once). However, it just shows a broken image and outputs an error complaining that the file it's looking for doesn't exist. If I refresh the page, the file does exist, and loads fine. What is going on here? I was under the impression that PHP was single-threaded

Below is the entirety of image-view.php (I have since deleted this code, as I made it for work, and it's not relevant to the problem I had)

Here is the resize_image() function from functions.php

//Return the path to a new or already existing resized version of the image you are looking for.
//I repeat: THIS FUNCTION RETURNS A PATH. IT DOES NOT RETURN AN ACTUAL IMAGE.
function resize_image($img, $imgPath, $width) {
    //error_log("!!!Calling resize_image({$img},{$imgName},{$width},{$imgDir})!!!");
    //Put the image in the .cache subdirectory.
    $imgName = end(explode("/",$imgPath));
    $imgDir = str_replace($imgName,"",$imgPath);
    $pathToThumbs = $imgDir . "/.cache/";
    $thumbWidth = $width;
    $thumbHeight = floor(imagesy($img) * ($width / imagesx($img)));
    $thumbName = get_resized_image_name($thumbWidth, $thumbHeight, $imgName);
    $resizedPath = "{$pathToThumbs}{$thumbName}";
    //Don't make a new image if it already exists.
    if (file_exists($pathToThumbs)) {
        if (file_exists($resizedPath)) {
            error_log("Resized image {$resizedPath} already exists. Exiting.");
            return $resizedPath;
        }
    }
    else {
        error_log("Cache folder does not exist. Creating.");
        $old_umask = umask(0);
        mkdir($pathToThumbs, 0777);
        umask($old_umask);
    }
    error_log("Resized image {$resizedPath} does not exist. Creating.");

    $thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
    //This is the magic line right here. Create the thumbnail.
    imagecopyresized($thumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, imagesx($img), imagesy($img));
    if (!file_exists($pathToThumbs)) {

    }
    imagepng($thumb, "{$pathToThumbs}{$thumbWidth}x{$thumbHeight}-{$imgName}");
    return $thumb;
}

Upvotes: 0

Views: 83

Answers (3)

Kyborek
Kyborek

Reputation: 1511

The reason why it does not work is because at the first call you are returning $thumb which is returned by imagecreatetruecolor() call.

The problem is it returns an image identifier, not file name. Check the documentation

You can easily fix your code by changing the last line

//return $thumb;
return $resizedPath;

Upvotes: 1

pierotofy
pierotofy

Reputation: 43

Change:

imagepng($thumb, "{$pathToThumbs}{$thumbWidth}x{$thumbHeight}-{$imgName}");
return $thumb;

To:

$outPath = "{$pathToThumbs}{$thumbWidth}x{$thumbHeight}-{$imgName}";
imagepng($thumb, $outPath);
return $outPath;

Upvotes: 2

Jite
Jite

Reputation: 5847

On your first run, you return the resource that you pass in to the imagepng function. On second run, you return the path.

imagepng($thumb, "{$pathToThumbs}{$thumbWidth}x{$thumbHeight}-{$imgName}");
// $thumb is a resource.
return $thumb;

Make the function return the path in both cases.

Upvotes: 1

Related Questions