IMG_JPG, "jpg"=>IMG_JPG, "gif"=>IMG_GIF, "png"=>IMG_PNG ); function getImageType($name) { global $types; $way = pathinfo($name); $ext = strtolower($way['extension']); $t = $types[$ext]; return $t; } function resize_center($oldname) { $newname = "thumb-". $oldname; // Dimensions of displayed thumbnail $thumbh = 150; $thumbw = 140; // Dimension of intermediate thumbnail $nh = $thumbh; $nw = $thumbw; // Offsets to center the image $xoff = 0; $yoff = 0; $size = getImageSize($oldname); $w = $size[0]; $h = $size[1]; // Applying calculations to dimensions of the image $ratio = $h / $w; $nratio = $thumbh / $thumbw; if($ratio > $nratio) { $x = intval($w * $nh / $h); if ($x < $nw) { $nh = intval($h * $nw / $w); } else { $nw = $x; } $yoff = intval(($nh - $thumbh) / 2); } else { $x = intval($h * $nw / $w); if ($x < $nh) { $nw = intval($w * $nh / $h); } else { $nh = $x; } $xoff = intval(($nw - $thumbw) / 2); } echo "$oldname was $w x $h,"; echo " $newname is $nw x $nh cropped to $thumbw x $thumbh\n"; // Building the intermediate resized thumbnail $t = getImageType($oldname); switch($t) { case IMG_JPG: $resimage = imagecreatefromjpeg($oldname); break; case IMG_GIF: $resimage = imagecreatefromgif($oldname); break; case IMG_PNG: $resimage = imagecreatefrompng($oldname); break; } $newimage = imagecreatetruecolor($nw, $nh); // use alternate function if not installed imageCopyResampled($newimage, $resimage,0,0,0,0,$nw, $nh, $w, $h); // Making the final cropped thumbnail $viewimage = imagecreatetruecolor($thumbw, $thumbh); imagecopy($viewimage, $newimage, 0, 0, $xoff, $yoff, $nw, $nh); // saving imageJpeg($viewimage, $newname, 85); } // List of images for demo: the script on scriptol.com will take them from a directory $ilist = array("cubes.jpg", "bluedoom.gif", "garden.jpg", "grayhouse.png", "redtree.jpg"); foreach ($ilist as $iname) { if(!file_exists($iname)) { echo "$iname not found\n"; continue; } resize_center($iname); } ?>