/ Published in: PHP
With this function you can make a thumbnail from an image. Usage: $image = "something.jpg"; thumb($image, 100, 100);
It can be easy modified to accept other types of images, actually only support jpeg, gif and png. Gif images lost their animation, and png or gif image lost their transparency.
Expand |
Embed | Plain Text
<?php function thumb($img, $w, $h, $fill = true) { return false; } //$ext = strtolower(array_pop(explode(".",$img))); switch ($imgInfo[2]) { case 1: $im = imagecreatefromgif($img); break; case 2: $im = imagecreatefromjpeg($img); break; case 3: $im = imagecreatefrompng($img); break; } if ($imgInfo[0] <= $w && $imgInfo[1] <= $h && !$fill) { $nHeight = $imgInfo[1]; $nWidth = $imgInfo[0]; }else{ if ($w/$imgInfo[0] < $h/$imgInfo[1]) { $nWidth = $w; $nHeight = $imgInfo[1]*($w/$imgInfo[0]); }else{ $nWidth = $imgInfo[0]*($h/$imgInfo[1]); $nHeight = $h; } } $newImg = imagecreatetruecolor($nWidth, $nHeight); imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]); switch ($imgInfo[2]) { case 1: imagegif($newImg); break; case 2: imagejpeg($newImg); break; case 3: imagepng($newImg); break; } imagedestroy($newImg); } ?>
You need to login to post a comment.
