搜尋
首頁php教程PHP源码php生成任意缩放图片
php生成任意缩放图片May 25, 2016 pm 05:03 PM
php

1. [PHP]代码   

<?php
/**
 * @abstract 生成图片的缩略图,可以指定任意尺寸,生成的图片为png格式
 * @example
 * $file = &#39;test.png&#39;;
 * $th =new Thumbnail();
 * $th->GenerateThumbnail($file, 400, 500);
 *
 */
class Thumbnail{
     /**
     * @var string $from 源图片
     */
     private $from;
     /**
     * @var string $name 缩略图的文件名
     */
     private $name = &#39;&#39;;
     /**
     * @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(&#39;gd_info&#39;)){
                 throw new Exception(&#39;Must GD extension is enabled&#39;);
             }
         }
         catch(Exception $e){
             $msg = &#39;class &#39; . __CLASS__ . &#39; Error:&#39; . $e->getMessage();
             echo $msg;
             exit;
         }
     }
     /**
     * @var $from 原图像
     * @var $width 生成的缩略图的宽
     * @var $height 生成缩略图的高
     * @var $name 生成的缩略图的文件名,不带后缀
     * @return string 生成的缩略图
     */
     public function GenerateThumbnail($from, $width, $height, $name=&#39;&#39;){
         try{
             if(!file_exists($from)){
                 throw new Exception(&#39;File does not exist&#39;);
             }
             if($width <= 0){
                 throw new Exception(&#39;The width is invalid&#39;);
             }
             if($height <= 0){
                 throw new Exception(&#39;The height is invalid&#39;);
             }
             $this->from = $from;
             $this->tWidth = $width;
             $this->tHeight = $height;
             if(!empty($name)){
                 $this->name = $name;
             }
             else{
                 $this->name = date(&#39;Ymd&#39;) . mt_rand(0, 9999);
             }
             $this->createThumbnail();
         }
         catch(Exception $e){
             $msg = &#39;class &#39; . __CLASS__ . &#39; Error:&#39; . $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 = &#39;&#39;;
             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(&#39;Does not support this type of image&#39;);
             }
             //计算缩放后图像实际大小
             //原图宽高均比缩略图大
             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(&#39;Resize error&#39;);
             }

             //给缩略图添加白色背景
             $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.&#39;.png&#39;);
             imagedestroy($new_pic);
             imagedestroy($source_pic);
         }
         catch(Exception $e){
             $msg = &#39;class &#39; . __CLASS__ . &#39; Error:&#39; . $e->getMessage();
             echo $msg;
             exit;
         }
     }
}

                   

                   

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

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

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

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

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

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

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

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

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

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

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

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

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

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

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

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

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

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

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能