473,394 Members | 1,663 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,394 software developers and data experts.

image resizing in php

hi all

In my program i have to upload some gif,jpg like images and store these images in a floder. when i am display these images by giving the width=100 and height=100 the image is not dispalying clearly.

i.e. i want how i resize an image with the same clarity.


thanks in advance.

bye
Oct 31 '06 #1
4 2554
vssp
268 100+
<img src="as.jpg" width=30 hight = 30 >

if the set the width and hight then display immage automatically resize and dipalying well

vssp
Oct 31 '06 #2
ronverdonk
4,258 Expert 4TB
When you resize an image, you have to keep the ratio of width and height intact, otherwise the result gets distorted (stretched). So if you decrease the width by 30% you must also decrease the height by the same percentage.

If you want to get the best shrink result, you'd have to use GD to do that dynamically. Second best is to set the sizes in the <img> statement.

Following is a function I use to display images without actuallly resizing them via GD. It takes the desired height of the (shrunken) image, calculates the required width and displays it in an img tag. Call the function passing it the image file name, the required height, and the title which is displayed when you set the cursor on it. You can easily adapt it if you want the width to be the determining factor for shrinking. Have fun.
[php]function showImage($img, $height, $title) {
// --- determine width and height of the original image
list($orig_width, $orig_height) = getimagesize($img);
// --- calculate the compression ratio of thumbnail height and original height
$thumb_ratio = $height / $orig_height;
// --- re-calculate new thumbnail width by using the compression ratio
$width = $orig_width * $thumb_ratio;
// --- show the image
echo "<img src='$img' width='$width' height='$height' border='0' title='$title'>" ;
}[/php]

Ronald :cool:
Oct 31 '06 #3
steven
143 100+
There's actually some code to do this in the PHP manual (if you had read it)...

You want to resize the image with resampling, so that it doesn't look distorted and also doesn't have as large a filesize and a larger image just displayed smaller through html width and height img attributes.

http://fr.php.net/imagecopyresampled

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // The file
  3. $filename = 'test.jpg';
  4.  
  5. // Set a maximum height and width
  6. $width = 200;
  7. $height = 200;
  8.  
  9. // Content type
  10. header('Content-type: image/jpeg');
  11.  
  12. // Get new dimensions
  13. list($width_orig, $height_orig) = getimagesize($filename);
  14.  
  15. $ratio_orig = $width_orig/$height_orig;
  16.  
  17. if ($width/$height > $ratio_orig) {
  18.    $width = $height*$ratio_orig;
  19. } else {
  20.    $height = $width/$ratio_orig;
  21. }
  22.  
  23. // Resample
  24. $image_p = imagecreatetruecolor($width, $height);
  25. $image = imagecreatefromjpeg($filename);
  26. imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
  27.  
  28. // Output
  29. imagejpeg($image_p, null, 100);
  30. ?> 
  31.  
The code is quite easy to follow. Oh and in future, please at least look at the manual before asking. It doesn't hurt to keep the online manual bookmarked and to occasionally have a look through it. If you see code you don't understand in the example on that page, simply pop the function name that you're not familiar with, into the search box at the top of the page and have a look.

Good luck.
Oct 31 '06 #4
exoskeleton
104 100+
"Oh and in future, please at least look at the manual before asking", you shouldn't tell him/her this phrase pal, you may hurt his/her feelings pal...WATCH YOUR WORD..this is a community where questions are welcome...

There's actually some code to do this in the PHP manual (if you had read it)...

You want to resize the image with resampling, so that it doesn't look distorted and also doesn't have as large a filesize and a larger image just displayed smaller through html width and height img attributes.

http://fr.php.net/imagecopyresampled

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // The file
  3. $filename = 'test.jpg';
  4.  
  5. // Set a maximum height and width
  6. $width = 200;
  7. $height = 200;
  8.  
  9. // Content type
  10. header('Content-type: image/jpeg');
  11.  
  12. // Get new dimensions
  13. list($width_orig, $height_orig) = getimagesize($filename);
  14.  
  15. $ratio_orig = $width_orig/$height_orig;
  16.  
  17. if ($width/$height > $ratio_orig) {
  18.    $width = $height*$ratio_orig;
  19. } else {
  20.    $height = $width/$ratio_orig;
  21. }
  22.  
  23. // Resample
  24. $image_p = imagecreatetruecolor($width, $height);
  25. $image = imagecreatefromjpeg($filename);
  26. imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
  27.  
  28. // Output
  29. imagejpeg($image_p, null, 100);
  30. ?> 
  31.  
The code is quite easy to follow. Oh and in future, please at least look at the manual before asking. It doesn't hurt to keep the online manual bookmarked and to occasionally have a look through it. If you see code you don't understand in the example on that page, simply pop the function name that you're not familiar with, into the search box at the top of the page and have a look.

Good luck.
Nov 4 '06 #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...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.