PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

imagecopyresized> <imagecopymergegray
Last updated: Fri, 22 Aug 2008

view this page in

imagecopyresampled

(PHP 4 >= 4.0.6, PHP 5)

imagecopyresampledCopie, redimensionne, rééchantillonne une image

Description

bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )

imagecopyresampled() copie une zone rectangulaire de l'image src_im vers l'image dst_im . Durant la copie, la zone est rééchantillonnée de manière à conserver la clarté de l'image durant une réduction.

En d'autres termes, imagecopyresampled() prendra une forme rectangulaire src_image d'une largeur de src_w et d'une hauteur src_h à la position (src_x ,src_y ) et le placera dans une zone rectangulaire dst_image d'une largeur de dst_w et d'une hauteur de dst_h à la position (dst_x ,dst_y ).

Si les hauteurs et largeurs des source et destination diffèrent, l'image copiée sera étirée de manière appropriée. Les coordonnées sont celles du coin supérieur gauche. imagecopyresampled() peut servir à copier des zones d'une image vers elle-même, (si dst_image est la même que src_image ) mais si les régions se chevauchent, les résultats sont imprévisibles.

Liste de paramètres

dst_im

Lien vers la ressource de l'image de destination

src_im

Lien vers la ressource de l'image source

dst_x

X : coordonnées du point de destination

dst_y

Y : coordonnées du point de destination

src_x

X : coordonnées du point source

src_y

Y : coordonnées du point source

dst_w

Largeur de la destination

dst_h

Hauteur de la destination

src_w

Largeur de la source

src_h

Hauteur de la source

Valeurs de retour

Cette fonction retourne TRUE en cas de succès, FALSE en cas d'échec.

Exemples

Exemple #1 Exemple simple

Cet exemple redimensionne une image à la moitié de sa taille originale.

<?php
// Le fichier
$filename 'test.jpg';
$percent 0.5;

// Content type
header('Content-type: image/jpeg');

// Calcul des nouvelles dimensions
list($width$height) = getimagesize($filename);
$new_width $width $percent;
$new_height $height $percent;

// Redimensionnement
$image_p imagecreatetruecolor($new_width$new_height);
$image imagecreatefromjpeg($filename);
imagecopyresampled($image_p$image0000$new_width$new_height$width$height);

// Affichage
imagejpeg($image_pnull100);
?>

L'exemple ci-dessus va afficher quelque chose de similaire à :

Exemple #2 Redimensionnement proportionnel d'une image

Cet exemple affichera une image avec une largeur ou une hauteur maximale de 200 pixels.

<?php
// Le fichier
$filename 'test.jpg';

// Définition de la largeur et de la hauteur maximale
$width 200;
$height 200;

// Content type
header('Content-type: image/jpeg');

// Cacul des nouvelles 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;
}

// Redimensionnement
$image_p imagecreatetruecolor($width$height);
$image imagecreatefromjpeg($filename);
imagecopyresampled($image_p$image0000$width$height$width_orig$height_orig);

// Affichage
imagejpeg($image_pnull100);
?>

L'exemple ci-dessus va afficher quelque chose de similaire à :

Notes

Note: Il y a un problème dû aux limitations de la taille de la palette (255 + 1 couleurs différentes). Filtrer ou rééchantillonner une image demande plus de 255 couleurs, une approximation est alors utilisée pour calculer le nouveau nombre de couleurs. Avec une palette, si une nouvelle couleur ne peut être allouée, la couleur la plus proche (en théorie) est utilisée. Ce n'est pas toujours la couleur la plus proche visuellement. Cela peut générer des problèmes étranges, comme des images blanches. Pour éviter ce problème, passez en image TrueColor, comme celles générée par la fonction imagecreatetruecolor().

Voir aussi

imagecopyresized()



imagecopyresized> <imagecopymergegray
Last updated: Fri, 22 Aug 2008
 
