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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:57:04+00:00 2026-06-16T12:57:04+00:00

I’m currently working on a script that shows thumbnails from a website on a

  • 0

I’m currently working on a script that shows thumbnails from a website on a mobile app, and I’m having problems with the “retina display”. If the original image is big enough, I show a thumbnail with the double of the required size, and if it is not, I show it on the required size. Now, my function checks if it can be resized proportionally and if it can’t, I resize it to the “min-width” or “min-height” and crop it from the center.

Here is the problem: if it detects that the image can’t be shown on the double of size, “scale up” the cropped size until it reaches the limits of the original size, and I can’t find a way to scale it right. (My main problem is that I’m not really good at maths :P).

For a more simple lecture:

  • This is the original size of the image: 600 x 301
  • This is the required/cropped size: 320 x 180
  • This is the size I want to get: 535 x 301. I got it from Photoshop, and is the result of scaling 320×180 up until it finds the limit of the original size.

PS: I know GD, so I only need the formula to calculate the size.

  • 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-16T12:57:05+00:00Added an answer on June 16, 2026 at 12:57 pm

    The same algorithm that scales down can also scale up. This is untested code, and it is only designed to scale down, but if you remove the “scale down” tests it might do the trick for you.

    <?php // RAY_image_resize_and_crop.php
    error_reporting(E_ALL);
    
    
    // RESIZE AN IMAGE PROPORTIONALLY AND CROP TO THE CENTER
    
    
    function resize_and_crop($original_image_url, $thumb_image_url, $thumb_w, $thumb_h, $quality=75)
    {
        // ACQUIRE THE ORIGINAL IMAGE: http://php.net/manual/en/function.imagecreatefromjpeg.php
        $original = imagecreatefromjpeg($original_image_url);
        if (!$original) return FALSE;
    
        // GET ORIGINAL IMAGE DIMENSIONS
        list($original_w, $original_h) = getimagesize($original_image_url);
    
        // RESIZE IMAGE AND PRESERVE PROPORTIONS
        $thumb_w_resize = $thumb_w;
        $thumb_h_resize = $thumb_h;
        if ($original_w > $original_h)
        {
            $thumb_h_ratio  = $thumb_h / $original_h;
            $thumb_w_resize = (int)round($original_w * $thumb_h_ratio);
        }
        else
        {
            $thumb_w_ratio  = $thumb_w / $original_w;
            $thumb_h_resize = (int)round($original_h * $thumb_w_ratio);
        }
        if ($thumb_w_resize < $thumb_w)
        {
            $thumb_h_ratio  = $thumb_w / $thumb_w_resize;
            $thumb_h_resize = (int)round($thumb_h * $thumb_h_ratio);
            $thumb_w_resize = $thumb_w;
        }
    
        // CREATE THE PROPORTIONAL IMAGE RESOURCE
        $thumb = imagecreatetruecolor($thumb_w_resize, $thumb_h_resize);
        if (!imagecopyresampled($thumb, $original, 0,0,0,0, $thumb_w_resize, $thumb_h_resize, $original_w, $original_h)) return FALSE;
    
        // ACTIVATE THIS TO STORE THE INTERMEDIATE IMAGE
        // imagejpeg($thumb, 'RAY_temp_' . $thumb_w_resize . 'x' . $thumb_h_resize . '.jpg', 100);
    
        // CREATE THE CENTERED CROPPED IMAGE TO THE SPECIFIED DIMENSIONS
        $final = imagecreatetruecolor($thumb_w, $thumb_h);
    
        $thumb_w_offset = 0;
        $thumb_h_offset = 0;
        if ($thumb_w < $thumb_w_resize)
        {
            $thumb_w_offset = (int)round(($thumb_w_resize - $thumb_w) / 2);
        }
        else
        {
            $thumb_h_offset = (int)round(($thumb_h_resize - $thumb_h) / 2);
        }
    
        if (!imagecopy($final, $thumb, 0,0, $thumb_w_offset, $thumb_h_offset, $thumb_w_resize, $thumb_h_resize)) return FALSE;
    
        // STORE THE FINAL IMAGE - WILL OVERWRITE $thumb_image_url
        if (!imagejpeg($final, $thumb_image_url, $quality)) return FALSE;
        return TRUE;
    }
    
    
    // USE CASE
    echo '<a target="_blank" href="RAY_orig_600x374.jpg">Original 600x374</a><br/>';
    
    resize_and_crop('RAY_orig_600x374.jpg', 'RAY_temp_100x100.jpg', 100, 100);
    echo '<a target="_blank" href="RAY_temp_100x100.jpg">100x100</a><br/>';
    
    resize_and_crop('RAY_orig_600x374.jpg', 'RAY_temp_200x100.jpg', 200, 100);
    echo '<a target="_blank" href="RAY_temp_200x100.jpg">200x100</a><br/>';
    
    resize_and_crop('RAY_orig_600x374.jpg', 'RAY_temp_200x300.jpg', 200, 300);
    echo '<a target="_blank" href="RAY_temp_200x300.jpg">200x300</a><br/>';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am currently running into a problem where an element is coming back from
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

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.