php添加文字水印/图片水印,压缩,剪切的封装类

php中文网
发布: 2016-06-20 13:03:30
原创
1096人浏览过

本文php图片操作封装类里面的四种方法,文字水印(imagettftext()),图片水印(imagecopymerge()),图片压缩,图片剪切(imagecopyresampled()),其余的常用gd函数便不赘述。

直接上代码:

<?php </p>
                    <div class="aritcle_card">
                        <a class="aritcle_card_img" href="/ai/1468">
                            <img src="https://img.php.cn/upload/ai_manual/000/000/000/175680376930740.jpg" alt="AI改图神器">
                        </a>
                        <div class="aritcle_card_info">
                            <a href="/ai/1468">AI改图神器</a>
                            <p>AI万能图片编辑器,一键抠图,去水印,智能图片美化,照片转漫画,照片变活转视频,图片无损放大,一键背景虚化,位图智能转矢量图</p>
                            <div class="">
                                <img src="/static/images/card_xiazai.png" alt="AI改图神器">
                                <span>37</span>
                            </div>
                        </div>
                        <a href="/ai/1468" class="aritcle_card_btn">
                            <span>查看详情</span>
                            <img src="/static/images/cardxiayige-3.png" alt="AI改图神器">
                        </a>
                    </div>
                <br />class Image<br />{    <br />    private $info;<br /><br />    private $image;<br />    public $type;<br />    public function __construct($src)<br />    {<br /><br />        $this->info=getimagesize($src);<br />        $this->type=image_type_to_extension($this->info['2'],false);<br />        $fun="imagecreatefrom{$this->type}";<br />        $this->image=$fun($src);<br />    }<br />    /**<br />     * 文字水印<br />     * @param  [type]  $font     字体<br />     * @param  [type]  $content  内容<br />     * @param  [type]  $size     文字大小<br />     * @param  [type]  $col      文字颜色(四元数组)<br />     * @param  array   $location 位置 <br />     * @param  integer $angle    倾斜角度<br />     * @return [type]           <br />     */<br />    public function fontMark($font,$content,$size,$col,$location,$angle=0){<br />        $col=imagecolorallocatealpha($this->image, $col['0'], $col['1'], $col['2'],$col['3']);<br /><br />        imagettftext($this->image, $size, $angle, $location['0'], $location['1'], $col,$font,$content);<br />    }<br />    <br />    /**<br />     * 图片水印<br />     * @param  [type] $imageMark 水印图片地址<br />     * @param  [type] $dst       水印图片在原图片中的位置<br />     * @param  [type] $pct       透明度<br />     * @return [type]            <br />     */<br />    public function imageMark($imageMark,$dst,$pct){<br />        $info2=getimagesize($imageMark);<br />        $type=image_type_to_extension($info2['2'],false);<br />        $func2="imagecreatefrom".$type;<br />        $water=$func2($imageMark);<br /><br />        imagecopymerge($this->image, $water, $dst[0], $dst[1], 0, 0, $info2['0'], $info2['1'], $pct);<br />        imagedestroy($water);<br /><br />    }<br />    /**<br />     * 压缩图片<br />     * @param  [type] $thumbSize 压缩图片大小<br />     * @return [type]            [description]<br />     */<br />    public function thumb($thumbSize){<br />        $imageThumb=imagecreatetruecolor($thumbSize[0], $thumbSize[1]);<br />        <br />        imagecopyresampled($imageThumb, $this->image, 0, 0, 0, 0, $thumbSize[0], $thumbSize[1], $this->info['0'], $this->info['1']);<br />        imagedestroy($this->image);<br />        $this->image=$imageThumb;<br />    }<br />    /**<br />    * 裁剪图片<br />     * @param  [type] $cutSize  裁剪大小<br />     * @param  [type] $location 裁剪位置<br />     * @return [type]           [description]<br />     */<br />     public function cut($cutSize,$location){<br />         $imageCut=imagecreatetruecolor($cutSize[0],$cutSize[1]);<br /><br />         imagecopyresampled($imageCut, $this->image, 0, 0, $location[0], $location[1],$cutSize[0],$cutSize[1],$cutSize[0],$cutSize[1]);<br />         imagedestroy($this->image);<br />         $this->image=$imageCut;<br />     }<br />    /**<br />     * 展现图片<br />     * @return [type] [description]<br />     */<br />    public function show(){<br />        header("content-type:".$this->info['mime']);<br /><br />        $funn="image".$this->type;<br /><br />        $funn($this->image);<br />    }<br />    /**<br />     * 保存图片<br /> * @param  [type] $newname 新图片名<br /> * @return [type]          [description]<br /> */<br />     public function save($newname){<br />         header("content-type:".$this->info['mime']);<br /><br />         $funn="image".$this->type;<br /><br />         $funn($this->image,$newname.'.'.$this->type);<br />     }<br />     public function __destruct(){<br />         imagedestroy($this->image);<br />     }<br /><br /> }<br /><br /> ?>
登录后复制


PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号