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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:42:57+00:00 2026-05-13T07:42:57+00:00

I am using directory iterator to iterate through directories and resize images found in

  • 0

I am using directory iterator to iterate through directories and resize images found in that directory, I am doing this from browser cause I don’t have ssh access to that server.
Most pictures resize fine but for every 10 pictures (more or less) I get jiberish data out.
I think it’s a picture source. in that data there is always a string CREATOR: gd-jpeg v1.0 so I’m wondering what is this? I disabled any error output with @ sign.

EDIT:

Here is the code, and also I disabled error output cause there aren’t any errors and I thought that disabling error output would disable this jiberish data, but data is displayed no matter.

Code:

<?php
/*
big = 350
thumb = 90
thumb2 = 70
top = 215
*/

set_time_limit(0);
ini_set('memory_limit', '128M');
ini_set('display_errors', 'On'); 

class ResizeImages
{
    private $dir = 'images/articles_backup_2009-12-19';
    private $imageType = array(
        '_big' => 'h:350',
        '_thumb' => 'm:90',
        '_thumb2' => 'h:70',
        '_top' => 'h:215'
    );

    public function __construct()
    {
        $this->deleteImages();
        $this->resizeImages();
    }

    private function resizeImages()
    {
        $n = 0;
        $dh = opendir($this->dir);
        while (($file = readdir($dh)) !== false) 
        {
            if(is_dir($this->dir."/".$file) && $file != '.' && $file != '..')
            {   
                echo $this->dir."/".$file.'<br />';
                $deldir = opendir($this->dir."/".$file);
                while (($filedel = readdir($deldir)) !== false) 
                {
                    if ($filedel != '.' && $filedel != '..' && $filedel != 'Thumbs.db') 
                    {
                        $val = $this->resize($this->dir."/".$file."/".$filedel);
                        $n++;
                    }
                }
            }
        }
        closedir($dh);
    }

    private function resize($target)
    {
        $img = $target;

        $origSize = getimagesize($img);
        $origWidth = $origSize[0];
        $origHeight = $origSize[1];

        foreach($this->imageType as $key=>$value)
        {
            $attr = explode(':', $value);

            if(strpos($attr[0], 'w') !== false) 
            {
                $this->imageWidth = $attr[1];
                $this->imageHeight = false;
            }
            if(strpos($attr[0], 'h') !== false) 
            {
                $this->imageHeight = $attr[1];
                $this->imageWidth = false;
            }

            $imageTmp = explode('.', $img);
            if(count($imageTmp) == 2) $image_name_fin = $imageTmp[0].$key.'.'.$imageTmp[1];
            else if(count($imageTmp) == 4) $image_name_fin = $imageTmp[0].'.'.$imageTmp[1].$key.'.'.$imageTmp[2];

            if($this->imageWidth != false) 
            {
                if($origWidth <= $this->imageWidth)
                {
                    $resizeHeight = $origHeight;
                    $resizeWidth = $origWidth;
                }
                else
                {
                    $resizeHeight = round($origHeight / ($origWidth / $this->imageWidth));
                    $resizeWidth = $this->imageWidth;
                }
            }
            else if($this->imageHeight != false) 
            {
                if($origHeight <= $this->imageHeight)
                {
                    $resizeHeight = $origHeight;
                    $resizeWidth = $origWidth;
                }
                else
                {
                    $resizeWidth = round($origWidth / ($origHeight / $this->imageHeight));
                    $resizeHeight = $this->imageHeight;
                }
            }

            $im = ImageCreateFromJPEG ($img) or // Read JPEG Image
            $im = ImageCreateFromPNG ($img) or // or PNG Image
            $im = ImageCreateFromGIF ($img) or // or GIF Image
            $im = false; // If image is not JPEG, PNG, or GIF

            if (!$im) 
            {
                $this->error = array(
                    'error' => true,
                    'notice' => 'UPLOADUNSUCCESSFULL'
                );
                return $this->error;
            }

            $thumb = ImageCreateTrueColor ($resizeWidth, $resizeHeight);
            ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, $resizeWidth, $resizeHeight, $origWidth, $origHeight);
            ImageJPEG ($thumb, $image_name_fin, $this->imageQuality);
            //echo $image_name_fin.'<br />';
        }

        $this->error = array(
            'imageUrl' => $image_name,
            'error' => false,
            'notice' => 'IMAGEUPLOADED'
        );
        return $this->error;            
    }

    private function deleteImages()
    {
        $dh = opendir($this->dir);
        while (($file = readdir($dh)) !== false) 
        {
            if(is_dir($this->dir."/".$file))
            {
                //echo $file.'<br />';
                $deldir = opendir($this->dir."/".$file);
                while (($filedel = readdir($deldir)) !== false) 
                {
                    if(strpos($this->dir."/".$file."/".$filedel, '_big.') !== false || strpos($this->dir."/".$file."/".$filedel, '_thumb.') !== false || strpos($this->dir."/".$file."/".$filedel, '_thumb2.') !== false || strpos($this->dir."/".$file."/".$filedel, '_top.') !== false)
                    {
                        unlink($this->dir."/".$file."/".$filedel);
                    }
                }
            }
        }
        closedir($dh);
    }
}

$batch = new ResizeImages;


?>
  • 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-13T07:42:57+00:00Added an answer on May 13, 2026 at 7:42 am

    At the top add this:

    error_reporting(E_ALL);
    

    And try changing this:

    $im = ImageCreateFromJPEG ($img) or // Read JPEG Image
    $im = ImageCreateFromPNG ($img) or // or PNG Image
    $im = ImageCreateFromGIF ($img) or // or GIF Image
    $im = false; // If image is not JPEG, PNG, or GIF
    

    To this:

    $im = ImageCreateFromString(file_get_contents($img));
    

    Did it help? Also is there any pattern on the corrupted images? Are they all of the same type?

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

Sidebar

Related Questions

I have code using boost to list directory contents, iterate through each file, and
Is it possible in Struts 2 to iterate an enum using the tag <s:iterator>
I need to filter out some files from directory that contains loads of files.
Why when I try to iterate all directories and subdirectories of a partition using
I am currently using the directory walker from Here import os class DirectoryWalker: #
I'm trying to iterator over some directories containing approximately 3000 images. I load the
I have some code that iterates through files in a directory and does useful
I understand that to iterate through a Map , I need to use the
How do I iterate over this (or the data object that is passed to
I am using Directory.GetFiles to find files that will be copied. I need to

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.