`
空洞的世界
  • 浏览: 49379 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

好用的图片缩略图上传类

    博客分类:
  • php
阅读更多
<?php
function resizeImage($im,$maxwidth,$maxheight,$name,$filetype)
 {
  $pic_width = imagesx($im);
  $pic_height = imagesy($im);
 
  if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight))
  {
   if($maxwidth && $pic_width>$maxwidth)
   {
    $widthratio = $maxwidth/$pic_width;
    $resizewidth_tag = true;
   }
 
   if($maxheight && $pic_height>$maxheight)
   {
    $heightratio = $maxheight/$pic_height;
    $resizeheight_tag = true;
   }
 
   if($resizewidth_tag && $resizeheight_tag)
   {
    if($widthratio<$heightratio)
     $ratio = $widthratio;
    else
     $ratio = $heightratio;
   }
 
   if($resizewidth_tag && !$resizeheight_tag)
    $ratio = $widthratio;
   if($resizeheight_tag && !$resizewidth_tag)
    $ratio = $heightratio;
 
   $newwidth = $pic_width * $ratio;
   $newheight = $pic_height * $ratio;
 
   if(function_exists("imagecopyresampled"))
   {
    $newim = imagecreatetruecolor($newwidth,$newheight);//PHP系统函数
      imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);//PHP系统函数
   }
   else
   {
    $newim = imagecreate($newwidth,$newheight);
      imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
   }
 
   $name = $name.$filetype;
   imagejpeg($newim,"22222/".$name);
   imagedestroy($newim);
  }
  else
  {
   $name = $name.$filetype;
   imagejpeg($im,"22222/".$name);
  }
 }
//使用方法:
$im=imagecreatefromjpeg("22222/psu.jpg");//参数是图片的存方路径
$maxwidth="120";//设置图片的最大宽度
$maxheight="100";//设置图片的最大高度
$name="psu";//图片的名称,随便取吧
$filetype=".jpg";//图片类型
resizeImage($im,$maxwidth,$maxheight,$name,$filetype);//调用上面的函数


下载附件,直接在本地访问试试
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics