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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:27:06+00:00 2026-06-03T23:27:06+00:00

I have found a few things on the web about PHP+GD regarding image manipulation,

  • 0

I have found a few things on the web about PHP+GD regarding image manipulation, but none seem to give me what I am looking for.

I have someone upload an image of any dimensions, the script I have written resizes the image to no more than 200px wide by 200px high while maintaining aspect ratio. So final image could be 150px by 200px for example. What I want to do is then manipulate the image further and add a matting around the image to pad it to 200px by 200px while not affecting the original image. For example:

Unpadded Image 143x200

Padded image 200x200

The code I have to get the image resized is here, I have tried a few things, but am definitely having an issue implementing the secondary process of adding the padding.

list($imagewidth, $imageheight, $imageType) = getimagesize($image);
$imageType = image_type_to_mime_type($imageType);
$newImageWidth = ceil($width * $scale);
$newImageHeight = ceil($height * $scale);
$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
switch($imageType) {
    case "image/gif":
        $source=imagecreatefromgif($image); 
        break;
    case "image/pjpeg":
    case "image/jpeg":
    case "image/jpg":
        $source=imagecreatefromjpeg($image); 
        break;
    case "image/png":
    case "image/x-png":
        $source=imagecreatefrompng($image); 
        break;
}
imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
imagejpeg($newImage,$image,80);
chmod($image, 0777);

I am thinking I need to use imagecopy() right after the imagecopyresampled() call. That way the image is already the size I want, I just need to create an image exactly 200 x 200 and paste $newImage into the center (vert and horiz) of that. Do I need to create an entirely new image and merge the two, or is there a way to just pad the image I alread have created ($newImage)? Thanks in advance, all the tutorials I have found have led me nowhere, and the only applicable one I found on SO was for android 🙁

  • 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-06-03T23:27:07+00:00Added an answer on June 3, 2026 at 11:27 pm
    1. Open the original image
    2. Create a new blank image.
    3. Fill the new image with the background colour your want
    4. Use ImageCopyResampled to resize&copy the original image centered onto the new image
    5. Save the new image

    Instead of your switch statement you can also use

    $img = imagecreatefromstring( file_get_contents ("path/to/image") );
    

    This will auto detect the image type (if the imagetype is supported by your install)

    Updated with code sample

    $orig_filename = 'c:\temp\380x253.jpg';
    $new_filename = 'c:\temp\test.jpg';
    
    list($orig_w, $orig_h) = getimagesize($orig_filename);
    
    $orig_img = imagecreatefromstring(file_get_contents($orig_filename));
    
    $output_w = 200;
    $output_h = 200;
    
    // determine scale based on the longest edge
    if ($orig_h > $orig_w) {
        $scale = $output_h/$orig_h;
    } else {
        $scale = $output_w/$orig_w;
    }
    
        // calc new image dimensions
    $new_w =  $orig_w * $scale;
    $new_h =  $orig_h * $scale;
    
    echo "Scale: $scale<br />";
    echo "New W: $new_w<br />";
    echo "New H: $new_h<br />";
    
    // determine offset coords so that new image is centered
    $offset_x = ($output_w - $new_w) / 2;
    $offset_y = ($output_h - $new_h) / 2;
    
        // create new image and fill with background colour
    $new_img = imagecreatetruecolor($output_w, $output_h);
    $bgcolor = imagecolorallocate($new_img, 255, 0, 0); // red
    imagefill($new_img, 0, 0, $bgcolor); // fill background colour
    
        // copy and resize original image into center of new image
    imagecopyresampled($new_img, $orig_img, $offset_x, $offset_y, 0, 0, $new_w, $new_h, $orig_w, $orig_h);
    
        //save it
    imagejpeg($new_img, $new_filename, 80);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found a few libraries to edit MP3 tags (UltraID3Lib is great) but
I have question regarding disabling browser caching. I have already found few solutions, and
I have found some libraries or web services in PHP that does the job.
I have found few interesting things (for me at least) while generating random integers
I have found a few samples on how to use GameKit for bluetooth communication
I have found some code that I need to use for my application but
I have found some in the Cappuccino website (vim, textmate and SubEthaEdit), but not
I'm here to ask a specific topic - I really found few info about
I'm developing my personal web site using php. everything is ok but I just
I found a simple web service online at http://chennaiemergency.co.in/sree/s2.php?wsdl which i am able 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.