0

0

给图片生成缩略图和加版权的类

php中文网

php中文网

发布时间:2016-05-22 17:21:11

|

1837人浏览过

|

来源于php中文网

原创

最近几天看了一下php的图片处理方面的功能,以前这方面的需求比较少,也就没怎么看,最近有空看了一下。感觉图片处理一些简单的功能还可以,复杂的就算了,gd库都2.0.1了,还是不支持中文,看了几篇文章,想使用中文只能先将gb2312转换成unicode再写入图片,太麻烦了,索性只使用英文算了。 

在图像生成部分可以定义图片的最大高,宽,比较适用于新闻及相册等系统。 

GD2.0.1在图片处理上有很大提高,我试了下imageCopyResized和imageCopyResampled,后者处理的图片明显好于前者,据手册上讲后者对改变大小后的图片重新采样基本保持不失真,生成缩略图的效果还真不错。

sourcePath = $sourcePath;
        $this->galleryPath = $galleryPath;
        $this->fontName = $fontPath . "04B_08__.TTF";
    }
    //==========================================
    // 函数: makeThumb($sourFile,$width=128,$height=128)
    // 功能: 生成缩略图(输出到浏览器)
    // 参数: $sourFile 图片源文件
    // 参数: $width 生成缩略图的宽度
    // 参数: $height 生成缩略图的高度
    // 返回: 0 失败 成功时返回生成的图片路径
    //==========================================
    function makeThumb($sourFile, $width = 128, $height = 128) {
        $imageInfo = $this->getInfo($sourFile);
        $sourFile = $this->sourcePath . $sourFile;
        $newName = substr($imageInfo["name"], 0, strrpos($imageInfo["name"], ".")) . "_thumb.jpg";
        switch ($imageInfo["type"]) {
            case 1: //gif
                $img = imagecreatefromgif($sourFile);
                break;
            case 2: //jpg
                $img = imagecreatefromjpeg($sourFile);
                break;
            case 3: //png
                $img = imagecreatefrompng($sourFile);
                break;
            default:
                return 0;
                break;
        }
        if (!$img) return 0;
        $width = ($width > $imageInfo["width"]) ? $imageInfo["width"] : $width;
        $height = ($height > $imageInfo["height"]) ? $imageInfo["height"] : $height;
        $srcW = $imageInfo["width"];
        $srcH = $imageInfo["height"];
        if ($srcW * $width > $srcH * $height) $height = round($srcH * $width / $srcW);
        else $width = round($srcW * $height / $srcH);
        //*
        if (function_exists("imagecreatetruecolor")) //GD2.0.1
        {
            $new = imagecreatetruecolor($width, $height);
            ImageCopyResampled($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]);
        } else {
            $new = imagecreate($width, $height);
            ImageCopyResized($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]);
        }
        //*/
        if ($this->toFile) {
            if (file_exists($this->galleryPath . $newName)) unlink($this->galleryPath . $newName);
            ImageJPEG($new, $this->galleryPath . $newName);
            return $this->galleryPath . $newName;
        } else {
            ImageJPEG($new);
        }
        ImageDestroy($new);
        ImageDestroy($img);
    }
    //==========================================
    // 函数: waterMark($sourFile, $text)
    // 功能: 给图片加水印
    // 参数: $sourFile 图片文件名
    // 参数: $text 文本数组(包含二个字符串)
    // 返回: 1 成功 成功时返回生成的图片路径
    //==========================================
    function waterMark($sourFile, $text) {
        $imageInfo = $this->getInfo($sourFile);
        $sourFile = $this->sourcePath . $sourFile;
        $newName = substr($imageInfo["name"], 0, strrpos($imageInfo["name"], ".")) . "_mark.jpg";
        switch ($imageInfo["type"]) {
            case 1: //gif
                $img = imagecreatefromgif($sourFile);
                break;
            case 2: //jpg
                $img = imagecreatefromjpeg($sourFile);
                break;
            case 3: //png
                $img = imagecreatefrompng($sourFile);
                break;
            default:
                return 0;
                break;
        }
        if (!$img) return 0;
        $width = ($this->maxWidth > $imageInfo["width"]) ? $imageInfo["width"] : $this->maxWidth;
        $height = ($this->maxHeight > $imageInfo["height"]) ? $imageInfo["height"] : $this->maxHeight;
        $srcW = $imageInfo["width"];
        $srcH = $imageInfo["height"];
        if ($srcW * $width > $srcH * $height) $height = round($srcH * $width / $srcW);
        else $width = round($srcW * $height / $srcH);
        //*
        if (function_exists("imagecreatetruecolor")) //GD2.0.1
        {
            $new = imagecreatetruecolor($width, $height);
            ImageCopyResampled($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]);
        } else {
            $new = imagecreate($width, $height);
            ImageCopyResized($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]);
        }
        $white = imageColorAllocate($new, 255, 255, 255);
        $black = imageColorAllocate($new, 0, 0, 0);
        $alpha = imageColorAllocateAlpha($new, 230, 230, 230, 40);
        //$rectW = max(strlen($text[0]),strlen($text[1]))*7;
        ImageFilledRectangle($new, 0, $height - 26, $width, $height, $alpha);
        ImageFilledRectangle($new, 13, $height - 20, 15, $height - 7, $black);
        ImageTTFText($new, 4.9, 0, 20, $height - 14, $black, $this->fontName, $text[0]);
        ImageTTFText($new, 4.9, 0, 20, $height - 6, $black, $this->fontName, $text[1]);
        //*/
        if ($this->toFile) {
            if (file_exists($this->galleryPath . $newName)) unlink($this->galleryPath . $newName);
            ImageJPEG($new, $this->galleryPath . $newName);
            return $this->galleryPath . $newName;
        } else {
            ImageJPEG($new);
        }
        ImageDestroy($new);
        ImageDestroy($img);
    }
    //==========================================
    // 函数: displayThumb($file)
    // 功能: 显示指定图片的缩略图
    // 参数: $file 文件名
    // 返回: 0 图片不存在
    //==========================================
    function displayThumb($file) {
        $thumbName = substr($file, 0, strrpos($file, ".")) . "_thumb.jpg";
        $file = $this->galleryPath . $thumbName;
        if (!file_exists($file)) return 0;
        $html = ".net/$file\">";
        echo $html;
    }
    //==========================================
    // 函数: displayMark($file)
    // 功能: 显示指定图片的水印图
    // 参数: $file 文件名
    // 返回: 0 图片不存在
    //==========================================
    function displayMark($file) {
        $markName = substr($file, 0, strrpos($file, ".")) . "_mark.jpg";
        $file = $this->galleryPath . $markName;
        if (!file_exists($file)) return 0;
        $html = "";
        echo $html;
    }
    //==========================================
    // 函数: getInfo($file)
    // 功能: 返回图像信息
    // 参数: $file 文件路径
    // 返回: 图片信息数组
    //==========================================
    function getInfo($file) {
        $file = $this->sourcePath . $file;
        $data = getimagesize($file);
        $imageInfo["width"] = $data[0];
        $imageInfo["height"] = $data[1];
        $imageInfo["type"] = $data[2];
        $imageInfo["name"] = basename($file);
        return $imageInfo;
    }
}
?>

