Voting

: min(five, eight)?
(Example: nine)

The Note You're Voting On

bobbyboyojones at hotmail dot com
17 years ago
I hated that enlarging an image resulted in giant pixels rather than a smoother look, so I wrote this function. It takes longer, but gives a much nicer look.

<?php

function imagecopyresampledSMOOTH(&$dst_img, &$src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $mult=1.25){
// don't use a $mult that's too close to an int or this function won't make much of a difference

$tgt_w = round($src_w * $mult);
$tgt_h = round($src_h * $mult);

// using $mult <= 1 will make the current step w/h smaller (or the same), don't allow this, always resize by at least 1 pixel larger
if($tgt_w <= $src_w){ $tgt_w += 1; }
if(
$tgt_h <= $src_h){ $tgt_h += 1; }

// if the current step w/h is larger than the final height, adjust it back to the final size
// this check also makes it so that if we are doing a resize to smaller image, it happens in one step (since that's already smooth)
if($tgt_w > $dst_w){ $tgt_w = $dst_w; }
if(
$tgt_h > $dst_h){ $tgt_h = $dst_h; }

$tmpImg = imagecreatetruecolor($tgt_w, $tgt_h);

imagecopyresampled($tmpImg, $src_img, 0, 0, $src_x, $src_y, $tgt_w, $tgt_h, $src_w, $src_h);
imagecopy($dst_img, $tmpImg, $dst_x, $dst_y, 0, 0, $tgt_w, $tgt_h);
imagedestroy($tmpImg);

// as long as the final w/h has not been reached, reep on resizing
if($tgt_w < $dst_w OR $tgt_h < $dst_h){
imagecopyresampledSMOOTH($dst_img, $dst_img, $dst_x, $dst_y, $dst_x, $dst_y, $dst_w, $dst_h, $tgt_w, $tgt_h, $mult);
}
}

?>

<< Back to user notes page

To Top