Resize an image without dithering or loss of resolution..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
agadgil
Forum Newbie
Posts: 7
Joined: Thu Aug 28, 2003 6:02 am

Resize an image without dithering or loss of resolution..

Post 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?
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post 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.
Image Image
User avatar
Orkan
Forum Commoner
Posts: 32
Joined: Sun Aug 24, 2003 9:07 am
Location: Ukraine
Contact:

Post 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.
User avatar
Orkan
Forum Commoner
Posts: 32
Joined: Sun Aug 24, 2003 9:07 am
Location: Ukraine
Contact:

Post 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
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post 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.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

class

Post 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
fractalvibes
Forum Contributor
Posts: 335
Joined: Thu Sep 26, 2002 6:14 pm
Location: Waco, Texas

Post by fractalvibes »

Resizing upward with fractal images seems to cause a pixelated effect sometimes, it seems.

fv
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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
timj
Forum Newbie
Posts: 14
Joined: Tue Aug 26, 2003 8:00 am
Location: Belgium

Post 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.
Post Reply