Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Resizing Images

  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    87
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Question Resizing Images

    I want to achieve the following task (not sure whether in PHP, HTML or CSS): If an image exceeds a certain width, it resizes in a way that doesn't compress the image. If the image does not exceed the width, it displays in its original dimensions.

    Someone explain to me how to achieve the task. Please give code examples. Thank you.

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    did you get ImageMagik the other week? that can manipulate omages in almost any way you need it to
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #3
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    i wrote a script doing pretty much the same thing for entire directory with pics some time ago.
    search this forum and you should find it

    -------------------------------------------------------------------------------
    "Real Programmers can write assembly code in any language" - Larry Wall

  • #4
    Banned
    Join Date
    Mar 2013
    Posts
    139
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Quote Originally Posted by Thuita Maina View Post
    I want to achieve the following task (not sure whether in PHP, HTML or CSS):
    Do it server side (PHP or similar). Don't do using javascript.

    Courtesy of Mr. Google.

    PHP Code:
    <?php
    class Image_Batch_Processor {

        private 
    $srcDir;
        private 
    $outDir;
        private 
    $maxWidth;
        private 
    $maxHeight;

        
    /* -----------------------------------------------------------
         * Class Constructor
          ----------------------------------------------------------- */

        
    public function __construct($srcDir$outDir$maxWidth$maxHeight) {
            
    $this->srcDir      $srcDir;
            
    $this->outDir      $outDir;
            
    $this->maxWidth    $maxWidth;   
            
    $this->maxHeight   $maxHeight;
        }

        
    /* -----------------------------------------------------------
         * Accessors
          ----------------------------------------------------------- */
        
    public function setSrcDir($srcDir) {
            
    $this->srcDir      $srcDir;
        }
        public function 
    setOutDir($outDir) {
            
    $this->outDir      $outDir;
        }
        public function 
    setMaxWidth($maxWidth) {
            
    $this->maxWidth      $maxWidth;
        }
        public function 
    setMaxheight($maxHeight) {
            
    $this->maxHeight      $maxHeight;
        }
         public function 
    getSrcDir($srcDir) {
            return 
    $this->srcDir;
        }
        public function 
    getOutDir($outDir) {
            return 
    $this->outDir;
        }
        public function 
    getMaxWidth($maxWidth) {
            return 
    $this->maxWidth;
        }
        public function 
    getMaxheight($maxHeight) {
            return 
    $this->maxHeight;
        }
         
    /* -----------------------------------------------------------
         * Class Methods
          ----------------------------------------------------------- */
        
    public function resize($imgsArr) {
            foreach(
    $imgsArr as $file) {
            
    $image $this->srcDir.$file;    
            
    $outFile $this->outDir.$file;

            
    //get the original image attributes
            
    $size   GetImageSize($image);
            
    $width   $size[0];
            
    $height  $size[1];
            
    $imageType  $size[2];

            
    //scaling factors
            
    $xRatio $this->maxWidth $width;  
            
    $yRatio $this->maxHeight $height;
     
            
    //calculate the new width and height
            
    if($width <= $this->maxWidth && $height <= $this->maxHeight) {//image does not need resizing
                 
    $toWidth  $width;
                    
    $toHeight  $height;
            }    else if(
    $xRatio $height $this->maxHeight)    {
                     
    $toHeight round($xRatio $height);
                    
    $toWidth  $this->maxWidth;        
            }    else {
                     
    $toWidth round($yRatio $width);
                    
    $toHeight  $this->maxHeight;
            }
            
            
    //create the resized image
            //Type of image  1=GIF  2=JPG 3=PNG 4=SWF 5=PSD 6=BMP 7=TIFF(intel byte order) 8=TIFF(motorola byte order) 9=JPC 10=JP2 11=JPX 12=JB2 13=SWC 14=IFF 15=WBMP 16=XBM
            
    $newImage ImageCreateTrueColor($toWidth,$toHeight); //create the new image canvas
            
    switch ($imageType)    {    
                case 
    1:  //gif file
                 
    $oldImage ImageCreateFromGif($image);
                break;
     
                case 
    3:  //png file
                 
    $oldImage ImageCreateFromPng($image);
                break; 
     
                default: 
    //jpg file
                 
    $oldImage ImageCreateFromJpeg($image);
                break;      
            }
     
            
    ImageCopyResampled($newImage,$oldImage,0,0,0,0,$toWidth,$toHeight,$width,$height); //resize the new image
                //output the new resized image
            
    switch ($imageType)    {    
                case 
    1:  //gif file
                
    header('Content-type: image/gif');
                
    ImageGif($newImage,$outFile);
                break; 
     
                case 
    3:  //png file
                
    header('Content-type: image/png');
                
    ImagePng($newImage,$outFile);
                break; 
     
                default: 
    //jpg file
                
    header('Content-type: image/jpeg');
                
    ImageJpeg($newImage,$outFile);  
                break;      
            }
     
              
    //clean up resources
            
    ImageDestroy($oldImage);
            
    ImageDestroy($newImage);
        }  
        }
        
    //end of class
    //----------------------------------------------------------

    ?>
    It resizes (maintaining original aspect ratio) an array of images.

  • #5
    New Coder
    Join Date
    Sep 2012
    Posts
    87
    Thanks
    18
    Thanked 0 Times in 0 Posts
    knightCoder, I'm new to PHP programming, so I don't how to implement it in the code you've given. Please help.
    Below is the PHP code displaying images on my webpage from a link in a database. I'd love to know how I'd incorporate the code you've given. Thank you.
    Code:
    echo '<img src = "' . $row['Photo_link'] . '" alt = "" />


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •