Page 1 of 1

Resize an image without dithering or loss of resolution..

Posted: Thu Aug 28, 2003 6:02 am
by agadgil
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?

Posted: Thu Aug 28, 2003 9:08 am
by phice
Would you want this done in PHP? If not, you can do it in the <img tag, by setting either the height or the width, in which will change the opposite setting to make it look exactly the same thing.

Hopefully that wasn't too confusing.

Posted: Thu Aug 28, 2003 10:12 am
by Orkan
To done this in php I use this script:

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);
?>
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.

Posted: Thu Aug 28, 2003 10:23 am
by Orkan
Oh! and another one!
If you want to limit width only - set $wd=1; (like now)
But if you want limit height too - set $wd=0; (then your pictures height & width will be not greater than the limit).

P.S.: it resizes image proportionally

Posted: Thu Aug 28, 2003 11:31 am
by Unipus
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.

class

Posted: Thu Aug 28, 2003 2:11 pm
by phpScott
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

Posted: Thu Aug 28, 2003 8:54 pm
by fractalvibes
Resizing upward with fractal images seems to cause a pixelated effect sometimes, it seems.

fv

Posted: Thu Aug 28, 2003 9:01 pm
by McGruff
Check out the user comments in php manual for a bicubic resampler.

I should say that I've never actually messed around with php image fns and my solicitor has advised me to state that I can't be held responsible for any loss or damage resulting from bad advice :D

Posted: Fri Aug 29, 2003 5:06 am
by timj
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.