Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-28-2013, 03:37 PM   PM User | #1
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 806
Thanks: 92
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
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.
tomharto is offline   Reply With Quote
Old 02-28-2013, 03:49 PM   PM User | #2
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 624
Thanks: 16
Thanked 70 Times in 70 Posts
Arcticwarrio is on a distinguished road
how are you setting max width?
__________________
There are 10 types of people on CodingForums,
Those who understand Binary and those who dont.
Arcticwarrio is offline   Reply With Quote
Old 02-28-2013, 03:54 PM   PM User | #3
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 624
Thanks: 16
Thanked 70 Times in 70 Posts
Arcticwarrio is on a distinguished road
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.
Arcticwarrio is offline   Reply With Quote
Old 02-28-2013, 04:02 PM   PM User | #4
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 806
Thanks: 92
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
I didn't think it would be that simple , i'll give it a try now, thanks
tomharto is offline   Reply With Quote
Old 02-28-2013, 09:36 PM   PM User | #5
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
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.
__________________
For professional Hosting and Web design.....


NetEssentials.co.uk

Last edited by Redcoder; 02-28-2013 at 09:38 PM..
Redcoder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:07 AM.


Advertisement
Log in to turn off these ads.