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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:21:32+00:00 2026-06-08T01:21:32+00:00

Is it possible to re-size an image while displaying an image in website?/ How

  • 0

Is it possible to re-size an image while displaying an image in website?/ How to re-size an image after image been uploaded?

While adding i am uploading original image into original folder and creating 1 thumbnail uploading into thumb folder. But in website i need to display images in so-many places with different dimension. So i need to re-size the images for required size to display to avoid image shrink.

or Should i need to create an image for all dimensions i needed while uploading?

  • 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-08T01:21:33+00:00Added an answer on June 8, 2026 at 1:21 am

    Of course, it is possible. Besides, it is, possibly, the best approach: to create necessary thumbnail images by request. If your website is in development stage you will never guess what dimensions will create the designer tomorrow. And you will return to your uploading function again and again to fit it for new design. It worth removing hard dependency between design and logic.

    I’m not sure about codeignitor. Anyway, use something like this in your templates:

    class Image {
    
        public $filename;
        public $caption;
    
        /**
         * Return full path to image.
         * @return string path to file to make thumb
         */
        public function fullPath() {
            return "data/files/{$this->filename}";
        }
    
        /**
         * Renders HTML IMG for thumb of given size.
         * 
         * @param int $width max width, set to -1, if not important
         * @param type $height max height, set to -1, if not important
         * @return string html tag for image with correct width and height attributes
         */
        public function htmlTag($width, $height) {
            $t = $this->getThumb($width, $height);
            return "<img src=\"{$t}\" alt=\"{$this->caption}\" width=\"{$width}\" height=\"{$height}\" />";
        }
    
        /**
         * Get/create thumb image
         * @param int $width width of the image
         * @param int $height height of the image
         * @return string path to the image
         */
        public function getThumb(&$width, &$height) {
            $currentImage = $this->fullPath();
    
            $thumbFilename = md5($this->path . $width . $height) . '.png';
    
            $thumbDir = 'data/thumbs/';
            $thumbFilename = "{$thumbDir}/{$thumbFilename}";
    
            // thumb already done?
            if (is_file($thumbFilename)) {
                // get real size to create correct html img tag
                if ($width<0 || $height<0) {
                    $size = getimagesize($thumbFilename);
                    $width = $size[0];
                    $height = $size[1];
                }
                return $thumbFilename;
            }
    
            $ext = strtolower(pathinfo($currentImage, PATHINFO_EXTENSION));
            if ($ext == 'jpeg' || $ext == 'jpg') {
                $source = imagecreatefromjpeg($currentImage);
            } else if ($ext == 'gif') {
                $source = imagecreatefromgif($currentImage);
            } else if ($ext == 'png') {
                $source = imagecreatefrompng($currentImage);
            }
    
            $currentWidth = imagesx($source);
            $currentHeight = imagesy($source);
    
            // the sizes which we really will apply (default setup)
            $realWidth = $width;
            $realHeight = $height;
            $realX = 0;
            $realY = 0;
    
            // decide regarding cutting
            // if all params > 0, cuttin will be done
            $cut = FALSE;
            if ($width > 0 && $height > 0) {
                $cut = TRUE;
            } else if ($width < 0) { // width is not important, set proportion to that
                $width = $realWidth = round($currentWidth * $height / $currentHeight);
            } else if ($height < 0) { // height is not imporant
                $height = $realHeight = round($currentHeight * $width / $currentWidth);
            }
    
            if ($cut) {
                $kw = $currentWidth / $width;
                $kh = $currentHeight / $height;
    
                $k = $kw < $kh ? $kw : $kh;
    
                $realWidth = round($currentWidth / $k);
                $realHeight = round($currentHeight / $k);
    
                if ($kh < $kw) {
                    $realX = round(($realWidth - $width) / 2) * $k;
                } else {
                    $realY = round(($realHeight - $height) / 2) * $k;
                }
            }
    
            $virtual = imagecreatetruecolor($width, $height);
            imagealphablending($virtual, false);
            $col = imagecolorallocatealpha($virtual, 0, 0, 0, 127);
            imagefill($virtual, 0, 0, $col);
            imagesavealpha($virtual, true);
    
            imagecopyresampled($virtual, $source, 0, 0, $realX, $realY, $realWidth, $realHeight, $currentWidth, $currentHeight);
    
            // create file
            imagepng($virtual, $thumbFilename);
    
            return $thumbFilename;
        }
    }
    

    Usage:

    $image = new Image();
    $image->filename = "image.jpeg"; // really stored in 'data/files/image.jpg', let's say 300x400px
    $image->caption = "My Image";
    
    // get thumb 50x50: left and right parts of image will be cut off
    echo $image->htmlTag(50, 50);
    
    // get thumb of width 100 (height does not matter, keep proportions)
    echo $image->htmlTag(100, -1);
    
    // get thumb of height 100 (width does not matter, keep proportions)
    echo $image->htmlTag(-1, 100);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to set the size of the background image with CSS? I
Is it possible to get the size(in bytes) of a Session object after storing
I am using imagekit to handle custom size of uploaded images. While it works
Is it possible to downsize an image in html while keeping proportions? I know
I've been trying to figure out how to resize an uploaded image in PHP
I need to have full original image of any size, resized in to a
Is it possible to improve Website background image to load faster that it is.
Possible Duplicate: Size of character ('a') in C/C++ Can someone explain why in C
Possible Duplicate: C++: What is the size of an object of an empty class?
Possible Duplicate: when do we need to pass the size of array as a

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.