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
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    822
    Thanks
    93
    Thanked 18 Times in 18 Posts

    Help tweaking this image resizing script

    I have this image resizing script

    PHP Code:
    $size=GetimageSize($this->file['tmp_name']);
                if (
    $size[0] > $this->maxWidth)
                {
                    
    $height=round($this->maxWidth*$size[1]/$size[0]);
                    switch(
    $this->file['type'])
                    {
                        case 
    'image/jpeg':
                            
    $images_orig imagecreatefromjpeg($this->file['tmp_name']);
                            break;
                        case 
    'image/jpg':
                            
    $images_orig imagecreatefromjpeg($this->file['tmp_name']);
                            break;
                        case 
    'image/png':
                            
    $images_orig imagecreatefrompng($this->file['tmp_name']);
                            break;
                        case 
    'image/gif':
                            
    $images_orig imagecreatefromgif($this->file['tmp_name']);
                            break;
                    }

                    
    $photoX ImagesX($images_orig);
                    
    $photoY ImagesY($images_orig);
                    
    $images_fin imagecreatetruecolor($this->maxWidth$height);
                    
    imagecopyresampled($images_fin$images_orig0000$this->maxWidth+1$height+1$photoX$photoY);

                    switch(
    $this->file['type'])
                    {
                        case 
    'image/jpeg':
                            
    imagejpeg($images_fin,'images/'.$this->file['name']);
                            break;
                        case 
    'image/jpg':
                            
    imagejpeg($images_fin,'images/'.$this->file['name']);
                            break;
                        case 
    'image/png':
                            
    imagepng($images_fin,'images/'.$this->file['name']);
                            break;
                        case 
    'image/gif':
                            
    imagegif($images_fin,'images/'.$this->file['name']);
                            break;
                    }

                    
    imagedestroy($images_orig);
                    
    imagedestroy($images_fin); 
    It re-sizes the image if the width is above a certain value, but it also like it to check against the height too. So say if the max width is set to 500 and max height is set to 300 it would re-size a 600 x 600 image to 300 x 300 as it would currently re-size it to 500 x 500.

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    how are you setting max width?
    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 Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    PHP Code:
    $size=GetimageSize($this->file['tmp_name']);
                if (
    $size[0] > $this->maxWidth){
                    if (
    $size[1] > $this->maxHeight){
                        
    $width=round($this->maxHeight*$size[0]/$size[1]);
                        
    $height=$this->maxHeight;
                    }else{
                        
    $width=$this->maxWidth;
                        
    $height=round($this->maxWidth*$size[1]/$size[0]);
                    }
                    switch(
    $this->file['type'])
                    {
                        case 
    'image/jpeg':
                            
    $images_orig imagecreatefromjpeg($this->file['tmp_name']);
                            break;
                        case 
    'image/jpg':
                            
    $images_orig imagecreatefromjpeg($this->file['tmp_name']);
                            break;
                        case 
    'image/png':
                            
    $images_orig imagecreatefrompng($this->file['tmp_name']);
                            break;
                        case 
    'image/gif':
                            
    $images_orig imagecreatefromgif($this->file['tmp_name']);
                            break;
                    }

                    
    $photoX ImagesX($images_orig);
                    
    $photoY ImagesY($images_orig);
                    
    $images_fin imagecreatetruecolor($width$height);
                    
    imagecopyresampled($images_fin$images_orig0000$width+1$height+1$photoX$photoY);

                    switch(
    $this->file['type'])
                    {
                        case 
    'image/jpeg':
                            
    imagejpeg($images_fin,'images/'.$this->file['name']);
                            break;
                        case 
    'image/jpg':
                            
    imagejpeg($images_fin,'images/'.$this->file['name']);
                            break;
                        case 
    'image/png':
                            
    imagepng($images_fin,'images/'.$this->file['name']);
                            break;
                        case 
    'image/gif':
                            
    imagegif($images_fin,'images/'.$this->file['name']);
                            break;
                    }
                

                    
    imagedestroy($images_orig);
                    
    imagedestroy($images_fin); 
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #4
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    822
    Thanks
    93
    Thanked 18 Times in 18 Posts
    I didn't think it would be that simple , i'll give it a try now, thanks

  • #5
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    332
    Thanks
    2
    Thanked 47 Times in 46 Posts
    Tom,

    You are using the GD library...if you really want to optimize it, move to ImageMagick - written in C (although GD is written in C too).

    Just a few advantages of ImageMagick: much much less code, Faster.

    Check out this simple resize code for images comparing GD library and ImageMagick.
    Last edited by Redcoder; 02-28-2013 at 09:38 PM.


  •  

    Posting Permissions

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