Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9229239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:31:22+00:00 2026-06-18T05:31:22+00:00

Idea I have a function that checks to see if a thumbnail exists in

  • 0

Idea
I have a function that checks to see if a thumbnail exists in cache folder for a particular image. If it does, it returns the path to that thumbnail. If it does not, it goes ahead and generates the thumbnail for the image, saves it in the cache folder and returns the path to it instead.

Problem
Let’s say I have 10 images but only 7 of them have their thumbnails in the cache folder. Therefore, the function goes to generation of thumbnails for the rest 3 images. But while it does that, all I see is a blank, white loading page. The idea is to display the thumbnails that are already generated and then generate the ones that do not exist.

Code

$images = array(
        "http://i49.tinypic.com/4t9a9w.jpg",
        "http://i.imgur.com/p2S1n.jpg",
        "http://i49.tinypic.com/l9tow.jpg",
        "http://i45.tinypic.com/10di4q1.jpg",
        "http://i.imgur.com/PnefW.jpg",
        "http://i.imgur.com/EqakI.jpg",
        "http://i46.tinypic.com/102tl09.jpg",
        "http://i47.tinypic.com/2rnx6ic.jpg",
        "http://i50.tinypic.com/2ykc2gn.jpg",
        "http://i50.tinypic.com/2eewr3p.jpg"
    );

function get_name($source) {
    $name = explode("/", $source);
    $name = end($name);
    return $name;
}

function get_thumbnail($image) {
    $image_name = get_name($image);
    if(file_exists("cache/{$image_name}")) {
        return "cache/{$image_name}";
    } else {
        list($width, $height) = getimagesize($image);
        $thumb = imagecreatefromjpeg($image);
        if($width > $height) {
            $y = 0;
            $x = ($width - $height) / 2;
            $smallest_side = $height;
        } else {
            $x = 0;
            $y = ($height - $width) / 2;
            $smallest_side = $width;
        }

        $thumb_size = 200;
        $thumb_image = imagecreatetruecolor($thumb_size, $thumb_size);
        imagecopyresampled($thumb_image, $thumb, 0, 0, $x, $y, $thumb_size, $thumb_size, $smallest_side, $smallest_side);

        imagejpeg($thumb_image, "cache/{$image_name}");

        return "cache/{$image_name}";
    }
}

foreach($images as $image) {
    echo "<img src='" . get_thumbnail($image) . "' />";
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:31:23+00:00Added an answer on June 18, 2026 at 5:31 am

    To elaborate on @DCoder’s comment, what you could do is;

    • If the thumb exists in the cache, return the URL just as you do now. This will make sure that thumbs that are in the cache will load quickly.

    • If the thumb does not exist in the cache, return an URL similar to /cache/generatethumb.php?http://i49.tinypic.com/4t9a9w.jpg where the script generatethumb.php generates the thumbnail, saves it in the cache and returns the thumbnail. Next time, it will be in the cache and the URL won’t go through the PHP script.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have no idea how to properly 'return' this JSON object (in JavaScript): function
I have no idea what's wrong with this code: onupgradeneeded = function(){ z =
The Idea I have a main PHP page that loads content in DIV's from
Let's say I have this code: int function(bool b) { // execution path 1
So, the idea was a write a recursive function that compares two strings to
Suppose I have a function that takes an argument x of dimension 1 or
I have the following loop that simply checks all green rows in a table
I have a function that generates a CRC check byte based on the content
I have a function in R that looks somewhat like this: setMethod('[', signature(x=stack),definition=function(x,i,j,drop){ new('class',
I have no idea why this doesn't work, but doing some validation functions and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.