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
4 2554
<img src="as.jpg" width=30 hight = 30 >
if the set the width and hight then display immage automatically resize and dipalying well
vssp
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:
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 -
<?php
-
// The file
-
$filename = 'test.jpg';
-
-
// Set a maximum height and width
-
$width = 200;
-
$height = 200;
-
-
// Content type
-
header('Content-type: image/jpeg');
-
-
// Get new dimensions
-
list($width_orig, $height_orig) = getimagesize($filename);
-
-
$ratio_orig = $width_orig/$height_orig;
-
-
if ($width/$height > $ratio_orig) {
-
$width = $height*$ratio_orig;
-
} else {
-
$height = $width/$ratio_orig;
-
}
-
-
// Resample
-
$image_p = imagecreatetruecolor($width, $height);
-
$image = imagecreatefromjpeg($filename);
-
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
-
-
// Output
-
imagejpeg($image_p, null, 100);
-
?>
-
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.
" 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 -
<?php
-
// The file
-
$filename = 'test.jpg';
-
-
// Set a maximum height and width
-
$width = 200;
-
$height = 200;
-
-
// Content type
-
header('Content-type: image/jpeg');
-
-
// Get new dimensions
-
list($width_orig, $height_orig) = getimagesize($filename);
-
-
$ratio_orig = $width_orig/$height_orig;
-
-
if ($width/$height > $ratio_orig) {
-
$width = $height*$ratio_orig;
-
} else {
-
$height = $width/$ratio_orig;
-
}
-
-
// Resample
-
$image_p = imagecreatetruecolor($width, $height);
-
$image = imagecreatefromjpeg($filename);
-
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
-
-
// Output
-
imagejpeg($image_p, null, 100);
-
?>
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
| |