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 6092075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:24:03+00:00 2026-05-23T12:24:03+00:00

I am using the GD library with PHP. Basically, I’ve made a design change

  • 0

I am using the GD library with PHP.

Basically, I’ve made a design change and need to resize a whole bunch of images that are a certain width.

ie anything that is 876px wide needs to be 828px.

Is there a way to loop through all JPG files in a directory, check their width dimension, and if they equal X then grab their existing file name, rescale down to the same name?

  • 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-23T12:24:04+00:00Added an answer on May 23, 2026 at 12:24 pm

    Just need to use imagecopyresampled() in a loop.

    $path = "/my/path/to/jpgs";
    $targetWidth = 876;
    $newWidth = 828;
    $imageQuality = 95;
    
    if (is_dir($path)) {
        $dHandle = opendir($path);
        if ($dHandle) {
            while (($cFileName = readdir($dHandle)) !== false) {
                if (!preg_match("/\.jpg$/", $cFileName)) {
                    continue;
                }
    
                $cImagePath = $path . $cFileName;
                list ($cWidth, $cHeight) = getimagesize($cImagePath);
    
                if ($cWidth == $targetWidth) {
                    $cImage = imagecreatefromjpeg($cImagePath);
                    if ($cImage === false) {
                        echo "Error reading: " . $cImagePath . "\n";
                        continue;
                    }
    
                    $cNewHeight = round($cHeight * ($newWidth / $cWidth));
    
                    $cNewImage = imagecreatetruecolor($newWidth, $cNewHeight);
                    imagecopyresampled($cNewImage, $cImage, 0, 0, 0, 0, $newWidth, $cNewHeight, $cWidth, $cHeight);
    
                    if (imagejpeg($cNewImage, $cImagePath, $imageQuality) === false) {
                        echo "Error writing: " . $cImagePath . "\n";
                        continue;
                    }
                }
            }
    
            closedir($dHandle);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm calling a webservice using the NuSoap PHP library . The webservice appears to
I am using a library that has headers without the .h This defeats visual
I was using mysql++ library and compiling with GCC 3.3.4. That GCC version had
In C#, I am using a library that defines an enum. I would like
I am writing some PHP code to create PDFs using the FPDF library. And
I'm currently using a custom made library at my job. Until just rencently the
I'm using PHP to write a using socket library application. How could I get
I'm wrapping a C++ library in PHP using SWIG and there have been some
I'm using cakephp with this vendor: oauth_consumer.php It requires OAuth PHP library oauth.php Inside
I'm using Zend, a PHP library, to work with Amazon EC2. I have created

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.