• 技术文章 >后端开发 >php教程

    php图片处理类(生成缩略图,增加水印,获取图片信息)_PHP教程

    2016-07-13 10:45:38原创389
    php教程图片处理类(生成缩略图,增加水印,获取图片信息)
    本文章提供这款图片处理类,他可以做的事情有把图片生成缩略图,可能给图片增加水印以及获取图片信息,算是比较实用代码又简洁的函数*/

    class image
    {
    public $info=array();

    function __construct()
    {
    !extension_loaded('gd') && exit("www.bKjia.c0m提示:服务器环境不支持gd库");
    return true;
    }

    function image()
    {
    $this->__construct();
    }

    function thumb($image,$thumb_width=300,$thumb_height=225)
    {
    $info=$this->info($image);
    $scale=min(1,min($thumb_width/$info['width'],$thumb_height/$info['height'])); //按比例缩放
    $thumb_width=intval($info['width']*$scale);
    $thumb_height=intval($info['height']*$scale);
    $createfunc='imagecreatefrom'.($info['type']=='jpg'?'jpeg':$info['type']);
    $im=$createfunc($image);
    $thumb_im=$info['type']!='gif' && function_exists('imagecreatetruecolor')?imagecreatetruecolor($thumb_width,$thumb_height):imagecreate($thumb_width,$thumb_height);
    imagecopyresampled($thumb_im,$im,0,0,0,0,$thumb_width,$thumb_height,$info['width'],$info['height']);
    if($info['type']=='gif' || $info['type']=='png')
    {
    $bgcolor=imagecolorallocate($thumb_im,0,255,0);
    imagecolortransparent($thumb_im,$bgcolor);
    }
    $imagefunc='image'.($info['type']=='jpg'?'jpeg':$info['type']);
    $thumbname='thumb_'.$info['name'].'.'.$info['type'];
    $imagefunc($thumb_im,$info['path'].$thumbname);
    imagedestroy($im);
    imagedestroy($thumb_im);
    return $info['path'].$thumbname;
    }

    function watermark($image,$pos=9,$watermarkimg='images/watermark.gif',$pct=65,$text='',$w_font=5,$w_color='#ff0000')
    {
    $imageinfo=$this->info($image);
    $source_w=$imageinfo['width'];
    $source_h=$imageinfo['height'];
    $imagecreatefunc='imagecreatefrom'.($imageinfo['type']=='jpg'?'jpeg':$imageinfo['type']);
    $im=$imagecreatefunc($image);
    if(!empty($watermarkimg) && file_exists($watermarkimg)) //添加图片水印
    {
    $iswaterimage=true;
    $watermarkinfo=$this->info($watermarkimg);
    $width=$watermarkinfo['width'];
    $height=$watermarkinfo['height'];
    $watermarkcreatefunc='imagecreatefrom'.($watermarkinfo['type']=='jpg'?'jpeg':$watermarkinfo['type']);
    $watermark_im=$watermarkcreatefunc($watermarkimg);
    }
    else //添加文字水印
    {
    $iswaterimage=false;
    if(!empty($w_color) && strlen($w_color)==7)
    {
    $r=hexdec(substr($w_color,1,2));
    $g=hexdec(substr($w_color,3,2));
    $b=hexdec(substr($w_color,5,2));
    }
    $temp = imagettfbbox(ceil($w_font*2.5), 0, 'fonts/alger.ttf', $text);
    $width = $temp[2] - $temp[6];
    $height = $temp[3] - $temp[7];
    unset($temp);
    }
    switch($pos)
    {
    case 0:
    $wx = mt_rand(0,($source_w - $width));
    $wy = mt_rand(0,($source_h - $height));
    break;
    case 1:
    $wx = 5;
    $wy = 5;
    break;
    case 2:
    $wx = ($source_w - $width) / 2;
    $wy = 5;
    break;
    case 3:
    $wx = $source_w - $width-5;
    $wy = 5;
    break;
    case 4:
    $wx = 5;
    $wy = ($source_h - $height) / 2;
    break;
    case 5:
    $wx = ($source_w - $width) / 2;
    $wy = ($source_h - $height) / 2;
    break;
    case 6:
    $wx = $source_w - $width-5;
    $wy = ($source_h - $height) / 2;
    break;
    case 7:
    $wx = 5;
    $wy = $source_h - $height-5;
    break;
    case 8:
    $wx = ($source_w - $width) / 2;
    $wy = $source_h - $height-5;
    break;
    default:
    $wx = $source_w - $width-5;
    $wy = $source_h - $height-5;
    break;
    }
    if($iswaterimage)
    {
    if($imageinfo['type'] == 'png') {
    imagecopy($im, $watermark_im, $wx, $wy, 0, 0, $width, $height);
    } else {
    imagecopymerge($im, $watermark_im, $wx, $wy, 0, 0, $width, $height, $pct);
    }
    }
    else
    {
    imagestring($im,$w_font,$wx,$wy,$text,imagecolorallocate($im,$r,$g,$b));
    }
    $imagefunc='image'.($imageinfo['type']=='jpg'?'jpeg':$imageinfo['type']);
    $imagefunc($im,$image);
    imagedestroy($im);
    return true;
    }

    function info($image)
    {
    $info=array();
    $info['size']=filesize($image);
    $imageinfo=getimagesize($image);
    $info['width']=$imageinfo[0];
    $info['height']=$imageinfo[1];
    $info['width_height']=$imageinfo[3];
    $info['mime']=$imageinfo['mime'];
    unset($imageinfo);
    $imageinfo=pathinfo($image);
    $info['path']=$imageinfo['dirname'].'//m.sbmmt.com/m/';
    $info['type']=strtolower($imageinfo['extension']); //图片类型,不含'.'
    $info['name']=$imageinfo['filename'];
    unset($imageinfo,$name);
    $this->info=$info;
    return $info;
    }
    }

    www.bkjia.comtruehttp://www.bkjia.com/PHPjc/633005.htmlTechArticlephp教程图片处理类(生成缩略图,增加水印,获取图片信息) 本文章提供这款图片处理类,他可以做的事情有把图片生成缩略图,可能给图片增...

    php入门到就业线上直播课:查看学习

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    专题推荐:php 图片 处理 生成 略图 增加 水印 获取 信息 php 教程 图片 处理 生成 略图 增加 水印
    上一篇:php图片上传并生成缩略图效果_PHP教程 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• 你知道如何用PHP实现多进程吗• PHP与MySQL连接的方法总结• 求解:phpcms模板怎样转码?该怎么解决• php 之 cookie 跟 session 简单解读(笔记)• php怎的快捷知道某个函数在哪个文件用过
    1/1

    PHP中文网