Enjoy an ad free experience by logging in. Not a member yet?
Register .
02-28-2013, 03:37 PM
PM User |
#1
Regular Coder
Join Date: Jul 2010
Location: Sheffield
Posts: 806
Thanks: 92
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_orig , 0 , 0 , 0 , 0 , $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.
02-28-2013, 03:49 PM
PM User |
#2
Regular Coder
Join Date: May 2012
Location: UK
Posts: 624
Thanks: 16
Thanked 70 Times in 70 Posts
how are you setting max width?
__________________
There are 10 types of people on CodingForums,
Those who understand Binary and those who dont.
02-28-2013, 03:54 PM
PM User |
#3
Regular Coder
Join Date: May 2012
Location: UK
Posts: 624
Thanks: 16
Thanked 70 Times in 70 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_orig , 0 , 0 , 0 , 0 , $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.
02-28-2013, 04:02 PM
PM User |
#4
Regular Coder
Join Date: Jul 2010
Location: Sheffield
Posts: 806
Thanks: 92
Thanked 18 Times in 18 Posts
I didn't think it would be that simple
, i'll give it a try now, thanks
02-28-2013, 09:36 PM
PM User |
#5
Regular Coder
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 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 ..
Jump To Top of Thread
Thread Tools
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
HTML code is Off
All times are GMT +1. The time now is 06:07 AM .
Advertisement
Log in to turn off these ads.