add a note add a note User Contributed Notes
imagecopyresampled
crash
12-Aug-2008 03:49
The suggestion of converting a gif to a png to retain transparency was useful. However the steps used to maintain the transparency work for a gif as well, so the conversion step is not necessary. Just do everything the same, and save as a gif anyway. i.e.

<?php
$g_iw is
new image width
$g_ih is
new image height

$img_src
=imagecreatefromgif($g_srcfile);
$img_dst=imagecreatetruecolor($g_iw,$g_ih);

//preserve alpha
imagecolortransparent($img_dst, imagecolorallocate($img_dst, 0, 0, 0));
imagealphablending($img_dst, false);
imagesavealpha($img_dst, true);
imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $g_iw, $g_ih, $g_is[0], $g_is[1]);

imagegif($img_dst, $g_dstfile);
imagedestroy($img_dst);
?>
swizec at swizec dot com
17-Jul-2008 11:20
Wrote a function for sanitising user uploaded images. It saves the native image in size roughly 800x600 so it still fits on most screens when opened, makes a desired size thumbnail and turns all images into high quality jpegs for smaller bandwidth use.

Thumbnails are made in a similar way a designer would make them in photoshop, first resize the most troublesome dimension to desired size, then crop the rest out so the image retains proportions and most of it ends up in the thumbnail.

<?php

// $image is $_FILES[ <image name> ]
// $imageId is the id used in a database or wherever for this image
// $thumbWidth and $thumbHeight are desired dimensions for the thumbnail
function processImage( $image, $imageId, $thumbWidth, $thumbHeight )
{
   
$type = $image[ 'type' ];
   
$galleryPath = 'images/collection/';
   
    if (
strpos( $type, 'image/' ) === FALSE )
    {
// not an image
       
return FALSE;
    }
   
$type = str_replace( 'image/', '', $type );
   
$createFunc = 'imagecreatefrom' . $type;
   
   
$im = $createFunc( $image[ 'tmp_name' ] );
   
   
$size = getimagesize( $image[ 'tmp_name' ] );
   
   
$w = $size[ 0 ];
   
$h = $size[ 1 ];
    if (
$w > 800 || $h > 600 )
    {
// we make sure the image isn't too huge
       
if ( $w > 800 )
        {
           
$nw = 800;
           
$nh = ceil( $nw*($h/$w) );
        }elseif(
$h > 600 )
        {
           
$nh = 600;
           
$nw = ceil( $nh*($w/$h) );
        }
       
       
$im2 = imagecreatetruecolor( $nw, $nh );
       
imagecopyresampled( $im2, $im, 0, 0, 0, 0, $nw, $nh, $w, $h );
       
imagedestroy( $im );
       
       
$im = $im2;
       
$w = $nw;
       
$h = $nh;
    }
   
   
// create thumbnail
   
$tw = $thumbWidth;
   
$th = $thumbHeight;
   
$imT = imagecreatetruecolor( $tw, $th );
   
    if (
$tw/$th > $th/$tw )
    {
// wider
       
$tmph = $h*($tw/$w);
       
$temp = imagecreatetruecolor( $tw, $tmph );
       
imagecopyresampled( $temp, $im, 0, 0, 0, 0, $tw, $tmph, $w, $h ); // resize to width
       
imagecopyresampled( $imT, $temp, 0, 0, 0, $tmph/2-$th/2, $tw, $th, $tw, $th ); // crop
       
imagedestroy( $temp );
    }else
    {
// taller
       
$tmpw = $w*($th/$h );
       
$imT = imagecreatetruecolor( $tmpw, $th );
       
imagecopyresampled( $imT, $im, 0, 0, 0, 0, $tmpw, $h, $w, $h ); // resize to height
       
imagecopyresampled( $imT, $temp, 0, 0, $tmpw/2-$tw/2, 0, $tw, $th, $tw, $th ); // crop
       
imagedestroy( $temp );
    }
   
   
// save the image
   
imagejpeg( $im, $galleryPath . $imgid . '.jpg', 100 );
   
imagejpeg( $imT, $galleryPath . $imgid . '_thumb.jpg', 100 );
}

?>
mattura gmail com
19-May-2008 07:53
Here's a little function I wrote to resize images to a maximum dimension - based on what facebook does in the galleries. You put in a source, destination and a maximum dimension in pixels (eg 300), and for example if the image is long and thin, the longest edge will be 300px, yet the image retains proportions. A square image will become 300x300, a 6x4 (landscape) will become 300x200, a 4x6 (portrait) - 200x300 etc.
It works on jpg images, but other formats can easily be added.
<?php
function createThumb($spath, $dpath, $maxd) {
 
$src=@imagecreatefromjpeg($spath);
 if (!
$src) {return false;} else {
 
$srcw=imagesx($src);
 
$srch=imagesy($src);
  if (
$srcw<$srch) {$height=$maxd;$width=floor($srcw*$height/$srch);}
  else {
$width=$maxd;$height=floor($srch*$width/$srcw);}
  if (
$width>$srcw && $height>$srch) {$width=$srcw;$height=$srch;}  //if image is actually smaller than you want, leave small (remove this line to resize anyway)
 
$thumb=imagecreatetruecolor($width, $height);
  if (
$height<100) {imagecopyresized($thumb, $src, 0, 0, 0, 0, $width, $height, imagesx($src), imagesy($src));}
  else {
imagecopyresampled($thumb, $src, 0, 0, 0, 0, $width, $height, imagesx($src), imagesy($src));}
 
imagejpeg($thumb, $dpath);
  return
true;
 }
}
?>
bobbyboyojones at hotmail dot com
18-Mar-2008 08:41
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);
    }
}

?>
arnar at netvistun dot is
16-Mar-2008 07:58
A small thumb script. Lets you specify max height and width. The thumb will always be of a rectangular shape while the image itself retains it's proportions. Very clean.

<?php
// The file
$filename = 'a.jpg';

// Set a maximum height and width
$width = 80;
$height = 80;

$thumbsize = 80;

// 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($thumbsize, $thumbsize);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, -($width/2) + ($thumbsize/2), -($height/2) + ($thumbsize/2), 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, null, 100);
?>
rayg at daylongraphics dot com
07-Mar-2008 11:45
Here's a simple function to resample one JPEG imagefile to another while keeping aspect ratio of the source within the destination's dimensions. You can also tune the allowable distortion if you end up making too many thumbnails with thin blank areas around them. Should work when enlarging images too. Function returns true if it worked, false if not.

function resample_picfile($src, $dst, $w, $h)
{
    // If distortion stretching is within the range below,
    // then let image be distorted.
    $lowend = 0.8;
    $highend = 1.25;

    $src_img = imagecreatefromjpeg($src);
    if($src_img)
    {
        $dst_img = ImageCreateTrueColor($w, $h);
        /* if you don't want aspect-preserved images
to have a black bkgnd, fill $dst_img with the color of your choice here.
        */

        if($dst_img)
        {
            $src_w = imageSX($src_img);
            $src_h = imageSY($src_img);

            $scaleX = (float)$w / $src_w;
            $scaleY = (float)$h / $src_h;
            $scale = min($scaleX, $scaleY);

            $dstW = $w;
            $dstH = $h;
            $dstX = $dstY = 0;

            $scaleR = $scaleX / $scaleY;
            if($scaleR < $lowend || $scaleR > $highend)
            {
                $dstW = (int)($scale * $src_w + 0.5);
                $dstH = (int)($scale * $src_h + 0.5);

                // Keep pic centered in frame.
                $dstX = (int)(0.5 * ($w - $dstW));
                $dstY = (int)(0.5 * ($h - $dstH));
            }
           
            imagecopyresampled(
                $dst_img, $src_img, $dstX, $dstY, 0, 0,
                $dstW, $dstH, $src_w, $src_h);
            imagejpeg($dst_img, $dst);
            imagedestroy($dst_img);
        }
        imagedestroy($src_img);
        return file_exists($dst);
    }
    return false;
}
RandomFeatureRequest
02-Mar-2008 04:51
If anyone felt like implementing Bicubic Sharper and/or Bicubic Smoother, that would seriously rock.
wm at violet dot bg
20-Feb-2008 10:16
This is a fixed version of ImageCopyResampledBicubic posted by liviu.malaescu
The original version wasn't respecting src_x & src_y args

<?php
   
function ImageCopyResampledBicubic(&$dst_image, &$src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)  {
       
// we should first cut the piece we are interested in from the source
       
$src_img = ImageCreateTrueColor($src_w, $src_h);
       
imagecopy($src_img, $src_image, 0, 0, $src_x, $src_y, $src_w, $src_h);

       
// this one is used as temporary image
       
$dst_img = ImageCreateTrueColor($dst_w, $dst_h);

       
ImagePaletteCopy($dst_img, $src_img);
       
$rX = $src_w / $dst_w;
       
$rY = $src_h / $dst_h;
       
$w = 0;
        for (
$y = 0; $y < $dst_h; $y++)  {
           
$ow = $w; $w = round(($y + 1) * $rY);
           
$t = 0;
            for (
$x = 0; $x < $dst_w; $x++)  {
               
$r = $g = $b = 0; $a = 0;
               
$ot = $t; $t = round(($x + 1) * $rX);
                for (
$u = 0; $u < ($w - $ow); $u++)  {
                    for (
$p = 0; $p < ($t - $ot); $p++)  {
                       
$c = ImageColorsForIndex($src_img, ImageColorAt($src_img, $ot + $p, $ow + $u));
                       
$r += $c['red'];
                       
$g += $c['green'];
                       
$b += $c['blue'];
                       
$a++;
                    }
                }
               
ImageSetPixel($dst_img, $x, $y, ImageColorClosest($dst_img, $r / $a, $g / $a, $b / $a));
            }
        }

       
// apply the temp image over the returned image and use the destination x,y coordinates
       
imagecopy($dst_image, $dst_img, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h);

       
// we should return true since ImageCopyResampled/ImageCopyResized do it
       
return true;
    }
?>
Michael Shepanski
15-Jan-2008 07:09
Here is a function I thought I would share that will resample and copy an image with rounded corners.

<?php
/** ------------------------------------------------------------
 * Copy and resample an image with rounded corners.
 * ----------------------------------------------------------- */
function imageRoundedCopyResampled(&$dstimg, &$srcimg, $dstx, $dsty, $srcx,
                                  
$srcy, $dstw, $dsth, $srcw, $srch, $radius) {
   
# Resize the Source Image
   
$srcResized = imagecreatetruecolor($dstw, $dsth);
   
imagecopyresampled($srcResized, $srcimg, 0, 0, $srcx, $srcy,
                      
$dstw, $dsth, $srcw, $srch);
   
# Copy the Body without corners
   
imagecopy($dstimg, $srcResized, $dstx+$radius, $dsty,
             
$radius, 0, $dstw-($radius*2), $dsth);
   
imagecopy($dstimg, $srcResized, $dstx, $dsty+$radius,
             
0, $radius, $dstw, $dsth-($radius*2));
   
# Create a list of iterations; array(array(X1, X2, CenterX, CenterY), ...)
    # Iterations in order are: Top-Left, Top-Right, Bottom-Left, Bottom-Right
   
$iterations = array(
        array(
0, 0, $radius, $radius),
        array(
$dstw-$radius, 0, $dstw-$radius, $radius),
        array(
0, $dsth-$radius, $radius, $dsth-$radius),
        array(
$dstw-$radius, $dsth-$radius, $dstw-$radius, $dsth-$radius)
    );
   
# Loop through each corner 'iteration'
   
foreach($iterations as $iteration) {
        list(
$x1,$y1,$cx,$cy) = $iteration;
        for (
$y=$y1; $y<=$y1+$radius; $y++) {
            for (
$x=$x1; $x<=$x1+$radius; $x++) {
               
# If length (X,Y)->(CX,CY) is less then radius draw the point
               
$length = sqrt(pow(($cx - $x), 2) + pow(($cy - $y), 2));
                if (
$length < $radius) {
                   
imagecopy($dstimg, $srcResized, $x+$dstx, $y+$dsty,
                             
$x, $y, 1, 1);
                }
            }
        }
    }
}
?>
matt1walsh DESPAMMER gmail dot com
20-Nov-2007 04:15
None of the stuff I've seen for resizing transparent GIFs works consistently and yields a good image. The hack I thought of is silly, but it works okay -- convert GIF to PNG. This worked for me.

