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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:10:25+00:00 2026-06-06T23:10:25+00:00

I am having some issues with imagecopyresampled mostly that the image is not scaling

  • 0

I am having some issues with imagecopyresampled mostly that the image is not scaling correctly and that the position of the image is wrong and there is a black border around the edges.

I have the following vars set up

    $tW = $width; // Original width of image
    $tH = $height; // Orignal height of image

    $w = postvar; // New width
    $h = postvar; // New height
    $x = postvar; // New X pos
    $y = postvar; // New Y pos

And then run the following

    $tn = imagecreatetruecolor($w, $h);
    $image = imagecreatefromjpeg('filepathhere.jpeg');
    imagecopyresampled($tn, $image, 0, 0, $x, $y, $w, $h, $tW, $tH);

If anyone has any clues then that would be great help! Thanks

  • 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-06T23:10:26+00:00Added an answer on June 6, 2026 at 11:10 pm

    There are several issues here. First, you shouldn’t create the new image with the specified new height and width, but instead calculate what they should be based on the ratio of the original image, otherwise your scaled image will be distorted. For example, the code below will create a properly resized image that will fit into the given rectangle of $w x $h:

    $tW = $width;    //original width
    $tH = $height;   //original height
    
    $w = postvar;
    $h = postvar;
    
    if($w == 0 || $h == 0) {
        //error...
        exit;
    }
    
    if($tW / $tH > $w / $h) {
        // specified height is too big for the specified width
        $h = $w * $tH / $tW;
    }
    elseif($tW / $tH < $w / $h) {
        // specified width is too big for the specified height
        $w = $h * $tW / $tH;
    }
    
    $tn = imagecreatetruecolor($w, $h);  //this will create it with black background
    imagefill($tn, 0, 0, imagecolorallocate($tn, 255, 255, 255));    //fill it with white;
    
    //now you can copy the original image:
    $image = imagecreatefromjpeg('filepathhere.jpeg');
    //next line will just create a scaled-down image
    imagecopyresampled($tn, $image, 0, 0, 0, 0, $w, $h, $tW, $tH);
    

    Now, if you want to copy only a certain portion of the original image, say from coordinates ($x, $y) to the right-hand corner, then you need to include that into your calculation:

    $tW = $width - $x;    //original width
    $tH = $height - $y;   //original height
    
    $w = postvar;
    $h = postvar;
    
    if($w == 0 || $h == 0) {
        //error...
        exit;
    }
    
    if($tW / $tH > $w / $h) {
        // specified height is too big for the specified width
        $h = $w * $tH / $tW;
    }
    elseif($tW / $tH < $w / h) {
        // specified width is too big for the specified height
        $w = $h * $tW / $tH;
    }
    
    $tn = imagecreatetruecolor($w, $h);  //this will create it with black background
    imagefill($tn, 0, 0, imagecolorallocate($tn, 255, 255, 255));    //fill it with white;
    
    //now you can copy the original image:
    $image = imagecreatefromjpeg('filepathhere.jpeg');
    //next line will create a scaled-down portion of the original image from coordinates ($x, $y) to the lower-right corner
    imagecopyresampled($tn, $image, 0, 0, $x, $y, $w, $h, $tW, $tH);
    

    If you give more details about what you’re trying to achieve, I may be able to help further.

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

Sidebar

Related Questions

I'm having some issues to deserialize a Json array that follows this format: [
I am having some issues with a code that is occasionally and sporadically throwing
Recently I was having some issues with a singelton class that was lazy initializing
I'm having some issues getting file uploads to work correctly and the following code
I having some issues with a client of mine. I've built (not designed) an
I am having some issues with the colorWithPatternImage. The image stretched out in a
Having some issues with code not executing within the classes I created and thought
I'm having some issues returning unique results from a table that has some repeating
I'm having some issues with a web server. It is not used a lot
I am having some issues with an extension that i am trying to make.

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.