PHP 处理图片的类实现代码

2021-01-23 20:19:208

复制代码 代码如下:

/**

* author:yagas

* email:yagas60@21cn.com

*/

class Image

{

/** 类保护变量 */

protected $th_width = 100;

protected $th_height = 50;

protected $quality = 85; //图片质量

protected $transparent = 50; //水印透明度

protected $background = "255,255,255"; //背景颜色

/**

* 生成缩略图文件

* @param $src 原图文件

* @param $dst 目标文件

*/

public function thumb($src, $dst=null, $output=true)

{

$thumb = array($this->th_width, $this->th_height);

$this->scale($src, $thumb, $dst, $output);

}

/**

* 对图片按百分比进行缩放处理

* @param string $src 原图文件

* @param string $dst 输入的目标文件

* @param float/array $zoom 缩放比例,浮点类型时按百分比绽放,数组类型时按指定大小时行缩放

* @param boolean $output 是否生成文件输出

*/

public function scale($src, $dst=null, $zoom=1, $output=true)

{

if(!file_exists($src)) die('File not exists.');

if(!$zoom) die('the zoom undefine.');

$src_im = $this->IM($src);

$old_width = imagesx($src_im);

if(is_float($zoom)) {

//按百分比进行缩放

$new_width = $old_width * $zoom;

}

elseif(is_array($zoom)) {

//明确的缩放尺寸

$new_width = $zoom[0];

}

//是否定义的缩放的高度

if(!isset($zoom[1])) {

//等比例缩放

$resize_im = $this->imageresize($src_im, $new_width);

}

else {

//非等比例缩放

$resize_im = $this->imageresize($src_im, $new_width, $zoom[1]);

}

if(!$output) {

header("Content-type: image/jpeg");

imagejpeg($resize_im, null, $this->quality);

}

else {

$new_file = empty($dst)? $src:$dst;

imagejpeg($resize_im, $new_file, $this->quality);

}

imagedestroy($im);

imagedestroy($nIm);

}

/**

* 对图片进行裁切

* @param $src 原始文件

* @param $dst 目标文件

* @param $output 是否生成目标文件

*/

public function capture($src, $dst=null, $output=true) {

if(!file_exists($src)) die('File not exists.');

$width = $this->th_width;

$height = $this->th_height;

$src_im = $this->IM($src);

$old_width = imagesx($src_im);

$old_height = imagesy($src_im);

$capture = imagecreatetruecolor($width, $height);

$rgb = explode(",", $this->background);

$white = imagecolorallocate($capture, $rgb[0], $rgb[1], $rgb[2]);

imagefill($capture, 0, 0, $white);

//当图片大于缩略图时进行缩放

if($old_width > $width && $old_height>$height) {

$resize_im = $this->imageresize($src_im, $width);

//图片比例不合规范时,重新计算比例进行裁切

if(imagesy($resize_im) < $height) {

$proportion = $old_height/$this->th_height;

$resize_im = $this->imageresize($src_im, $old_width/$proportion);

}

$posx = 0;

$posy = 0;

}

else {

//图片小于缩略图时将图片居中显示

$posx = ($width-$old_width)/2;

$posy = ($height-$old_height)/2;

$resize_im = $src_im;

}

imagecopy($capture, $resize_im, $posx, $posy, 0, 0, imagesx($resize_im), imagesy($resize_im));

if(!$output) {

header("Content-type: image/jpeg");

imagejpeg($capture, null, $this->quality);

}

else {

$new_file = empty($dst)? $src:$dst;

imagejpeg($capture, $new_file, $this->quality);

}

imagedestroy($src_im);

@imagedestroy($resize_im);

imagedestroy($capture);

}

/**

* 写入水印图片

* @param $src 需要写入水印的图片

* @param $mark 水印图片

* @param $transparent 水印透明度

*/

public function mark($src, $mark, $dst='', $output=true)

{

$mark_info = getimagesize($mark);

$src_info = getimagesize($src);

list($mw,$mh) = $mark_info;

list($sw,$sh) = $src_info;

$px = $sw - $mw;

$py = $sh - $mh;

$im = $this->IM($src);

$mim = $this->IM($mark);

imagecopymerge($im, $mim, $px, $py, 0, 0, $mw, $mh, $this->transparent);

if($output){

$new_file = empty($dst)? $src:$dst;

imagejpeg($im, $new_file, $this->quality);

}

else

{

header('Content-type: image/jpeg');

imagejpeg($im);

}

imagedestroy($im);

imagedestroy($mim);

}

/**

* 通过文件,获取不同的GD对象

*/

protected function IM($file)

{

if(!file_exists($file)) die('File not exists.');

$info = getimagesize($file);

switch($info['mime'])

{

case 'image/gif':

$mim = imagecreatefromgif($file);

break;

case 'image/png':

$mim = imagecreatefrompng($file);

imagealphablending($mim, false);

imagesavealpha($mim, true);

break;

case 'image/jpeg':

$mim = imagecreatefromjpeg($file);

break;

default:

die('File format errors.');

}

return $mim;

}

/**

* 对图片进行缩放的处理

* @param resource $src_im 图像GD对象

* @param integer $width 图片的宽度

* @param integer $height 图片的高度,如果不设置高度,将对图片进行等比例缩放

* @return resuorce $im 返回一个GD对象

*/

protected function imageresize($src_im, $width, $height=null) {

$old_width = imagesx($src_im);

$old_height = imagesy($src_im);

$proportion = $old_width/$old_height;

$new_width = $width;

$new_height = is_null($height)? round($new_width / $proportion):$height;

//创建新的图象并填充默认的背景色

$im = imagecreatetruecolor($new_width, $new_height);

$rgb = explode(",", $this->background);

$white = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]);