$g_iw is new image width
$g_ih is new image height

$img_src=imagecreatefromgif($g_srcfile);
$img_dst=imagecreatetruecolor($g_iw,$g_ih);

//preserve alpha
imagecolortransparent($img_dst, imagecolorallocate($img_dst, 0, 0, 0));
imagealphablending($img_dst, false);
imagesavealpha($img_dst, true);
imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $g_iw, $g_ih, $g_is[0], $g_is[1]);

imagepng($img_dst, $g_dstfile);
imagedestroy($img_dst);
Dave McCourt
12-Nov-2007 02:32
This function is taken from lots of web sources so thanks to all for posting. It creates square or landscape thumbnails from .jpgs  from either portait or landscape original images. I decide in advance which way I want the thumbs to display for consistency. I usually sharpen the images as well post-upload, to save on server resources. I can post this code if anyone wants it. I hope this helps someone...

    # create thumbnails from jpgs
    # usage:
    # create_jpgthumb(uploaded file, final file (with path), thumb height, thumb width, jpg quality, scale thumb (true) or fixed size (false);
    function create_jpgthumb($original, $thumbnail, $max_width, $max_height, $quality, $scale = true) {
       
        list ($src_width, $src_height, $type, $w) = getimagesize($original);
       
        if (!$srcImage = @imagecreatefromjpeg($original)) {
            return false;
        }

        # image resizes to natural height and width
        if ($scale == true) {
                       
            if ($src_width > $src_height ) {
                $thumb_width = $max_width;
                $thumb_height = floor($src_height * ($max_width / $src_width));
            } else if ($src_width < $src_height ) {
                $thumb_height = $max_height;
                $thumb_width = floor($src_width * ($max_height / $src_height));
            } else {
                $thumb_width = $max_height;
                $thumb_height = $max_height;
            }

            if (!@$destImage = imagecreatetruecolor($thumb_width, $thumb_height)) {
                return false;
            }
           
            if (!@imagecopyresampled($destImage, $srcImage, 0, 0, 0, 0, $thumb_width, $thumb_height, $src_width, $src_height)) {
                return false;
            }
                       
        # image is fixed to supplied width and height and cropped
        } else if ($scale == false) {
       
            $ratio = $max_width / $max_height;
               
            # thumbnail is landscape
             if ($ratio > 1) {
            
                # uploaded pic is landscape
                if ($src_width > $src_height) {

                    $thumb_width = $max_width;
                    $thumb_height = ceil($max_width * ($src_height / $src_width));
                   
                    if ($thumb_height > $max_width) {
                        $thumb_height = $max_width;
                        $thumb_width = ceil($max_width * ($src_width / $src_height));
                    }

                # uploaded pic is portrait
                } else {
               
                    $thumb_height = $max_width;
                    $thumb_width = ceil($max_width * ($src_height / $src_width));
                   
                    if ($thumb_width > $max_width) {
                        $thumb_width = $max_width;
                        $thumb_height = ceil($max_width * ($src_height / $src_width));
                    }
                   
                    $off_h = ($src_height - $src_width) / 2;
           
                }
   
                if (!@$destImage = imagecreatetruecolor($max_width, $max_height)) {
                    return false;
                }
       
                if (!@imagecopyresampled($destImage, $srcImage, 0, 0, 0, $off_h, $thumb_width, $thumb_height, $src_width, $src_height)) {
                    return false;
                }

            # thumbnail is square
             } else {
            
                if ($src_width > $src_height) {
                    $off_w = ($src_width - $src_height) / 2;
                    $off_h = 0;
                    $src_width = $src_height;
                } else if ($src_height > $src_width) {
                    $off_w = 0;
                    $off_h = ($src_height - $src_width) / 2;
                    $src_height = $src_width;
                } else {
                    $off_w = 0;
                    $off_h = 0;
                }

                if (!@$destImage = imagecreatetruecolor($max_width, $max_height)) {
                    return false;
                }
       
                if (!@imagecopyresampled($destImage, $srcImage, 0, 0, $off_w, $off_h, $max_width, $max_height, $src_width, $src_height)) {
                    return false;
                }

             }
        
                           
        }
       
        @imagedestroy($srcImage);

        if (!@imageantialias($destImage, true)) {
            return false;
        }
       
        if (!@imagejpeg($destImage, $thumbnail, $quality)) {
            return false;
        }
                       
        @imagedestroy($destImage);
       
        return true;
    }
