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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:05:57+00:00 2026-05-26T09:05:57+00:00

I have a PHP function that I use regularly for working with images (resizing,

  • 0

I have a PHP function that I use regularly for working with images (resizing, watermarking, converting to grayscale, etc). I am happy with it and it works well. However, it is designed to work with the $_FILES superglobal, and accepts it as a parameter.

I’ve run into a situation where I have an existing directory of files on my server that I need to process in the same way as I do for files uploaded from a form into the $_FILES array.

Figuring it would be easiest to work with my existing function, I have been looking for a way to duplicate the $_FILES superglobal, so I can pass it to my script, but I am not finding the functions/properties I need to accomplish this. (Although, at a glance, the getimagesize and filesize functions looks like they may help).

Can anyone advise on what functions/properties I would need to duplicate the $_FILES array? (Or an alternate way to accomplish what I am trying to do?)

For reference’s sake, the image function I use is here:

function resize_upload ($file, $dest, $maxw = 50, $maxh = 50, $grey = false, $wm = false, $mark = "a/i/watermark.png", $opa = 40) {     
    $allowext = array("gif", "jpg", "png", "jpeg", "bmp");      
    $fileext = strtolower(getExtension($file['name'])); 
    if (!in_array($fileext,$allowext)) {
        echo "Wrong file extension.";
        exit();
    }
    list($width, $height, $imgcon) = getimagesize($file['tmp_name']);   
    if ($file['size'] && ($width > $maxw || $height > $maxh)) {     
        if($file['type'] == "image/pjpeg" || $file['type'] == "image/jpeg"){$newimg = imagecreatefromjpeg($file['tmp_name']);}
        elseif($file['type'] == "image/x-png" || $file['type'] == "image/png"){$newimg = imagecreatefrompng($file['tmp_name']);}
        elseif($file['type'] == "image/gif"){$newimg = imagecreatefromgif($file['tmp_name']);}          
        $ratio = $width/$height;
        if ($ratio < 1) { // Width < Height
            $newheight = $maxh;
            $newwidth = $width * ($maxh/$height);
            if ($newwidth > $maxw) {
                $newheight = $newheight * ($maxw/$newwidth);
                $newwidth = $maxw;              
            }
        } elseif ($ratio == 1) { // Width = Height
            if ($maxw < $maxh) {
                $newheight = $maxw;
                $newwidth = $maxw;
            } elseif ($maxw == $maxh) {
                $newheight = $maxh;
                $newwidth = $maxw;
            } elseif ($maxw > $maxh) {
                $newheight = $maxh;
                $newwidth = $maxh;
            }
        } elseif ($ratio > 1) { // Width > Height           
            $newwidth = $maxw;
            $newheight = $height * ($maxw/$width);
            if ($newheight > $maxh) {
                $newwidth = $newwidth * ($maxh/$newheight);
                $newheight = $maxh;
            }
        }       
        if (function_exists(imagecreatetruecolor)) {$resize = imagecreatetruecolor($newwidth, $newheight);}             
        if (($imgcon == IMAGETYPE_GIF)) {
            $trnprt_indx = imagecolortransparent($newimg);
            if ($trnprt_indx >= 0) {
                $trnprt_color = imagecolorsforindex($newimg, $trnprt_indx);
                $trnprt_indx = imagecolorallocate($resize, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
                imagefill($resize, 0, 0, $trnprt_indx);
                imagecolortransparent($resize, $trnprt_indx);
            }
        } elseif ($imgcon == IMAGETYPE_PNG) {
            imagealphablending($resize, false);
            $color = imagecolorallocatealpha($resize, 0, 0, 0, 127);
            imagefill($resize, 0, 0, $color);
            imagesavealpha($resize, true);
        }
        imagecopyresampled($resize, $newimg, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        if ($wm) {
            $watermark = imagecreatefrompng($mark);
            $wm_width = imagesx($watermark);
            $wm_height = imagesy($watermark);
            $destx = $newwidth - $wm_width - 5;
            $desty = $newheight - $wm_height - 5;
            imagecopymerge($resize, $watermark, $destx, $desty, 0, 0, $wm_width, $wm_height, $opa);
            imagedestroy($watermark);
        }
        $filename = random_name().".".$fileext;
        if ($grey) {imagefilter($resize, IMG_FILTER_GRAYSCALE);}
        if($file['type'] == "image/pjpeg" || $file['type'] == "image/jpeg"){$new = imagejpeg($resize, $dest."/".$filename, 100);}
        elseif($file['type'] == "image/x-png" || $file['type'] == "image/png"){$new = imagepng($resize, $dest."/".$filename, 0);}
        elseif($file['type'] == "image/gif"){$new = imagegif($resize, $dest."/".$filename);}        
        imagedestroy($resize);
        imagedestroy($newimg);      
        return $filename;
    } elseif ($file['size']) {
        $filename = random_name().".".getExtension($file['name']);
        if ($grey) {
            if($file['type'] == "image/pjpeg" || $file['type'] == "image/jpeg"){$newimg = imagecreatefromjpeg($file['tmp_name']);}
            elseif($file['type'] == "image/x-png" || $file['type'] == "image/png"){$newimg = imagecreatefrompng($file['tmp_name']);}
            elseif($file['type'] == "image/gif"){$newimg = imagecreatefromgif($file['tmp_name']);}
            imagefilter($newimg, IMG_FILTER_GRAYSCALE);
            if($file['type'] == "image/pjpeg" || $file['type'] == "image/jpeg"){imagejpeg($newimg, $dest."/".$filename);}
            elseif($file['type'] == "image/x-png" || $file['type'] == "image/png"){imagepng($newimg, $dest."/".$filename);}
            elseif($file['type'] == "image/gif"){imagegif($newimg, $dest."/".$filename);}
            imagedestroy($newimg);
            return $filename;
        } else {
            $upload = file_upload($file, $dest);
            return $upload;
        }
    }
}
  • 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-26T09:05:58+00:00Added an answer on May 26, 2026 at 9:05 am

    The $_FILES array contains a nested array for an uploaded file. This nested array has 5 keys. For each key I explain what it should contain, and what function to use:

    • name: the name of the file, use the basename() function for this entry
    • type: the mime type of the file, for images set to ‘image/png’, ‘image/jpeg’, etc
    • tmp_name: the path to the actual file, here you should set the path to your images
    • error: this indicates that an error occured with the upload, in your case you can set it to 0 for no error
    • size: the size of the file in bytes, so you can use the filesize() function for your image

    An example:

    $_FILES = array('image' => array(
        'name' => basename('/path/to/image.png'),
        'type' => 'image/png',
        'tmp_name' => '/path/to/image.png',
        'error' => 0,
        'size' => filesize('/path/to/image.png')
    ));
    

    If you want to process multiple files at once, you should be aware that the structure of the $_FILES array is different than what you would expect in this case, see this comment in the PHP docs.

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

Sidebar

Related Questions

I have a function that I use on index.php page and I would like
I have a .NET DLL containing functions that I can use in PHP. Though
I have a PHP function that takes a variable number of arguments (using func_num_args()
I have a PHP function that requires can take 3 parameteres... I want to
Is it possible to have a PHP function that is both recursive and anonymous?
I have a php function I wrote that will take a text file and
I have written a PHP function to take a video embed code that has
I have a function that finds a regex thingy, then replaces with php code.
I have a jquery-ajax function that sends data to a php script and the
I have created a function in PHP that calls a webservice and parses through

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.