在php中生成缩略图是程序开发中常用的,下面我找了几个不错的php生成缩略图的实现程序,有需要的朋友可使用,本人亲测绝对好用.
创建图像缩略图需要许多时间,此代码将有助于了解缩略图的逻辑,代码如下:
-
-
-
-
-
-
- function resize_image($filename, $tmpname, $xmax, $ymax)
- {
- $ext = explode(".", $filename);
- $ext = $ext[count($ext)-1];
-
- if($ext == "jpg" || $ext == "jpeg")
- $im = imagecreatefromjpeg($tmpname);
- elseif($ext == "png")
- $im = imagecreatefrompng($tmpname);
- elseif($ext == "gif")
- $im = imagecreatefromgif($tmpname);
-
- $x = imagesx($im);
- $y = imagesy($im);
-
- if($x <= $xmax && $y <= $ymax)
- return $im;
-
- if($x >= $y) {
- $newx = $xmax;
- $newy = $newx * $y / $x;
- }
- else {
- $newy = $ymax;
- $newx = $x / $y * $newy;
- }
-
- $im2 = imagecreatetruecolor($newx, $newy);
- imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);
- return $im2;
- }
-
-
- function creat_thumbnail($img,$w,$h,$path)
- {
- $org_info = getimagesize($img);
-
-
- $orig_x = $org_info[0];
- $orig_y = $org_info[1];
- $orig_type = $org_info[2];
-
-
- if (function_exists("imagecreatetruecolor"))
- {
- switch($orig_type)
- {
-
- case 1 : $thumb_type = ".gif"; $_creatImage = "imagegif"; $_function = "imagecreatefromgif";
- break;
-
- case 2 : $thumb_type = ".jpg"; $_creatImage = "imagejpeg"; $_function = "imagecreatefromjpeg";
- break;
-
- case 3 : $thumb_type = ".png"; $_creatImage = "imagepng"; $_function = "imagecreatefrompng";
- break;
- }
- }
-
- if(function_exists($_function))
- {
- $orig_image = $_function($img);
- }
- if (($orig_x / $orig_y) >= (4 / 3))
- {
- $y = round($orig_y / ($orig_x / $w));
- $x = $w;
- }
- else
- {
- $x = round($orig_x / ($orig_y / $h));
- $y = $h;
- }
- $sm_image = imagecreatetruecolor($x, $y);
-
- Imagecopyresampled($sm_image, $orig_image, 0, 0, 0, 0, $x, $y, $orig_x, $orig_y);
-
- $tnpath = $path."/"."s_".date('YmdHis').$thumb_type;
- $thumbnail = @$_creatImage($sm_image, $tnpath, 80);
- imagedestroy ($sm_image);
- if($thumbnail==true)
- {
- return $tnpath;
- }
- }
|