tim dot daldini at gmail dot be
04-Oct-2007 05:57
Tim's function is a whole lot faster, however, the quality setting doesnt seem right since resizing very big images doesnt affect quality of the resulting thumbnails that much on quality 1 for example.

Anyone has figured out how to use a more correct quality setting by comparing image surfaces (for example) basing on Tim's function?

images with a total of 10Megapixels that contain 5Megapixels when resized should use the same quality setting like images that contain 1Megapixels but only 0.5 when resized.

Not hard to understand, but the memoryload would be a lot lower if the function could decide to use a setting of quality 1 automatically when resizing big images to small thumbnails. This would be handy especially when using the function for images of various sizes in the same application.
liviu dot malaescu at gmail dot com
25-Sep-2007 09:37
3800x2500px - 2s
3807x6768 - 4.7s
Tested on a core2duo e4300/1gb ram - only one core used.
It reduces first with nearest-neighbour and then with bicubic on the smaller image.
Thumb quality is fine :)

<?php
$cale_in
= "test.jpg"; //input-file
$cale_out = "test_thumb.jpg"; //output-file
$make_thumbs = "YES";
$thumbsize[0] = 100;
$thumbsize[1] = 75;
ini_set("memory_limit", "134217728"); // needed for huge pictures

// Bicubic resampling, not written by me
function ImageCopyResampleBicubic
(&$dst_img, &$src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)  {
   
ImagePaletteCopy ($dst_img, $src_img);
   
$rX = $src_w / $dst_w;
   
$rY = $src_h / $dst_h;
   
$w = 0;
    for (
$y = $dst_y; $y < $dst_h; $y++)  {
       
$ow = $w; $w = round(($y + 1) * $rY);
       
$t = 0;
        for (
$x = $dst_x; $x < $dst_w; $x++)  {
           
$r = $g = $b = 0; $a = 0;
           
$ot = $t; $t = round(($x + 1) * $rX);
            for (
$u = 0; $u < ($w - $ow); $u++)  {
                for (
$p = 0; $p < ($t - $ot); $p++)  {
                   
$c = ImageColorsForIndex ($src_img,
ImageColorAt ($src_img, $ot + $p, $ow + $u));
                   
$r += $c['red'];
                   
$g += $c['green'];
                   
$b += $c['blue'];
                   
$a++;
                }
            }
           
ImageSetPixel ($dst_img, $x, $y,
ImageColorClosest ($dst_img, $r / $a, $g / $a, $b / $a));
        }
    }
}

