php图片处理类:缩略,裁剪,圆角,倾斜(1/3)

 更新时间:2016年11月25日 16:58  点击:2186
本文章免费提供一款php图片处理类:缩略,裁剪,圆角,倾斜代码,他可以切出不同风格的图片哦,哈哈,比其它的在线切图要好很多了哦。
 代码如下 复制代码
class resizeimage
{
   //图片类型
   var $type;
   //实际宽度
   var $width;
   //实际高度
   var $height;
   //改变后的宽度
   var $resize_width;
   //改变后的高度
   var $resize_height;
   //是否裁图
   var $cut;
   //源图象
   var $srcimg;
   //目标图象地址
   var $dstimg;
   //圆角源
   var $corner;
   var $im;

function resizeimage($img, $corner, $wid, $hei,$c, $corner_radius, $angle)
   {
       $this->srcimg = $img;
           $this->corner = $corner;
       $this->resize_width = $wid;
       $this->resize_height = $hei;
       $this->cut = $c;
           $this->corner_radius = $corner_radius;
           $this->angle = $angle;
       //图片的类型
       $this->type = substr(strrchr($this->srcimg,"."),1);
       //初始化图象
       $this->initi_img();
       //目标图象地址
       $this -> dst_img();
       //--
       $this->width = imagesx($this->im);
       $this->height = imagesy($this->im);
       //生成图象
       $this->newimg();
       imagedestroy ($this->im);
   }
   function newimg()
   {
       //改变后的图象的比例
       $resize_ratio = ($this->resize_width)/($this->resize_height);
       //实际图象的比例
       $ratio = ($this->width)/($this->height);
       if(($this->cut)=="1")
       //裁图
       {
           if($ratio>=$resize_ratio)
           //高度优先
           {
               $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
               imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
                           $tmp = $this->rounded_corner($newimg,$this->resize_width);
               imagepng ($tmp,$this->dstimg);
           }
           if($ratio<$resize_ratio)
           //宽度优先
           {
               $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
               imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
                           $tmp = $this->rounded_corner($newimg);
               imagepng ($tmp,$this->dstimg);
           }
       }
       else
      

 

 

在很多地方我们都会看到把ip地址生成在图片哦,今天我们就来告诉你如何利用php 把ip生成在图片上哦,好了下面我们就来看看实例吧。

 代码如下 复制代码

$img = imagecreate(180,50);

  $ip = $_server['remote_addr'];

  imagecolortransparent($img,$bgcolor);

  $bgcolor = imagecolorallocate($img, 0x2c,0x6d,0xaf); // 背景颜色

  $shadow = imagecolorallocate($img, 250,0,0); // 阴影颜色

  $textcolor = imagecolorallocate($img, oxff,oxff,oxff); // 字体颜色

  imagettftext($img,10,0,78,30,$shadow,"c:/windows/fonts/tahoma.ttf",$ip);

  //显示背景

  imagettftext($img,10,0,25,28,$textcolor,"c:/windows/fonts/tahoma.ttf","your ip is".$ip);

  // 显示ip

  imagepng($img);

  imagecreatefrompng($img);

  imagedestroy($img);

其实就利用了php生成图片功能,生成验证码也经常会用到这样的功能哦。

本款php生成验证码代码原理生成随机数-->创建图片-->随机数写进图片-->保存在session中 ,看一下流程验证码图片生成 通知浏览器将要输出PNG图片 准备好随机数发生器种子 srand((double)microtime()*1000000); 将四位整数验证码绘入图片
 代码如下 复制代码

/*=====================
产生随机字符串函数
=====================*/
function random($length) {
 $hash = '';
 $chars = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz';
 $max = strlen($chars) - 1;
 mt_srand((double)microtime() * 1000000);
 for($i = 0; $i < $length; $i++) {
  $hash .= $chars[mt_rand(0, $max)];
 }
 return $hash;
}

//验证码图片生成

 session_start();
    //通知浏览器将要输出png图片
    header("content-type: image/png");
    //准备好随机数发生器种子 
    //srand((double)microtime()*1000000);
    //准备图片的相关参数  
    $im = imagecreate(62,22);
    $black = imagecolorallocate($im, 0,0,0);  //rgb黑色标识符
    $white = imagecolorallocate($im, 255,255,255); //rgb白色标识符
    $gray = imagecolorallocate($im, 179,183,185); //rgb灰色标识符
    //开始作图    
    imagefill($im,0,0,$gray);
    //while(($randval=rand()%100000)<10000);{
        //$_session["check_code"] = $randval;
        //将四位整数验证码绘入图片
  $randval=random(4);
  $_session["check_code"]=$randval;
        imagestring($im, 5, 10, 3, $randval, $white);
    //}
    //加入干扰象素   
    for($i=0;$i<150;$i++){
        $randcolor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
        imagesetpixel($im, rand()%70 , rand()%30 , $white);
    }
    //输出验证图片
    imagepng($im);
    //销毁图像标识符
    imagedestroy($im);

?>

本款是一款php 读取目录下图像文件哦,代码,他利用了opendir来打开目录然后获取文件后缀名,判断是否为指定文件。
 代码如下 复制代码

$directory = 'gallery';

$allowed_types=array('jpg','jpeg','gif','png');
$file_parts=array();
$ext='';
$title='';
$i=0;

$dir_handle = @opendir($directory) or die("there is an error with your image directory!");

while ($file = readdir($dir_handle))
{
 if($file=='.' || $file == '..') continue;
 
 $file_parts = explode('.',$file);
 $ext = strtolower(array_pop($file_parts));

 $title = implode('.',$file_parts);
 $title = htmlspecialchars($title);
 
 $nomargin='';
 
 if(in_array($ext,$allowed_types))
 {
  if(($i+1)%4==0) $nomargin='nomargin';
 
  echo '
  <div class="pic '.$nomargin.'" style="background:url('.$directory.'/'.$file.') no-repeat 50% 50%;">
  <a href="'.$directory.'/'.$file.'" title="'.$title.'" target="_blank">'.$title.'</a>
  </div>';
  
  $i++;
 }
}

closedir($dir_handle);

这是一款图象缩略函数哦,把上传的新图片给$srcfile然后进行文件按$thumbwidth 缩小图宽最大尺寸 与$thumbheitht 缩小图高最大尺寸 生成小图。

图象缩略函数
参数说明:

 代码如下 复制代码
$srcfile 原图地址;
$dir  新图目录
$thumbwidth 缩小图宽最大尺寸
$thumbheitht 缩小图高最大尺寸
$ratio 默认等比例缩放 为1是缩小到固定尺寸。
*/
function makethumb($srcfile,$dir,$thumbwidth,$thumbheight,$ratio=0)
{
 //判断文件是否存在
if (!file_exists($srcfile))return false;
 //生成新的同名文件,但目录不同
$imgname=explode('/',$srcfile);
$arrcount=count($imgname);
$dstfile = $dir.$imgname[$arrcount-1];
//缩略图大小
$tow = $thumbwidth;
$toh = $thumbheight;
if($tow < 40) $tow = 40;
if($toh < 45) $toh = 45;   
 //获取图片信息
    $im ='';
    if($data = getimagesize($srcfile)) {
        if($data[2] == 1) {
            $make_max = 0;//gif不处理
            if(function_exists("imagecreatefromgif")) {
                $im = imagecreatefromgif($srcfile);
            }
        } elseif($data[2] == 2) {
            if(function_exists("imagecreatefromjpeg")) {
                $im = imagecreatefromjpeg($srcfile);
            }
        } elseif($data[2] == 3) {
            if(function_exists("imagecreatefrompng")) {
                $im = imagecreatefrompng($srcfile);
            }
        }
    }
    if(!$im) return '';
    $srcw = imagesx($im);
    $srch = imagesy($im);
    $towh = $tow/$toh;
    $srcwh = $srcw/$srch;
    if($towh <= $srcwh){
        $ftow = $tow;
        $ftoh = $ftow*($srch/$srcw);
    } else {
        $ftoh = $toh;
        $ftow = $ftoh*($srcw/$srch);
    }
    if($ratio){
        $ftow = $tow;
        $ftoh = $toh;
    }
    //缩小图片
    if($srcw > $tow || $srch > $toh || $ratio) {
        if(function_exists("imagecreatetruecolor") && function_exists("imagecopyresampled") && @$ni = imagecreatetruecolor($ftow, $ftoh)) {
            imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);
        } elseif(function_exists("imagecreate") && function_exists("imagecopyresized") && @$ni = imagecreate($ftow, $ftoh)) {
            imagecopyresized($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);
        } else {
            return '';
        }
        if(function_exists('imagejpeg')) {
            imagejpeg($ni, $dstfile);
        } elseif(function_exists('imagepng')) {
            imagepng($ni, $dstfile);
        }
    }else {
        //小于尺寸直接复制
    copy($srcfile,$dstfile);
    }
    imagedestroy($im);
    if(!file_exists($dstfile)) {
        return '';
    } else {
        return $dstfile;
    }
}

?>

[!--infotagslink--]

相关文章

