473,383 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Image resizing

I need better image resizing code can any one assist me
Aug 17 '07 #1
4 1198
Atli
5,058 Expert 4TB
Hi, sridhar, and welcome to TSDN!

Take a look at this thread. It may be just what you need.

If not, post back here with a little more details on you problem and perhaps the code that you need fixed, and we well do what we can to help you.
Aug 17 '07 #2
I've actually been very happy with the following [PHP]function createthumb($filename,$target,$newwidth,$newheight )
{

// Get new sizes
list($width, $height) = getimagesize($filename);

// Get Proportional Dimensions
if ($width > $height) {
$thumb_w=$newwidth;
$thumb_h=$height*($newheight/$width);
}
if ($width < $height) {
$thumb_w=$width*($newwidth/$height);
$thumb_h=$newheight;
}
if ($width == $height) {
$thumb_w=$newwidth;
$thumb_h=$newheight;
}

// Center Image
$x = ( $newwidth - $thumb_w ) / 2;
$y = ( $newheight - $thumb_h ) / 2;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Change Background
$white = imagecolorallocate($thumb, 255, 255, 255);
imagefill($thumb, 0, 0, $white);

// Resize
imagecopyresized($thumb, $source, $x, $y, 0, 0, $thumb_w, $thumb_h, $width, $height);

// Output
imagejpeg($thumb, $target);

// Free Memory
imagedestroy($thumb);
imagedestroy($source);

}[/PHP]
Aug 17 '07 #3
Atli
5,058 Expert 4TB
That looks all right.
I have a couple of thoughts though.

First, your if statements, they are all checking the same variables, so it would be far more efficient to use "if..else if..else" rather than three separate if statements.
For example:
Expand|Select|Wrap|Line Numbers
  1. // ----
  2. // Scenario one, three if statements
  3. if($x > $y) { // Even if this is true.. }
  4. if($x < $y) { // this one will also be checked }
  5. if($x == $y) { // as will this }
  6.  
  7. // ----
  8. // Scenario two, if.. else if
  9. if($x > $y) { // If this is true...}
  10. else if($x < $y) { // this will be skipped! }
  11. else { // As will this! }
  12.  
  13. /* ----
  14.  So using scenario two; two out of three boolean expressions will not be executed, most of the time, giving a slight performance boost 
  15. (not notable in this example but... still)
  16. */
  17.  
Second. You use the function imagecopyresized() which, granted, will do exactly what you want.
But, if you replace it with the imagecopyresampled function, your image will not only be resized, it will also be re sampled, increasing its quality.

Finally. You import both the new width and the new height, scale the image down and then center it on a canvas that may not be close to the original image aspect ratio.
Have you considered creating a thumb using the original aspect ratio and then use HTML or CSS to position it when you output it?
That way there will be no white background in the thumb itself, which will allow you much more freedom when you display it.
Imagine if you decide, 100.000 images from now, that you want to change the background color of your thumbs. Your way, it would not be possible without re-creating every single thumb. This way it would be as simple as changing a single CSS value.
Aug 17 '07 #4
pbmods
5,821 Expert 4TB
Heya, Sridhar.

You might also want to have a look at this article.
Aug 18 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Jim | last post by:
I've heard that resizing images through PHP (either GD2 or ImageMagick) is a processor intensive exercise. I'm setting up a site where users will be uploading up to 10 images along with the details...
6
by: bissatch | last post by:
Hi, I have a collection of images stored in a DB. They are there for the purpose of a news system. When the user views the homepage it will diplay cropped versions of the news where the user...
3
by: Zahid Khan | last post by:
I need little help in my situation. I am reading a graphic file (jpg) from disk and then resizing it and save resized image. What happens, it gets blured, I want to retain same quality so that...
10
by: David W. Simmonds | last post by:
I have a DataList control that has an Image control in the ItemTemplate. I would like to resize the image that goes into that control. I have a series of jpg files that are full size, full...
8
by: berkshire | last post by:
Hi, Anyone know of a script out there that can resize images and not sacrifice image quality? I've been using phpthumb (http://phpthumb.sourceforge.net/) but when compared to an image resized...
9
by: tshad | last post by:
Is there a way to display images (imageButtons or linkbuttons for instance) as a max size (200px by 50px) and not have it stretch the image? What I want to be able to do is limit the real estate...
9
by: kombu67 | last post by:
I'm reading a series of images from a MS SQL table and saving them to directory. These are staff ID pictures from our security card app. Once I've extracted the ID photo from the security app to...
10
by: mishrarajesh44 | last post by:
hii all, I am facing a problem currently.. i have a script for image uploading and resizing.. the image uploading takes place properly for every size images.. but, the resizing works for...
11
by: shapper | last post by:
Hello, I am displaying an image on a few pages. The image size is 50 px height and 50 px width. In some pages I need the image to be 30x30 px in others 40x40 px and in others 50x50px. Can I...
14
anfetienne
by: anfetienne | last post by:
hi.....i have this script (below #1) that is linked to another php file SimpleImage.php (#2) im trying to get it to work on my uploaded images but it keeps coming up errors.....i haven't altered...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.