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

  • Home
  • SEARCH
  • 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 7599725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:43:58+00:00 2026-05-30T22:43:58+00:00

What I’m trying to accomplish with this code is, first it scans a directory

  • 0

What I’m trying to accomplish with this code is, first it scans a directory after images, then it takes one image and create a thumbnail out of it if its bigger than 170×170. The problem I’m having is when I try to load the new file it doesn’t work quite as expected.

<?php
foreach (glob('img/*.png') as $f) {
    $list[filemtime($f) . '-' . $f] = $f;
}   

$keys = array_keys($list);      
sort($keys);

$picOne = $list[array_pop($keys)];
$picTwo = $list[array_pop($keys)];
$picThree = $list[array_pop($keys)];
$picFour = $list[array_pop($keys)];

list($width, $height) = getimagesize($picOne);
if ($width > 170 && $height > 170) {
    $thumb = imagecreatetruecolor(170, 170);
    $source = imagecreatefrompng($picOne);
    imagecopyresized($thumb, $source, 0, 0, 0, 0, 170, 170, $width, $height);
    $path = $picOne."_170x170.png";
    imagepng($thumb, $path);
}

echo '<a href="'.$picOne.'" /><img width=170 height=170 src="'.$picOne.'" /></a>&nbsp;&nbsp;'; 
echo '<a href="'.$picTwo.'" /><img width=170 height=170 src="'.$picTwo.'" /></a>&nbsp;&nbsp;'; 
echo '<a href="'.$picThree.'" /><img width=170 height=170 src="'.$picThree.'" /></a>&nbsp;&nbsp;';  
echo '<a href="'.$picFour.'" /><img width=170 height=170 src="'.$picFour.'" /></a>';   

?>

This code should make a new file, right?

$path = $picOne."_170x170.png";
imagepng($thumb, $path)

That part works fine sure but $picOne gets _170x170.png added to it and I don’t understand why. What I think I’ve done is just to create a new file with the $picOne name + _170x170.png but instead $picOne gets it as well.

Can anyone help me fix this code or explain what I need to do to get it to work?

Here’s what I’m trying to do basically:
1. Scan a directory.
2. Take an image and create a thumbnail from it.
3. Output the thumbnail.

  • 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-05-30T22:43:59+00:00Added an answer on May 30, 2026 at 10:43 pm

    The reason you are having issue is that you are not creating new file but overwriting existing file. To create new file every time you try using this loop :

        $path =  $picOne."_170x170.png";
    
        $x = 0 ;
        while (file_exists($path))
        {
            $path = $picOne ."-" . $x . ."_170x170.png"; 
            $x++;
        }
    

    This would always give you a new file name all the time and you can put it in a simple function.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm trying to create an if statement in PHP that prevents a single post
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.