  • 使用PHP+JavaScript将HTML页面转换为图片的实例分享

    这篇文章主要介绍了使用PHP+JavaScript将HTML元素转换为图片的实例分享,文后结果的截图只能体现出替换的字体,也不能说将静态页面转为图片可以加快加载,只是这种做法比较interesting XD需要的朋友可以参考下...2016-04-19
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • Python 图片转数组,二进制互转操作

    这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
  • Photoshop古装美女图片转为工笔画效果制作教程

    今天小编在这里就来给各位Photoshop的这一款软件的使用者们来说说把古装美女图片转为细腻的工笔画效果的制作教程,各位想知道方法的使用者们,那么下面就快来跟着小编一...2016-09-14
  • php抓取网站图片并保存的实现方法

    php如何实现抓取网页图片,相较于手动的粘贴复制,使用小程序要方便快捷多了,喜欢编程的人总会喜欢制作一些简单有用的小软件,最近就参考了网上一个php抓取图片代码,封装了一个php远程抓取图片的类,测试了一下,效果还不错分享...2015-10-30
  • Windows批量搜索并复制/剪切文件的批处理程序实例

    这篇文章主要介绍了Windows批量搜索并复制/剪切文件的批处理程序实例,需要的朋友可以参考下...2020-06-30
  • jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮

    jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮...2013-10-13
  • BAT批处理判断服务是否正常运行的方法(批处理命令综合应用)

    批处理就是对某对象进行批量的处理,通常被认为是一种简化的脚本语言,它应用于DOS和Windows系统中。这篇文章主要介绍了BAT批处理判断服务是否正常运行(批处理命令综合应用),需要的朋友可以参考下...2020-06-30
  • python opencv通过4坐标剪裁图片

    图片剪裁是常用的方法,那么如何通过4坐标剪裁图片,本文就详细的来介绍一下,感兴趣的小伙伴们可以参考一下...2021-06-04
  • PHP file_get_contents设置超时处理方法

    file_get_contents的超时处理话说,从PHP5开始,file_get_content已经支持context了(手册上写着:5.0.0 Added the context support. ),也就是说,从5.0开始,file_get_contents其实也可以POST数据。今天说的这篇是讲超时的,确实在...2013-10-04
  • 使用PHP下载CSS文件中的图片的代码

    共享一段使用PHP下载CSS文件中的图片的代码 复制代码 代码如下: <?php //note 设置PHP超时时间 set_time_limit(0); //note 取得样式文件内容 $styleFileContent = file_get_contents('images/style.css'); //not...2013-10-04
  • C#多线程中的异常处理操作示例

    这篇文章主要介绍了C#多线程中的异常处理操作,涉及C#多线程及异常的捕获、处理等相关操作技巧,需要的朋友可以参考下...2020-06-25
  • PHP swfupload图片上传的实例代码

    PHP代码如下:复制代码 代码如下:if (isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) { $upload_file = $_FILES['Filedata']; $fil...2013-10-04
  • C#中图片旋转和翻转(RotateFlipType)用法分析

    这篇文章主要介绍了C#中图片旋转和翻转(RotateFlipType)用法,实例分析了C#图片旋转及翻转Image.RotateFlip方法属性的常用设置技巧,需要的朋友可以参考下...2020-06-25
  • OpenCV如何去除图片中的阴影的实现

    这篇文章主要介绍了OpenCV如何去除图片中的阴影的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-29
  • ps怎么制作图片阴影效果

    ps软件是现在很多人比较喜欢的,有着非常不错的使用效果,这次文章就给大家介绍下ps怎么制作图片阴影效果,还不知道制作方法的赶紧来看看。 ps图片阴影效果怎么做方法/...2017-07-06
  • C#将图片和字节流互相转换并显示到页面上

    本文主要介绍用C#实现图片转换成字节流,字节流转换成图片,并根据图片路径返回图片的字节流,有需要的朋友可以参考下...2020-06-25
  • postgresql 中的时间处理小技巧(推荐)

    这篇文章主要介绍了postgresql 中的时间处理小技巧(推荐),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-29
  • Python同时处理多个异常的方法

    这篇文章主要介绍了Python同时处理多个异常的方法,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-07-29
  • php上传图片学习笔记与心得

    我们在php中上传文件就必须使用#_FILE变量了,这个自动全局变量 $_FILES 从 PHP 4.1.0 版本开始被支持。在这之前,从 4.0.0 版本开始,PHP 支持 $HTTP_POST_FILES 数组。这...2016-11-25