---------------------------------- 

下面是使用方法 

这个类使用了一个04B_08__.TTF字体 

使用类的时候指定该字体路径即可 

迦恩计算机资源网源码(图书销售类)
迦恩计算机资源网源码(图书销售类)

采用三层架构开发,前台集成了产品在线展示,用户注册、在线调查、在线投稿后台有类别管理\图书管理\订单管理\会员管理\配送范围管理\邮件列表\广告管理\友情链接管理等后台添加图书时自动生成缩略图和文字水印主要参考了petshop的设计架构、使用了Asp.net2.0中很多MemberShip、master等新功能后台管理地址/web/admin/ 超级管理员账号密码均为aspx1特别提示:该系统需要

下载

----------------------------------- 

maxWidth = $img->maxHeight = 300;
$img->toFile = true;
$img->waterMark("mm.jpg", $text);
$img->makeThumb("mm.jpg");
$img->displayThumb("mm.jpg");
$img->displayMark("mm.jpg");


文章地址:

转载随意^^请带上本文地址!

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

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
go语言 注释编码
go语言 注释编码

本专题整合了go语言注释、注释规范等等内容,阅读专题下面的文章了解更多详细内容。

2

2026.01.31

go语言 math包
go语言 math包

本专题整合了go语言math包相关内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

go语言输入函数
go语言输入函数

本专题整合了go语言输入相关教程内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

golang 循环遍历
golang 循环遍历

本专题整合了golang循环遍历相关教程,阅读专题下面的文章了解更多详细内容。

0

2026.01.31

Golang人工智能合集
Golang人工智能合集

本专题整合了Golang人工智能相关内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

2026赚钱平台入口大全
2026赚钱平台入口大全

2026年最新赚钱平台入口汇总,涵盖任务众包、内容创作、电商运营、技能变现等多类正规渠道,助你轻松开启副业增收之路。阅读专题下面的文章了解更多详细内容。

76

2026.01.31

高干文在线阅读网站大全
高干文在线阅读网站大全

汇集热门1v1高干文免费阅读资源,涵盖都市言情、京味大院、军旅高干等经典题材,情节紧凑、人物鲜明。阅读专题下面的文章了解更多详细内容。

73

2026.01.31

无需付费的漫画app大全
无需付费的漫画app大全

想找真正免费又无套路的漫画App?本合集精选多款永久免费、资源丰富、无广告干扰的优质漫画应用,涵盖国漫、日漫、韩漫及经典老番,满足各类阅读需求。阅读专题下面的文章了解更多详细内容。

67

2026.01.31

漫画免费在线观看地址大全
漫画免费在线观看地址大全

想找免费又资源丰富的漫画网站?本合集精选2025-2026年热门平台,涵盖国漫、日漫、韩漫等多类型作品,支持高清流畅阅读与离线缓存。阅读专题下面的文章了解更多详细内容。

19

2026.01.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

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