PHP生成縮略圖實(shí)例講解
封裝的方法函數(shù):
<?php
/**
* 生成縮略圖
* $imgSrc 圖片源路徑
* $thumbWidth 縮略圖寬度
* $thumbHeight 縮略圖高度
* $thumbSrc 縮略圖路徑
* $isCut 是否剪切圖片
*/
function createThumbImg($imgSrc, $thumbWidth, $thumbHeight, $thumbSrc, $isCut = false) {
//1.獲取圖片的類型
$type = substr(strrchr($imgSrc, "."), 1);
//2.初始化圖象
if ($type == "jpg" || $type == "jpeg") {
//創(chuàng)建一塊畫布,并從JPEG文件或URL地址載入一副圖像
$sourceImg = imagecreatefromjpeg($imgSrc);
}elseif ($type == "gif") {
//創(chuàng)建一塊畫布,并從GIF文件或URL地址載入一副圖像
$sourceImg = imagecreatefromgif($imgSrc);
}elseif ($type == "png") {
//創(chuàng)建一塊畫布,并從PNG文件或URL地址載入一副圖像
$sourceImg = imagecreatefrompng($imgSrc);
}
elseif ($type == "wbmp") {
//創(chuàng)建一塊畫布,并從WBMP文件或URL地址載入一副圖像
$sourceImg = imagecreatefromwbmp($imgSrc);
}
//取得圖像寬度
$width = imagesx($sourceImg);
//取得圖像高度
$height = imagesy($sourceImg);
//3.生成圖象
//縮略圖的圖象比例
$scale = ($thumbWidth) / ($thumbHeight);
//源圖片的圖象比例
$ratio = ($width) / ($height);
if (($isCut) == 1) {
//高度優(yōu)先
if ($ratio >= $scale) {
//創(chuàng)建真彩圖像資源(imagecreatetruecolor()函數(shù)使用GDLibrary創(chuàng)建新的真彩色圖像)
$newimg = imagecreatetruecolor($thumbWidth, $thumbHeight);
//圖像處理
imagecopyresampled($newimg, $sourceImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, (($height) * $scale), $height);
//以JPEG格式將圖像輸出到瀏覽器或文件
ImageJpeg($newimg, $thumbSrc);
}
//寬度優(yōu)先
if ($ratio < $scale) {
$newimg = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresampled($newimg, $sourceImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $width, (($width) / $scale));
ImageJpeg($newimg, $thumbSrc);
}
} else {
if ($ratio >= $scale) {
$newimg = imagecreatetruecolor($thumbWidth, ($thumbWidth) / $ratio);
imagecopyresampled($newimg, $sourceImg, 0, 0, 0, 0, $thumbWidth, ($thumbWidth) / $ratio, $width, $height);
ImageJpeg($newimg, $thumbSrc);
}
if ($ratio < $scale) {
$newimg = imagecreatetruecolor(($thumbHeight) * $ratio, $thumbHeight);
imagecopyresampled($newimg, $sourceImg, 0, 0, 0, 0, ($thumbHeight) * $ratio, $thumbHeight, $width, $height);
ImageJpeg($newimg, $thumbSrc);
}
}
//銷毀圖像
ImageDestroy($sourceImg);
}
?>
調(diào)用示例:
<?php //圖片源路徑 $imgSrc="D:/PHP/test/demo.jpg"; //縮略圖路徑 $thumbSrc="D:/PHP/test/thumb.jpg"; createThumbImg($path,100,100,$thumbSrc); ?>
到此這篇關(guān)于PHP生成縮略圖實(shí)例講解的文章就介紹到這了,更多相關(guān)PHP生成縮略圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
php操作XML、讀取數(shù)據(jù)和寫入數(shù)據(jù)的實(shí)現(xiàn)代碼
這篇文章主要介紹了php操作XML、讀取數(shù)據(jù)和寫入數(shù)據(jù)的實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-08-08
php array_values 返回?cái)?shù)組的值實(shí)例詳解
php array_values 函數(shù)用于返回?cái)?shù)組中所有的值,注意該函數(shù)將為新數(shù)組建立數(shù)組索引,原來的文字索引將不存在。本文章向大家講解array_values函數(shù)的基本語法及使用實(shí)例,需要的朋友可以參考下2016-11-11
php設(shè)計(jì)模式之工廠方法模式分析【星際爭(zhēng)霸游戲案例】
這篇文章主要介紹了php設(shè)計(jì)模式之工廠方法模式,結(jié)合具體星際爭(zhēng)霸游戲案例形式分析了PHP工廠方法模式的相關(guān)原理、使用方法及操作注意事項(xiàng),需要的朋友可以參考下2020-01-01
CI框架學(xué)習(xí)筆記(二) -入口文件index.php
本文介紹的是CI框架的入口文件index.php的相關(guān)資料,需要的朋友可以參考下2014-10-10
php+mongodb判斷坐標(biāo)是否在指定多邊形區(qū)域內(nèi)的實(shí)例
本篇文章主要介紹了php+mongodb判斷坐標(biāo)是否在指定多邊形區(qū)域內(nèi)的實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-10-10
推薦10個(gè)提供免費(fèi)PHP腳本下載的網(wǎng)站
這篇文章主要介紹了推薦10個(gè)提供免費(fèi)PHP腳本下載的網(wǎng)站,需要的朋友可以參考下2014-12-12

