首頁 >php教程 >php手册 >支持文件上传兼容性好图片缩略图程序

支持文件上传兼容性好图片缩略图程序

WBOY
WBOY原創
2016-06-13 09:48:42956瀏覽

下面提供三款生成缩代码,兼容性都相当的不错,也可以自定高度与宽度哦。

function imageresize($srcfile,$tow,$toh,$tofile="")
{
if($tofile==""){ $tofile = $srcfile; }
$info = "";
$data = getimagesize($srcfile,$info);
switch ($data[2])
{
case 1:
if(!function_exists("imagecreatefromgif")){
echo "你的gd库不能使用gif格式的图片,请使用jpeg或png格式!返回";
exit();
}
$im = imagecreatefromgif($srcfile);
break;
case 2:
if(!function_exists("imagecreatefromjpeg")){
echo "你的gd库不能使用jpeg格式的图片,请使用其它格式的图片!返回";
exit();
}
$im = imagecreatefromjpeg($srcfile);
break;
case 3:
$im = imagecreatefrompng($srcfile);
break;
}
$srcw=imagesx($im);
$srch=imagesy($im);
$towh=$tow/$toh;
$srcwh=$srcw/$srch;
if($towh $ftow=$tow;
$ftoh=$ftow*($srch/$srcw);
}
else{
$ftoh=$toh;
$ftow=$ftoh*($srcw/$srch);
}
if($srcw>$tow||$srch>$toh)
{
if(function_exists("imagecreatetruecolor")){
@$ni = imagecreatetruecolor($ftow,$ftoh);
if($ni) imagecopyresampled($ni,$im,0,0,0,0,$ftow,$ftoh,$srcw,$srch);
else{
$ni=imagecreate($ftow,$ftoh);
imagecopyresized($ni,$im,0,0,0,0,$ftow,$ftoh,$srcw,$srch);
}
}else{
$ni=imagecreate($ftow,$ftoh);
imagecopyresized($ni,$im,0,0,0,0,$ftow,$ftoh,$srcw,$srch);
}
if(function_exists('imagejpeg')) imagejpeg($ni,$tofile);
else imagepng($ni,$tofile);
imagedestroy($ni);
}
imagedestroy($im);
}

实例代码二

/*构造函数-生成缩略图+水印,参数说明:
$srcfile-图片文件名,
$dstfile-另存文件名,
$markwords-水印文字,
$markimage-水印图片,
$dstw-图片保存宽度,
$dsth-图片保存高度,
$rate-图片保存品质*/
makethumb("a.jpg","b.jpg","50","50");
function makethumb($srcfile,$dstfile,$dstw,$dsth,$rate=100,$markwords=null,$markimage=null)
{
$data = getimagesize($srcfile);
switch($data[2])
{
case 1:
$im=@imagecreatefromgif($srcfile);
break;
case 2:
$im=@imagecreatefromjpeg($srcfile);
break;
case 3:
$im=@imagecreatefrompng($srcfile);
break;
}
if(!$im) return false;
$srcw=imagesx($im);
$srch=imagesy($im);
$dstx=0;
$dsty=0;
if ($srcw*$dsth>$srch*$dstw)
{
$fdsth = round($srch*$dstw/$srcw);
$dsty = floor(($dsth-$fdsth)/2);
$fdstw = $dstw;
}
else
{
$fdstw = round($srcw*$dsth/$srch);
$dstx = floor(($dstw-$fdstw)/2);
$fdsth = $dsth;
}
$ni=imagecreatetruecolor($dstw,$dsth);
$dstx=($dstx $dsty=($dstx $dstx=($dstx>($dstw/2))?floor($dstw/2):$dstx;
$dsty=($dsty>($dsth/2))?floor($dsth/s):$dsty;
$white = imagecolorallocate($ni,255,255,255);
$black = imagecolorallocate($ni,0,0,0);
imagefilledrectangle($ni,0,0,$dstw,$dsth,$white);// 填充背景色
imagecopyresized($ni,$im,$dstx,$dsty,0,0,$fdstw,$fdsth,$srcw,$srch);
if($markwords!=null)
{
$markwords=iconv("gb2312","utf-8",$markwords);
//转换文字编码
imagettftext($ni,20,30,450,560,$black,"simhei.ttf",$markwords); //写入文字水印
//参数依次为,文字大小|偏转度|横坐标|纵坐标|文字颜色|文字类型|文字内容
}
elseif($markimage!=null)
{
$wimage_data = getimagesize($markimage);
switch($wimage_data[2])
{
case 1:
$wimage=@imagecreatefromgif($markimage);
break;
case 2:
$wimage=@imagecreatefromjpeg($markimage);
break;
case 3:
$wimage=@imagecreatefrompng($markimage);
break;
}
imagecopy($ni,$wimage,500,560,0,0,88,31); //写入图片水印,水印图片大小默认为88*31
imagedestroy($wimage);
}
imagejpeg($ni,$dstfile,$rate);
imagejpeg($ni,$srcfile,$rate);
imagedestroy($im);
imagedestroy($ni);
}
?>

支持图片上传代码

 $pic_name=date("dmyhis");

 // 生成图片的宽度
 $pic_width=$_post['width'];

 // 生成图片的高度
 $pic_height=$_post['length'];

 function resizeimage($im,$maxwidth,$maxheight,$name){
  //取得当前图片大小
  $width = imagesx($im);
  $height = imagesy($im);
  //生成缩略图的大小
  if(($width > $maxwidth) || ($height > $maxheight)){
   $widthratio = $maxwidth/$width;  
   $heightratio = $maxheight/$height; 
   if($widthratio     $ratio = $widthratio;
   }else{
    $ratio = $heightratio;
   }
   $newwidth = $width * $ratio;
   $newheight = $height * $ratio;
  
   if(function_exists("imagecopyresampled")){
    $newim = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
   }else{
    $newim = imagecreate($newwidth, $newheight);
    imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
   }
   imagejpeg ($newim,$name . ".jpg");
   imagedestroy ($newim);
  }else{
   imagejpeg ($im,$name . ".jpg");
  }
 }

 if($_files['image']['size']){
  //echo $_files['image']['type'];
  if($_files['image']['type'] == "image/pjpeg"||$_files['image']['type'] == "image/jpg"||$_files['image']['type'] == "image/jpeg"){
   $im = imagecreatefromjpeg($_files['image']['tmp_name']);
  }elseif($_files['image']['type'] == "image/x-png"){
   $im = imagecreatefrompng($_files['image']['tmp_name']);
  }elseif($_files['image']['type'] == "image/gif"){
   $im = imagecreatefromgif($_files['image']['tmp_name']);
  }
  if($im){
   if(file_exists($pic_name.'.jpg')){
    unlink($pic_name.'.jpg');
   }
   resizeimage($im,$pic_width,$pic_height,$pic_name);
   imagedestroy ($im);
  }
 }
?>

支持文件上传兼容性好图片缩略图程序






生成缩略图宽度:


生成缩略图长度:



陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn