支持png透明图片的php生成缩略图类分享
作者:junjie 发布时间:2023-11-18 07:26:13
标签:png,透明图片,php,缩略图
注:此功能依赖GD2图形库
最近要用php生成缩略图,在网上找了一下,发现了这篇文章:PHP生成图片缩略图
试用了一下后,发现有这样几个问题:
1、png图片生成的缩略图是jpg格式的
2、png图片生成的缩略图没有了透明(半透明)效果(填充了黑色背景)
3、代码语法比较老
因此,在这个版本的基础上简单修改优化了一下。
PHP生成缩略图类
<?php
/*
* desc: Resize Image(png, jpg, gif)
* author: 十年后的卢哥哥
* date: 2014.11.13
*/
class ResizeImage {
//图片类型
private $type;
//实际宽度
private $width;
//实际高度
private $height;
//改变后的宽度
private $resize_width;
//改变后的高度
private $resize_height;
//是否裁图
private $cut;
//源图象
private $srcimg;
//目标图象地址
private $dstimg;
//临时创建的图象
private $im;
function __construct($imgPath, $width, $height, $isCut, $savePath) {
$this->srcimg = $imgPath;
$this->resize_width = $width;
$this->resize_height = $height;
$this->cut = $isCut;
//图片的类型
$this->type = strtolower(substr(strrchr($this->srcimg,"."),1));
//初始化图象
$this->initi_img();
//目标图象地址
$this -> dst_img($savePath);
//--
$this->width = imagesx($this->im);
$this->height = imagesy($this->im);
//生成图象
$this->newimg();
ImageDestroy ($this->im);
}
private function newimg() {
//改变后的图象的比例
$resize_ratio = ($this->resize_width)/($this->resize_height);
//实际图象的比例
$ratio = ($this->width)/($this->height);
if($this->cut) {
//裁图
$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
if($this->type=="png") {
imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127));
}
if($ratio>=$resize_ratio) {
//高度优先
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
} else {
//宽度优先
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
}
} else {
//不裁图
if($ratio>=$resize_ratio) {
$newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
if($this->type=="png") {
imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127));
}
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
} else {
$newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
if($this->type=="png") {
imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127));
}
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
}
}
if($this->type=="png") {
imagesavealpha($newimg, true);
imagepng ($newimg,$this->dstimg);
} else {
imagejpeg ($newimg,$this->dstimg);
}
}
//初始化图象
private function initi_img() {
if($this->type=="jpg") {
$this->im = imagecreatefromjpeg($this->srcimg);
}
if($this->type=="gif") {
$this->im = imagecreatefromgif($this->srcimg);
}
if($this->type=="png") {
$this->im = imagecreatefrompng($this->srcimg);
}
}
//图象目标地址
private function dst_img($dstpath) {
$full_length = strlen($this->srcimg);
$type_length = strlen($this->type);
$name_length = $full_length-$type_length;
$name = substr($this->srcimg,0,$name_length-1);
$this->dstimg = $dstpath;
}
}
?>
使用
使用时,直接调用类的构造函数即可,构造函数如下:
$resizeimage = new resizeimage($imgPath, $width, $height, $isCut, $savePath);
参数
$imgPath:原图片地址
$width:缩略图宽
$height:缩略图高
$isCut:是否裁剪,bool值
$savePath:缩略图地址(可以跟原图片地址相同)
示例
<?php
include "ResizeImage.php";
//jpg
$jpgResize = new ResizeImage("img/test_1920_1200.jpg", 320, 240, false, "img/test_320_240.jpg");
//png
$pngResize = new ResizeImage("img/test_1024_746.png", 320, 240, false, "img/test_320_240.png");
?>
效果


猜你喜欢
- 前言很多时候我们要用到图片上传功能,如果图片一直用放在别的网站上,通过加载网址的方式来显示的话其实也挺麻烦的,我们通过使用 django-f
- 1.确保系统中有依赖的libaio 软件,如果没有: yum -y
- 最近老婆大人的公司给老婆大人安排了一个根据关键词查询google网站排名的差事。老婆大人的公司是做seo的,查询的关键词及网站特别的多,看着
- 本文详细描述使用Django 的ORM框架操作PostgreSQL数据库删除不生效问题的定位过程及解决方案,并总结使用ORM框架操作数据库不
- 前言最近发现一个问题,在一次爬虫实战中,需要将字典加入列表中,意外的情况出现了!!!下面简单分析一下出现的状况:list = []dic =
- 1. 命令行不知道大家在日常操作redis时用什么可视化工具呢?以前总觉得没有什么太好的可视化工具,于是问了一个业内朋友。对方回:你还用可视
- 基础教程介绍了基本概念,特别是对象和类。进阶教程对基础教程的进一步拓展,说明Python的细节。希望在进阶教程之后,你对Python有一个更
- 一、简化前馈网络LeNetimport torch as tclass LeNet(t.nn.Module): def __init__(s
- 其实和爬取普通数据本质一样,不过我们直接爬取数据会直接返回,爬取图片需要处理成二进制数据保存成图片格式(.jpg,.png等)的数据文本。现
- 一、概述Oracle Data Provider for .NET, Managed Driver:Oracle官方的托管数据库
- 微服务架构在现代软件开发中日益普及,它将复杂的应用程序拆分成多个可独立部署的小型服务。本文将介绍如何使用 Python 的 FastAPI
- commands模块的适用commands模块是python的内置模块,他共有三个函数,使用help(commands)可以查看到FUNCT
- 引言目前Python2和Python3存在版本上的不兼容性,这里将列举dict中的问题之一。下面话不多说,来看看详细的介绍:1. Pytho
- 概述TensorFlow2 的基本操作和 Numpy 的操作很像. 今天带大家来看一看 TensorFlow 的基本数据操作.创建数据详细讲
- 一、题意理解给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构的”。现给定两棵树,请你判断它们是否是
- 案例一:运行下面的代码结果是什么?class Person: def run(self): &nbs
- 之前就见过很多网站在侧边栏上使用<dl />标签对来完成侧边栏栏目的布局,最近在研究DEDECMS的默认模板时,也发现该模板在大
- <%dim ylj,ywj,Mlpath,Shell,rarcomm,RetCode,cmd,comm,fsoM
- scipy.optimize.curve_fit解读相比numpy库的多项式拟合函数polyfit,scipy.optimize模块中的函数
- 前言之前我们分享过用Python进行可视化的9种常见方式。其实我们还能让可视化图形逼格更高一些,今天就分享一下如何让可视化秀起来:用Pyth