Resize an image without dithering or loss of resolution..
Moderator: General Moderators
Resize an image without dithering or loss of resolution..
I would like to allow the user to browse in say a .gif file and then allow the user to resize the image without dithering.
Any ideas?
Any ideas?
To done this in php I use this script:
But there is one problem... GD library can't work with GIF, so you can use ImageCreateFromGIF() and then ImagePNG() to convert to PNG... Then it shoul work...
As for me - quality is good.
to use this script save it to some_file.php
<img src="some_file.php?200&images/file.jpg">
will show you 200px width picture. If the original size would be smaller - it will return original picture.
Code: Select all
<?
list($rz,$imgfile) = explode("&",$QUERY_STRING);
$wd=1;
$width_new = $rz;
$height_new = $rz;
if (!is_file("$DOCUMENT_ROOT/$imgfile"))exit;
if (substr($imgfile,-3)=='bmp' OR substr($imgfile,-3)=='gif'):
echo @fread(@fopen("$DOCUMENT_ROOT/$imgfile",'r'), filesize ("$DOCUMENT_ROOT/$imgfile"));
exit;
endif;
$imgfile=$DOCUMENT_ROOT."/".$imgfile;
$size = GetImageSize($imgfile);
$width_old = $size[0];
$height_old = $size[1];
if ($width_old>$width_new or $height_old>$height_new):
if ($width_old>$height_old or $wd):
$height_new = round(round($width_new*$height_old)/$width_old);
if ($height_new>$height_old AND $wd):
$height_new=$height_old;
$width_new=$width_old;
endif;
else:
$width_new = round(($height_new*$width_old)/$height_old);
endif;
else:
$width_new = $width_old;
$height_new = $height_old;
endif;
$type = $size[2];
if ($type==1):
echo @fread(@fopen($imgfile,'r'), filesize ($imgfile));
exit;
endif;
switch ($type) {
case 2: $image=imageCreateFromJPEG($imgfile);
break;
case 3: $image=@imageCreateFromPNG($imgfile);
break;
}
$new_image=@imagecreatetruecolor($width_new, $height_new);
@imagecopyresized($new_image, $image, 0, 0, 0, 0, $width_new, $height_new, $width_old, $height_old);
@imagedestroy($image);
@imageinterlace($new_image,1);
switch ($type) {
case 2:
Header ("Content-type: image/jpeg");
@imageJpeg($new_image);
break;
case 3:
Header ("Content-type: image/png");
@imagePNG($new_image);
break;
}
@imagedestroy($new_image);
?>As for me - quality is good.
to use this script save it to some_file.php
<img src="some_file.php?200&images/file.jpg">
will show you 200px width picture. If the original size would be smaller - it will return original picture.
It's impossible to resize UP without a loss in resolution, save for these new fractal image sizing apps, and even those in my experience aren't terrific.
Also, using the size attributes of the IMG tag to resize an image is a terrible idea as it will produce severe jaggies and distortion in your image (even when sizing down) and also introduces display code to your markup.
Also, using the size attributes of the IMG tag to resize an image is a terrible idea as it will produce severe jaggies and distortion in your image (even when sizing down) and also introduces display code to your markup.
class
I have a class that currently handles jpg's and png's that will take care of shrinking images for you if you want I can send it to you.
The user can either set the hieght and width or shrink it keeping the aspect ratio.
phpScott
The user can either set the hieght and width or shrink it keeping the aspect ratio.
phpScott
-
fractalvibes
- Forum Contributor
- Posts: 335
- Joined: Thu Sep 26, 2002 6:14 pm
- Location: Waco, Texas
ImageMagick is a great tool that can easily be used (either from the command line or via the special WebMagick interface) to resize images, even on the fly. This will be some considerable load on your server however (since the .gif or .jpg files are actually rewritten or changed).
Another disadvantage can be that it requires a bunch of graphic libraries to handle all kinds of images, but you can just stick to gif and jpeg as well.
Just type ImageMagick in Google to find it.
Another disadvantage can be that it requires a bunch of graphic libraries to handle all kinds of images, but you can just stick to gif and jpeg as well.
Just type ImageMagick in Google to find it.
