首页 > 语言 > PHP > 正文

php大图生成小图代码

2024-09-04 11:44:22
字体:大 中 小
来源:转载
供稿:网友

这是一款利用php自带的功能把指定的大图生成我们指定大小的缩略图代码,使用方便简单,只要把设置下面四个参数就可以生成自己想的大小的缩略图了,代码如下:

  1. function bigtosmallimg($file,$path,$w=120,$h=90) 
  2. { 
  3.  $img=$path.$file; 
  4.  $imgarr=getimagesize($img); 
  5.  $sw=$imgarr[0];//原图宽 
  6.  $sh=$imgarr[1];//原图高 
  7.  $stype=$imgarr[2]; 
  8.  //按比例缩放 
  9.  if($sw/$sh>$w/$h){ 
  10.   $mw=$w; 
  11.   $mh=(int)$sh*($w/$sw); 
  12.  } 
  13.  else{ 
  14.   $mw=(int)$sw*($h/$sh); 
  15.   $mh=$h; 
  16.  } 
  17.  
  18.  switch($stype){//根据上传好的图形文件类型新建一个用来生成缩略图的源文件。  
  19.    case 1:  
  20.     $srcf = imagecreatefromgif($img);  
  21.     break;  
  22.    case 2:  
  23.     $srcf = imagecreatefromjpeg($img);  
  24.     break;  
  25.    case 3:  
  26.     $srcf = imagecreatefrompng($img);  
  27.     break;  
  28.    default:  
  29.     showmsg('程序调用错误。');  
  30.     break;  
  31.  } 
  32.  
  33.  $desf =imagecreatetruecolor($mw,$mh); 
  34.  
  35.  imagecopyresampled($desf,$srcf,0,0,0,0,$mw,$mh,$sw,$sh);  
  36.  $sm_name=$path."s_".$file; 
  37.  switch($stype){  
  38.   case 1:  
  39.    imagegif($desf,$sm_name);  
  40.    break;  
  41.   case 2:  
  42.    imagejpeg($desf,$sm_name);  
  43.    break;  
  44.   case 3:  
  45.    imagepng($desf,$sm_name);  
  46.    break;  
  47.   default:  
  48.    showmsg('无法生成www.Vevb.com' . $stype . '的缩略图。');  
  49.    break;  
  50.  }  
  51.  imagedestroy($desf);  
  52.  imagedestroy($srcf); 
  53.  
  54. } 
  55. //开源代码Vevb.com 
  56. //此缩略图调用方法,代码如下: 
  57.  
  58. bigtosmallimg($file,$path,$w=120,$h=90); 
  59. /* 
  60. $file = 图片的路径 
  61. $path = 生成后保存的路径 
  62. $w =图片宽度 
  63. $h =图片高度 
  64. */ 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表