imagefill($im, 0, 0, $white);

//对图片进行缩放

imagecopyresized($im, $src_im, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);

return $im;

}

/**

* 类变量赋值

*/

public function __set($key, $value)

{

$this->$key = $value;

}

/**

* 获取类变量值

*/

public function __get($key)

{

return $this->$key;

}

}

?>

点击这里复制本文地址

以上内容由聚米学院网友整理呈现,如对侵犯您的权益,请联系邮箱:fzsbm@qq.com

留言评论

php 自动处理小图的代码,PHP 处理图片的类实现代码相关推荐

  1. java代码加载_java类中代码加载顺序

    package org.senssic; /** * 一般顺序:静态块-->静态变量-->成员变量-->构造方法-->静态方法(或非静态方法,需要调用) * 1.静态代码块(只 ...

  2. python批量图像处理_基于python代码批量处理图片resize

    出差做PPT,要放一些图片上去,原图太大必须resize,十几张图片懒得一一处理了,最近正好在学python,最好的学习方式就是使用,于是写了一个批量处理图片resize的代码,在写的过程中,熟悉了p ...

  3. 网页广告代码php,【网页广告特效代码】精选5种常用的网页广告特效代码

    网站少不了发布一些活动广告,网页中的广告显示效果有很多种,一般是采用JS或者JQ实现多种网页广告显示效果,以下是php中文网精选5种常用的网页广告特效代码推荐给大家下载使用! 效果预览和下载地址:ht ...

  4. 【python】一个目录里面多个python程序文件,统计一下里面有多少行代码。即分别列出:代码、空行、注释的行数。

    一个目录里面多个python程序文件,统计一下里面有多少行代码.即分别列出:代码.空行.注释的行数. 题目 代码 结果 题目 一个目录里面多个python程序文件,统计一下里面有多少行代码.即分别列出 ...

  5. javascript 代码_如何使您JavaScript代码保持简单并提高其可读性

    javascript 代码 by Leonardo Lima 莱昂纳多·利马(Leonardo Lima) 如何使您JavaScript代码保持简单并提高其可读性 (How to keep your ...

  6. AI 生成的代码可信吗?编写的代码有 Bug 吗?

    编译 | 禾木木 出品 | AI科技大本营(ID:rgznai100) 即使是帮助开发人员编写软件的工具也会产生类似的bug. 目前,大部分的软件开发人员会让 AI 帮助开发者们编写代码,但是开发人员 ...

  7. 什么是整洁的代码?什么是肮脏的代码?

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试资料 来源:http://rrd.me/ekN6H 命名的艺术 注释 函 ...

  8. java gui构造工具_Java Web框架 静态代码块、构造代码块、构造函数、普通代码块 执行顺序 Decompiler JD-GUI 反编译工具...

    1.下载jd-gui-windows-1.4.0. http://jd.benow.ca/ 2.通过jd-gui.exe查看.class文件,用于分析类编译过程. 3.源文件. // 加载相应的 He ...

  9. R语言统计代码运行耗时实战:计算代码运行时间、使用proc.time函数计算代码运行时间

    R语言统计代码运行耗时实战:计算代码运行时间.使用proc.time函数计算代码运行时间 目录

最新文章

  1. hung-yi lee_p17_卷积神经网络
  2. 校招c语言面试题目及答案,C/C++学习之路(一)校招后端面试题及答案(作者回忆版)...
  3. C 语言编程 — 基本语法
  4. StrongOD快捷键说明及其例子
  5. python自学多久可以找到工作-25岁从零开始学习python还能找到工作吗?
  6. 5.11 程序示例--垃圾邮件检测-机器学习笔记-斯坦福吴恩达教授
  7. 五款软件快速解决网络故障问题
  8. 【❤️算法系列之二叉树的实现(包含前序、中序、后序遍历以及节点的查找和删除)❤️】
  9. c语言经典游戏,C语言——经典小游戏——打砖块
  10. Apache Hawq--优化笔记
  11. mysql高级操作_MySQL数据库的高级操作
  12. 理解一下策略模式,工厂模式
  13. 简单的聊天应用程序(多客户端聊天服务器) from multithread
  14. my first d3d application 哈哈哈。
  15. 【IT领导力】IT 使命、愿景和价值观声明:成功的基础
  16. [转]如何实现按键精灵的简单路点行走
  17. 仿真器和模拟器的区别
  18. 解决安装ENVI5.3报错:the installation of MSVC_2010_SP1_x64_32bit has failed
  19. 2021年P气瓶充装考试题库及P气瓶充装考试报名
  20. 计算机桌面点不进系统,电脑开机进不了桌面,小编教你电脑开机进不了桌面怎么办...

热门文章

  1. 数据库SQL的分组函数
  2. javascript 实现模拟滚动条,但不支持鼠标滚轮
  3. 配置Quartz.net Cluster以及远程管理
  4. A-Grade Browser By Yahoo
  5. python学习费用-深圳python学习费用
  6. python turtle循环图案-有趣的Python turtle绘图
  7. python精通-干货|Python学习必须精通的几个模块
  8. python贴吧爬虫-Python 爬虫练习: 爬取百度贴吧中的图片
  9. python运行慢-Python运行效率慢?因为你不知道这六大窍门!
  10. python编程在哪里写程序-第一个Python程序——在屏幕上输出文本