当前位置:首页 > PHP教程 > php图像处理 > 列表

PHP生成同比例的缩略图实现程序

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-18 15:40:06 浏览: 评论:0 

在php中生成缩略图是程序开发中常用的,下面我找了几个不错的php生成缩略图的实现程序,有需要的朋友可使用,本人亲测绝对好用.

创建图像缩略图需要许多时间,此代码将有助于了解缩略图的逻辑,代码如下:

  1. /********************** 
  2. *@filename - path to the image 
  3. *@tmpname - temporary path to thumbnail 
  4. *@xmax - max width 
  5. *@ymax - max height 
  6. */ 
  7. function resize_image($filename, $tmpname, $xmax, $ymax) 
  8. { 
  9.     $ext = explode(".", $filename); 
  10.     $ext = $ext[count($ext)-1]; 
  11.  
  12.     if($ext == "jpg" || $ext == "jpeg") 
  13.         $im = imagecreatefromjpeg($tmpname); 
  14.     elseif($ext == "png") 
  15.         $im = imagecreatefrompng($tmpname); 
  16.     elseif($ext == "gif") 
  17.         $im = imagecreatefromgif($tmpname); 
  18.  
  19.     $x = imagesx($im); 
  20.     $y = imagesy($im); 
  21.  
  22.     if($x <= $xmax && $y <= $ymax) 
  23.         return $im; 
  24.  
  25.     if($x >= $y) { 
  26.         $newx = $xmax; 
  27.         $newy = $newx * $y / $x; 
  28.     } 
  29.     else { 
  30.         $newy = $ymax; 
  31.         $newx = $x / $y * $newy; 
  32.     } 
  33.  
  34.     $im2 = imagecreatetruecolor($newx, $newy); 
  35.     imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y); 
  36.     return $im2; 
  37. } 
  38. //例2,代码如下 
  39. //开源代码phpfensi.com 
  40. function creat_thumbnail($img,$w,$h,$path) 
  41. { 
  42.  $org_info = getimagesize($img); //获得图像大小且是通过post传递过来的 
  43.  //var_dump($org_info); 
  44.  //Array ( [0] => 1024 [1] => 768 [2] => 3 [3] => width="1024" height="768" [bits] => 8 [mime] => image/png ) 
  45.  $orig_x = $org_info[0]; //图像宽度 
  46.  $orig_y = $org_info[1]; //图像高度 
  47.  $orig_type = $org_info[2]; //图片类别即后缀 1 = GIF,2 = JPG,3 = PNG, 
  48.  
  49.  //是真彩色图像 
  50.  if (function_exists("imagecreatetruecolor")) 
  51.  { 
  52.   switch($orig_type) 
  53.   { 
  54.    //从给定的gif文件名中取得的图像 
  55.    case 1  : $thumb_type = ".gif"; $_creatImage = "imagegif"; $_function = "imagecreatefromgif"; 
  56.    break; 
  57.    //从给定的jpeg,jpg文件名中取得的图像 
  58.    case 2  : $thumb_type = ".jpg"; $_creatImage = "imagejpeg"; $_function = "imagecreatefromjpeg"; 
  59.    break; 
  60.    //从给定的png文件名中取得的图像 
  61.    case 3  : $thumb_type = ".png"; $_creatImage = "imagepng"; $_function = "imagecreatefrompng"; 
  62.    break; 
  63.   } 
  64.  } 
  65.  //如果从给定的文件名可取得的图像 
  66.  if(function_exists($_function)) 
  67.  { 
  68.   $orig_image = $_function($img); //从给定的$img文件名中取得的图像 
  69.  } 
  70.  if (($orig_x / $orig_y) >= (4 / 3)) //如果宽/高 >= 4/3 
  71.  { 
  72.   $y = round($orig_y / ($orig_x / $w)); //对浮点数进行四舍五入 
  73.   $x = $w; 
  74.  } 
  75.  else //即 高/宽 >= 4/3 
  76.  { 
  77.   $x = round($orig_x / ($orig_y / $h)); 
  78.   $y = $h; 
  79.  } 
  80.  $sm_image = imagecreatetruecolor($x, $y); //创建真彩色图片 
  81.  //重采样拷贝部分图像并调整大小 
  82.  Imagecopyresampled($sm_image, $orig_image, 0, 0, 0, 0, $x, $y, $orig_x, $orig_y); 
  83.  //imageJPEG($sm_image, '', 80); //在浏览器输出图像 
  84.  $tnpath = $path."/"."s_".date('YmdHis').$thumb_type; //缩略图的路径 
  85.  $thumbnail = @$_creatImage($sm_image, $tnpath, 80); //生成图片,成功返回true(或1) 
  86.  imagedestroy ($sm_image); //销毁图像 
  87.  if($thumbnail==true) 
  88.  { 
  89.   return $tnpath; 
  90.  } 
  91. } 

Tags: PHP生成缩略图 实现程序

分享到: