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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:56:04+00:00 2026-05-25T11:56:04+00:00

I have a quick question that I’m not quite sure to set up. I’ve

  • 0

I have a quick question that I’m not quite sure to set up. I’ve seen examples elsewhere but nothing specifically like my situation. I would like to resize images using PHP so they’re readable and not just wonkily stretched like if you use HTML. If they’re not 250 pixels wide, or 160 pixels tall, how can I resize the picture so it’s proportionate but fits within that space?

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-05-25T11:56:04+00:00Added an answer on May 25, 2026 at 11:56 am

    Ok, so below is an Image object that I use in my store. It maintains scale – requires GD

    <?php
    class Store_Model_Image extends My_Model_Abstract 
    {
        const PATH  = STORE_MODEL_IMAGE_PATH;
        const URL   = "/store-assets/product-images/";
    
        public function get_image_url($width, $height)
        {
            $old_file = self::PATH . $this->get_filename();
            $basename = pathinfo($old_file, PATHINFO_FILENAME);
    
            $new_name = sprintf("%s_%sx%s.jpg", $basename, $width, $height);
            if(file_exists(self::PATH . $new_name))
            {
                return self::URL . $new_name;
            }
            else
            {
                list($width_orig, $height_orig, $image_type) = @getimagesize($old_file);
                $img = FALSE;
    
                // Get the image and create a thumbnail     
                switch($image_type)
                {
                    case 1:
                        $img = @imagecreatefromgif($old_file);
                        break;
                    case 2:
                        $img = @imagecreatefromjpeg($old_file);
                        break;
                    case 3: 
                        $img = @imagecreatefrompng($old_file);
                        break;
                }
    
                if(!$img)
                {
                    throw new Zend_Exception("ERROR: Could not create image handle from path.");
                }
    
                // Build the thumbnail
                if($width_orig > $height_orig)
                {
                    $width_ratio = $width / $width_orig;
                    $new_width   = $width;
                    $new_height  = $height_orig * $width_ratio;
                }
                else
                {
                    $height_ratio = $height / $height_orig;
                    $new_width    = $width_orig * $height_ratio;
                    $new_height   = $height;
                }
    
                $new_img = @imagecreatetruecolor($new_width, $new_height);
    
                // Fill the image black
                if(!@imagefilledrectangle($new_img, 0, 0, $new_width, $new_height, 0))
                {           
                    throw new Zend_Exception("ERROR: Could not fill new image");
                }
    
                if(!@imagecopyresampled($new_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig))
                {
                    throw new Zend_Exception("ERROR: Could not resize old image onto new bg.");
                }
    
                // Use a output buffering to load the image into a variable
                ob_start();
                imagejpeg($new_img, NULL, 100);
                $image_contents = ob_get_contents();
                ob_end_clean();
    
                // lastly (for the example) we are writing the string to a file
                $fh = fopen(self::PATH . $new_name, "a+");
                fwrite($fh, $image_contents);
                fclose($fh);
    
                return self::URL . $new_name;
            }
        }
    }
    

    I resize the image at request time, so the first time the page loads an image will be resized to the required size for the template. (this means I don’t have to crash a shared host trying to regenerate image thumbnails everytime my design changes)

    So in the template you pass your image object, and when you need a image thumb,

    <img src="<?php echo $image->get_image_url(100, 100); ?>" />
    

    you now have a 100×100 thumb, which is saved to the Server for reuse at a later date

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

Sidebar

Related Questions

I have a quick question regarding memory management that I am not quite sure
I have a quick question that I'm sure some of you would have an
I have a quick question that I could not figure out in the docs
just a question that needs a quick answer, I have a Action, lets say,
Quick question... I have a query that checks for duplicates that looks like this:
Quick question. I have an app that use a native DLL through PInvoke, this
Hey guys quick question, I have an entry that I put in my database,
Hey guys quick question. I have a div that gets assigned a number to
Quick question. What do you think, I have a few sites that use a
Quick question that requires a long explanation.. Say I have two tables - one

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.