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 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

WebStorm Mac 버전
유용한 JavaScript 개발 도구

Eclipse용 SAP NetWeaver 서버 어댑터
Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

맨티스BT
Mantis는 제품 결함 추적을 돕기 위해 설계된 배포하기 쉬운 웹 기반 결함 추적 도구입니다. PHP, MySQL 및 웹 서버가 필요합니다. 데모 및 호스팅 서비스를 확인해 보세요.

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

Dreamweaver Mac版
시각적 웹 개발 도구
