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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:16:58+00:00 2026-05-17T01:16:58+00:00

I have no idea how to resize image in PHP, my code is: for

  • 0

I have no idea how to resize image in PHP, my code is:

for ($index = 1; $index <= 2; $index++) { 

    if (!empty($_FILES["pic$index"]["name"])) {
        $ext = substr($_FILES["pic$index"]["name"], strrpos($_FILES["pic$index"]["name"], '.') + 1);
        $dir = "../gallery/$mkdir";

        HERE I NEED THE RESIZE OF THE TMP FILE OF IMAGE

        move_uploaded_file($_FILES["pic$index"]["tmp_name"] , "$dir/img-$index.$ext");

    }  

}

$mkdir = the name of the gallery’s folder (there are many galleries).

$dir = where the pics will be placed.

$ext = the type of the image (png, gif or jpg).

foreach loop runs two times because you can upload two pics.

This script is working good, I just need to do resize and I dont have an idea how to do it..

  • 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-17T01:16:58+00:00Added an answer on May 17, 2026 at 1:16 am

    Here is the code I’m using to resize images.

    In my case I give to the function the original file name and then the thumbnail file name.

    You can adapt it for your case very easily.

    public static function GenerateThumbnail($im_filename,$th_filename,$max_width,$max_height,$quality = 0.75)
    {
    // The original image must exist
    if(is_file($im_filename))
    {
        // Let's create the directory if needed
        $th_path = dirname($th_filename);
        if(!is_dir($th_path))
            mkdir($th_path, 0777, true);
        // If the thumb does not aleady exists
        if(!is_file($th_filename))
        {
            // Get Image size info
            list($width_orig, $height_orig, $image_type) = @getimagesize($im_filename);
            if(!$width_orig)
                return 2;
            switch($image_type)
            {
                case 1: $src_im = @imagecreatefromgif($im_filename);    break;
                case 2: $src_im = @imagecreatefromjpeg($im_filename);   break;
                case 3: $src_im = @imagecreatefrompng($im_filename);    break;
            }
            if(!$src_im)
                return 3;
    
    
            $aspect_ratio = (float) $height_orig / $width_orig;
    
            $thumb_height = $max_height;
            $thumb_width = round($thumb_height / $aspect_ratio);
            if($thumb_width > $max_width)
            {
                $thumb_width    = $max_width;
                $thumb_height   = round($thumb_width * $aspect_ratio);
            }
    
            $width = $thumb_width;
            $height = $thumb_height;
    
            $dst_img = @imagecreatetruecolor($width, $height);
            if(!$dst_img)
                return 4;
            $success = @imagecopyresampled($dst_img,$src_im,0,0,0,0,$width,$height,$width_orig,$height_orig);
            if(!$success)
                return 4;
            switch ($image_type) 
            {
                case 1: $success = @imagegif($dst_img,$th_filename); break;
                case 2: $success = @imagejpeg($dst_img,$th_filename,intval($quality*100));  break;
                case 3: $success = @imagepng($dst_img,$th_filename,intval($quality*9)); break;
            }
            if(!$success)
                return 4;
        }
        return 0;
    }
    return 1;
    }
    

    The return codes are just here to differentiate between different types of errors.

    By looking back at that code, I don’t like the “magic number” trick. I’m gonna have to change that (by exceptions for example).

    if (!empty($_FILES["pic$index"]["name"])) {
        $ext = substr($_FILES["pic$index"]["name"], strrpos($_FILES["pic$index"]["name"], '.') + 1);
        $dir = "../gallery/$mkdir";
        // Move it
        if(move_uploaded_file($_FILES["pic$index"]["tmp_name"] , "$dir/img-$index.$ext.tmp"))
        {
          // Resize it
          GenerateThumbnail("$dir/img-$index.$ext.tmp","$dir/img-$index.$ext",600,800,0.80);
          // Delete full size
          unlink("$dir/img-$index.$ext.tmp");
        }
    } 
    

    Use move_uploaded_file to move it (recommanded) and then you can resize it and send it to it’s final destination. You might not even need the “.tmp”, you can use.

        // Move it
        if(move_uploaded_file($_FILES["pic$index"]["tmp_name"] , "$dir/img-$index.$ext"))
        // Resize it
          GenerateThumbnail("$dir/img-$index.$ext","$dir/img-$index.$ext",600,800); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have an idea of how to resize an image isotropically? Meaning I
I have a script that runs to upload an image, then resize it into
Do you have any idea why jQuery cycle plugin breaks backgorund image centering when
Possible Duplicate: PHP image resize on the fly vs storing resized images I was
I need a PHP page that will take an image and resize to how
So, I am just trying to resize an image with WideImage, the code is
I am writing a PHP script to dynamically resize an image. The image ID
Anyone have idea about how to bind Boolean in sqlite with objective - c.
I have idea to write errors from my application to the Windows Event Viewer
Anyone have idea how to pass value from iPhone application to the REST-Webservice using

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.