# 圖片縮略實現
## 指定長寬縮略
指定長、寬實現圖片的縮略效果,縮略圖尺寸模仿京東圖片的尺寸。
```
<?php
$filename = './images/jd.jpg';
$file_info = getimagesize($filename);
if ($file_info) {
list($width, $height) = $file_info;
} else {
die("文件不是真實圖片");
}
$src_image = imagecreatefromjpeg($filename);
$dst_image_50 = imagecreatetruecolor(50, 50);
$dst_image_270 = imagecreatetruecolor(270, 270);
imagecopyresampled($dst_image_50, $src_image, 0, 0, 0, 0, 50, 50, $width, $height);
imagecopyresampled($dst_image_270,$src_image,0,0,0,0,270,270,$width,$height);
imagejpeg($dst_image_50,'./images/jd_50x50.jpg');
imagejpeg($dst_image_270,'./images/jd_270x270.jpg');
imagedestroy($dst_image_50);
imagedestroy($dst_image_270);
imagedestroy($src_image);
```
## 等比例縮放
指定縮放比例,最大寬度和高度,等比例縮放,可以對縮略圖文件添加前綴,選擇是否刪除縮略圖源文件
```
<?php
/**
* @param string $file_name 文件名
* @param string $dst_path 縮略圖保存路徑
* @param string $prefix 縮略圖保存的前綴
* @param null $dst_w 目標的最大寬度
* @param null $dst_h 目標的最大高度
* @param float $scale 縮放比例
* @param bool $delete_source 是否刪除源文件的標志
*/
function thumb($file_name, $dst_path = 'thumb', $prefix = 'thumb_', $dst_w = null, $dst_h = null, $scale = 0.5, $delete_source = false)
{
$file_info = getImageInfo($file_name);
$src_w = $file_info['width'];
$src_h = $file_info['height'];
// 如果指定最大寬度和高度按照等比例縮放進行處理
if (is_numeric($dst_w) && is_numeric($dst_h)) {
$ratio_orig = $src_w / $src_h;
if ($dst_w / $dst_h > $ratio_orig) {
$dst_w = $dst_h * $ratio_orig;
} else {
$dst_h = $dst_w / $ratio_orig;
}
} else {
$dst_w = ceil($src_w * $scale);
$dst_h = ceil($src_h * $scale);
}
$dst_image = imagecreatetruecolor($dst_w, $dst_h);
$src_image = $file_info['create_fun']($file_name);
imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
// 檢測目標目錄是否存在
if ($dst_path && !file_exists($dst_path)) {
mkdir($dst_path, 0777, true);
}
$dst_name = "{$prefix}{$file_info['file_name']}" . $file_info['extension'];
$destination = $dst_path ? $dst_path . '/' . $dst_name : $dst_name; // 保存的文件名
$file_info['out_fun']($dst_image, $destination);
if ($delete_source) {
@unlink($file_name);
}
imagedestroy($src_image);
imagedestroy($dst_image);
}
/**
* @param $filename string 文件名
* @return array
*/
function getImageInfo($filename)
{
if (!$info = getimagesize($filename)) {
exit('文件不是真實圖片!');
}
$file_info['file_name'] = pathinfo($filename)['filename'];
$file_info['width'] = $info[0];
$file_info['height'] = $info[1];
$mime = image_type_to_mime_type($info[2]);
$file_info['extension'] = image_type_to_extension($info[2], true);
$file_info['create_fun'] = str_replace('/', 'createfrom', $mime);
$file_info['out_fun'] = str_replace('/', '', $mime);
return $file_info;
}
```
- 開始
- PHP配置參數的介紹
- PHP代碼優化
- php中的命名空間
- PHP文件上傳類
- PHP文件下載
- PHP驗證碼
- ThinkPHP3.2 框架函數
- A函數:實例化控制器
- C函數:設置和獲取配置參數
- D函數:實例化模型
- F 函數:快速緩存設置和存取
- M函數:例化模型(無需定義模型類)
- L函數:設置和獲取語言變量
- S 函數:緩存設置和存取
- R函數:直接調用控制器的操作方法
- U函數:URL地址生成
- I 函數:安全獲取系統輸入變量
- 日志
- ThinkPHP在關閉調試模式導致函數被緩存
- MySQL觸發器使用時遇到的坑
- PHP常用函數
- 五一回家記錄
- window的PHP開發(wamp)下安裝redis擴展
- Windows下安裝使用Redis
- PHP7新特性
- 利用 phpmailer 類實現隊列發送郵件
- GD 庫圖像處理
- 檢測 PHP 模塊是否開啟
- GD 庫操作一般步驟
- GD 庫繪畫改變字體
- GD 繪制驗證碼
- GD 縮略圖實現
- GD 繪制水印
- 日期時間函數庫
- PHP 函數
- 無限極分類