function
makeThumb($cale_in)  {
    global
$thumbsize;
   
$sursa = imagecreatefromjpeg($cale_in);
   
$is_jpeg = getimagesize($cale_in);
   
$ws = $is_jpeg[0];
   
$hs = $is_jpeg[1];
    if(
$ws > $thumbsize[0] && $hs > $thumbsize[1])
    {
       
$aspect = $ws/$hs;
        if(
$aspect <= 1.333333)  {
           
$hd = $thumbsize[1];
           
$wd = floor($hd*$aspect);
        }
        else  {
           
$wd = $thumbsize[0];
           
$hd = floor($wd/$aspect);
        }
       
$Z = ceil(log(($ws*$hs)/(4*$thumbsize[0]*$thumbsize[1])))+1;
        if(
log(($ws*$hs)/(4*$thumbsize[0]*$thumbsize[1])) < 0) $Z=1;
       
$dx = $dy = 0;
        if(
$Z > 1) {
           
$dest = imagecreatetruecolor(round($ws/$Z), round($hs/$Z));
            for(
$i=0; $i < $hs; $i+=$Z) {
                for(
$j=0; $j < $ws; $j+=$Z) {
                   
$rgb = imagecolorat($sursa, $j, $i);
                   
$r = ($rgb >> 16) & 0xFF;
                   
$g = ($rgb >> 8) & 0xFF;
                   
$b = $rgb & 0xFF;
                   
$pcol = imagecolorallocate($dest, $r, $g, $b);
                   
imagesetpixel($dest, $dx, $dy, $pcol);
                   
$dx++;
                }
               
$dx=0;
               
$dy++;
            }
        }
        else
        {
           
$dest = imagecreatetruecolor($ws, $hs);
           
imagecopy($dest, $sursa, 0, 0, 0, 0, $ws, $hs );   
        }
       
imagedestroy($sursa);
       
$destrs = imagecreatetruecolor($wd, $hd);
       
ImageCopyResampleBicubic($destrs,$dest,0,0,0,0,
$wd,$hd,round($ws/$Z),round($hs/$Z));
       
ImageJpeg($destrs, $cale_out, 100);   
        echo
"Z:$Z <b>|</b> ($ws x $hs) -> ($wd x $hd) @ ".$ws/$hs;
    }   
}

function
mf() {
    list(
$usec, $sec) = explode(" ", microtime());
   
$x[0] = $sec$x[1] = $usec;
    return
$x;
}

$timp1 = mf(); //starting to count
makeThumb($cale_in); //makes the actual thumb
$timp2 = mf(); //timer ends
$dsec = $timp2[0] - $timp1[0];
$dusec = $timp2[1] - $timp1[1];
echo
"<b>|</b> Time : <b>".round($dusec+$dsec,6)."s</b> <b>|</b><br />";

?>
tim at leethost dot com
08-Sep-2007 06:25
Here's an improved and corrected version of my fastimagecopyresampled script.  Because I typically scale entire images it appears my previous code was not properly tested.  In any case, this new script has been thoroughly tested, cleaned up, and enhanced.  The code is a little tighter and quality values between 0 and 1 will yield a mosaic effect (basically lowering quality below the pervious lowest value of 1).  I would still suggest using a quality of 2 to 4 as that's the primary purpose of this script, which is high quality and fast results.  Don't be afraid of using a quality value of 1.5 for extreme cases where you have a bunch of very large images from digital cameras (like those 10M pixel cameras theses days) and creating thumbnails in batch mode.  Even a quality of 1.5 is a big improvement over imagecopyresized and is ultra-fast.

Anyway, enjoy and I hope this is the bug-free last version.

<?
function fastimagecopyresampled (&$dst_image, $src_image, $dst_x, $