1. [PHP]代码
<?php /** * @abstract 生成图片的缩略图,可以指定任意尺寸,生成的图片为png格式 * @example * $file = 'test.png'; * $th =new Thumbnail(); * $th->GenerateThumbnail($file, 400, 500); * */ class Thumbnail{ /** * @var string $from 源图片 */ private $from; /** * @var string $name 缩略图的文件名 */ private $name = ''; /** * @var 原图宽 */ private $rWidth; /** * @var 原图高 */ private $rHeight; /** * @var 缩略图宽 */ private $tWidth; /** * @var 缩略图高 */ private $tHeight; /** * @var 实际缩放到的宽度 */ private $width; /** * @var 实际缩放到的高度 */ private $height; public function __construct(){ try{ if(!function_exists('gd_info')){ throw new Exception('Must GD extension is enabled'); } } catch(Exception $e){ $msg = 'class ' . __CLASS__ . ' Error:' . $e->getMessage(); echo $msg; exit; } } /** * @var $from 原图像 * @var $width 生成的缩略图的宽 * @var $height 生成缩略图的高 * @var $name 生成的缩略图的文件名,不带后缀 * @return string 生成的缩略图 */ public function GenerateThumbnail($from, $width, $height, $name=''){ try{ if(!file_exists($from)){ throw new Exception('File does not exist'); } if($width <= 0){ throw new Exception('The width is invalid'); } if($height <= 0){ throw new Exception('The height is invalid'); } $this->from = $from; $this->tWidth = $width; $this->tHeight = $height; if(!empty($name)){ $this->name = $name; } else{ $this->name = date('Ymd') . mt_rand(0, 9999); } $this->createThumbnail(); } catch(Exception $e){ $msg = 'class ' . __CLASS__ . ' Error:' . $e->getMessage(); echo $msg; exit; } } public function getThumbnail(){ return $this->name; } /** * 生成缩略图文件 */ private function createThumbnail(){ try{ //读取原始图像信息 $sourceInfo = getimagesize($this->from); $this->rWidth = $sourceInfo[0]; $this->rHeight = $sourceInfo[1]; //创建缩略图图像资源句柄 $new_pic = imagecreatetruecolor($this->tWidth, $this->tHeight); //原图绘制到缩略图的x、y坐标 $x = 0; $y = 0; //创建原始图像资源句柄 $source_pic = ''; switch ($sourceInfo[2]){ case 1: $source_pic = imagecreatefromgif($this->from); //gif break; case 2: $source_pic = imagecreatefromjpeg($this->from); //jpg break; case 3: $source_pic = imagecreatefrompng($this->from); //png break; default: throw new Exception('Does not support this type of image'); } //计算缩放后图像实际大小 //原图宽高均比缩略图大 if($this->rWidth > $this->tWidth && $this->rHeight > $this->tHeight){ $midw = ($this->rWidth - $this->tWidth) / $this->rWidth; //宽缩小的比例 $midh = ($this->rHeight - $this->tHeight) / $this->rHeight; //高缩小的比例 //那个缩小的比例大以那个为准 if($midw > $midh){ $this->width = $this->tWidth; $this->height = $this->rHeight - floor($this->rHeight * $midw); $y = ($this->tHeight - $this->height) / 2; } else{ $this->width = $this->rWidth - floor($this->rWidth * $midh); $this->height = $this->tHeight; $x = ($this->tWidth - $this->width) / 2; } } //原图宽高均比缩略图小 elseif($this->rWidth < $this->tWidth && $this->rHeight < $this->tHeight){ $midw = ($this->tWidth - $this->rWidth) / $this->rWidth; //宽放大的比例 $midh = ($this->tHeight - $this->rHeight) / $this->rHeight; //高放大的比例 //那个放大的比例小以那个为准 if($midw < $midh){ $this->width = $this->tWidth; $this->height = $this->rHeight + floor($this->rHeight * $midw); $y = ($this->tHeight - $this->height) / 2; } else{ $this->width = $this->rWidth + floor($this->rWidth * $midh); $this->height = $this->tHeight; $x = ($this->tWidth - $this->width) / 2; } } //原图宽小于缩略图宽,原图高大于缩略图高 elseif($this->rWidth < $this->tWidth && $this->rHeight > $this->tHeight){ $mid = ($this->rHeight - $this->tHeight) / $this->rHeight; //高缩小的比例 $this->width = $this->rWidth - floor($this->rWidth * $mid); $this->height = $this->rHeight - floor($this->rHeight * $mid); $x = ($this->tWidth - $this->width) / 2; $y = ($this->tHeight - $this->height) / 2; } //原图宽大于缩略图宽,原图高小于缩略图高 elseif($this->rWidth > $this->tWidth && $this->rHeight < $this->tHeight){ $mid = ($this->rWidth - $this->tWidth) / $this->rWidth; //宽缩小的比例 $this->width = $this->rWidth - floor($this->rWidth * $mid); $this->height = $this->rHeight - floor($this->rHeight * $mid); $x = ($this->tWidth - $this->width) / 2; $y = ($this->tHeight - $this->height) / 2; } else{ throw new Exception('Resize error'); } //给缩略图添加白色背景 $bg = imagecolorallocate($new_pic, 255, 255, 255); imagefill($new_pic, 0, 0, $bg); //缩小原始图片到新建图片 imagecopyresampled($new_pic, $source_pic, $x, $y, 0, 0, $this->width, $this->height, $this->rWidth, $this->rHeight); //输出缩略图到文件 imagepng($new_pic, $this->name.'.png'); imagedestroy($new_pic); imagedestroy($source_pic); } catch(Exception $e){ $msg = 'class ' . __CLASS__ . ' Error:' . $e->getMessage(); echo $msg; exit; } } }

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver Mac版
視覺化網頁開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能