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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:45:55+00:00 2026-05-27T06:45:55+00:00

This is quite a complicated and brain busting question, so please read carefully and

  • 0

This is quite a complicated and brain busting question, so please read carefully and don’t just skim through. Thanks!

I’m extracting images from a PDF file and rebuilding the page image. It’s all working except I can’t get the coordinates just right for placing the little images over the larger background image.

I start with something like this:

$page_width = 1000;
$page_height = 1500;
$back_img_x = 5
$back_img_y = 8
$back_img_width = 990;
$back_img_height = 1501;
$back_img_truewidth = 3000;
$back_img_trueheight = 4000;
$little_img_x = 550;
$little_img_y = 250;
$little_img_width = 200;
$little_img_height = 100;

Now to explain, the back image is not necessarily the same size as the page, which means it’s being scaled, it is also not necessarily position at 0,0 which is due to whoever the stupid person was who made the PDF in the first place (or which application.)

What I now do is ignore the page size and try to map the little image onto the back image in the correct position after trimming the back image.

So lots of complicated things are now done to the back image, which results in the following:

$trimleft = 100;
$trimright = 100;
$trimtop = 40;
$trimbottom = 120;
$margin_x = 50;
$margin_y = 35
$offset_x = 0;
$offset_y = 90;

The back image is then correctly trimmed and placed upon a new canvas like this:

$fullwidth=($trimright-$trimleft)+($margin_x*2);
$fullheight=($trimbottom-$trimtop)+($margin_y*2);
$trimmed=imagecreate($fullwidth,$fullheight);
imagefill($trimmed,0,0,imagecolorallocate($trimmed,255,255,255)); // make white
imagecopy($trimmed,$image,$margin_x+$offset_x,$margin_y+$offset_y,$trimleft,$trimtop,$trimright-$trimleft,$trimbottom-$trimtop);

So far so good…

Now I want to find the x, y, width & height of the little image to be in the correct position over the background image.

I do this:

$little_img_width=($little_img_width/$back_img_width);
$little_img_height=($little_img_height/$back_img_height);
$little_img_x=(($little_img_x-$back_img_x)/$back_img_width)*$back_img_truewidth;
$little_img_y=(($little_img_y-$back_img_y)/$back_img_height)*$back_img_trueheight;

$little_img_width*=($back_img_truewidth/$fullwidth);
$little_img_height*=($back_img_trueheight/$fullheight);
$little_img_x=($little_img_x-$trimleft+$margin_x+$offset_x)/$fullwidth;
$little_img_y=($little_img_y-$trimtop+$margin_y+$offset_y)/$fullheight;

That should give me the correct x, y, width & height of the little image, correctly positioned and sized on the background image (in percentage where 0 is far left and 1 is far right, etc.) The width/height are correct but the coordinates are very slightly off. It is the y coordinate that is too low on the page by about 5%. Note that $offset_x is 0, which would suggest that the problem lies with the offset variables (since $offset_x is 0, it is not showing an positioning error for x axis, only y axis).

I’ve been staring at these values for days and rewritten them all a few times. This time it all looks perfect to me. I don’t understand what is still wrong.

  • 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-27T06:45:56+00:00Added an answer on May 27, 2026 at 6:45 am

    If I’m understanding the math correctly, here’s what’s happening:

    You have $fullwidth and $fullheight which are the width and height that the image will be occupying on your canvas.

    You then multiply the smaller image’s width and height by the ratio of its size to the full size in the original, unprocessed image. Which is correct.

    However, when you are calculating $little_img_x and $little_img_y, you are doing this:

    $little_img_x=(($little_img_x-$back_img_x)/$back_img_width)*$back_img_truewidth;
    $little_img_y=(($little_img_y-$back_img_y)/$back_img_height)*$back_img_trueheight;
    

    This ends up returning an awkwardly arbitrary number (something like 150 for x and 100 for y), which I can’t find any possible use for.

    You then go on and use that value to calculate the new x and y for the little image, which would result in another arbitrary number which shows no purpose.

    I propose a new approach where $little_img_x and $little_img_y are calculated as follows:

    $little_img_x = ($little_img_x/$back_img_x)*$fullwidth + (($little_img_x-$trimleft+$margin_x+$offset_x) / $fullwidth)
    $little_img_y = ($little_img_y/$back_img_y)*fullheight + (($little_img_y-$trimtop+$margin_y+$offset_y)/ $fullheight)
    

    Basically, we take the little image’s original x, get its ratio to the background’s x, multiply it by the new width to get the presented x, and then add all of the offset processing. We do the same thing with y.

    I have no idea if this works, but I hope it does. Good luck!

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

Sidebar

Related Questions

This is a more general question: I have a quite complicated files table in
I've edited this quite a bit and bolded my question at this point. I
I know I'm not asking this quite right, either. Please help me better form
This could quite possibly be the dumbest question ever asked. Our client wants us
This feels quite complicated to ask, and whilst the solution seems simple, the shear
This seems quite complicated to me, and I cannot get it to work. Basically
I have a quite complicated question to ask :) I am currently working on
Ok, I know the title is quite complicated but the question is hard to
This is probably quite trivial but.... I have a quite complicated linq query that
I am intentionally leaving this quite vague at first. I'm looking for discussion and

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.