日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php gd 缩略图,[PHP GD库]①0--缩略图封装

發布時間:2025/3/12 php 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php gd 缩略图,[PHP GD库]①0--缩略图封装 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

/** 返回圖片信息

* @param $filename

*/

function getImageInfo($filename)

{

if (@!$info = getimagesize($filename)) {

exit("文件不是真實圖片");

}

/**

* array

* 0 => int 756

* 1 => int 960

* 2 => int 2

* 3 => string 'width="756" height="960"' (length=24)

* 'bits' => int 8

* 'channels' => int 3

* 'mime' => string 'image/jpeg' (length=10)

*/

$fileInfo['width'] = $info[0];//756

$fileInfo['height'] = $info[1];//960

$mime = image_type_to_mime_type($info[2]);//image/jpeg

$createFun = str_replace('/', 'createfrom', $mime);

$outFun = str_replace('/', '', $mime);

$fileInfo['createFun'] = $createFun;

$fileInfo['outFun'] = $outFun;

$fileInfo['ext'] = strtolower(image_type_to_extension($info[2]));

return $fileInfo;

}

/**

* 形成縮略圖

* @param $filename 文件名

* @param string $dest 縮略圖保存路徑,默認'thumb'

* @param string $pre 默認前綴thumb_

* @param null $dst_w 最大寬度

* @param null $dst_h 最大高度

* @param float $scale 默認縮放比例

* @param boolean $delSource 是否刪除源文件標志

* @return string 最終保存路徑及文件名

*

*/

function thumb($filename, $dest = 'thumb', $pre = 'thumb_',

$dst_w = null, $dst_h = null, $scale = 0.5, $delSource = false)

{

$fileInfo = getImageInfo($filename);

$src_w = $fileInfo['width'];

$src_h = $fileInfo['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 = $fileInfo['createFun']($filename);

imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);

if ($dest && !file_exists($dest)) {

mkdir($dest, 07777, true);

}

$randNum = mt_rand(100000, 999999);

$dstName = "{$pre}{$randNum}" . $fileInfo['ext'];

$destination = $dest ? $dest . '/' . $dstName : $dstName;

$fileInfo['outFun']($dst_image, $destination);

imagedestroy($src_image);

imagedestroy($dst_image);

if ($delSource) {

@unlink($filename);

}

return $destination;

}

?>

總結

以上是生活随笔為你收集整理的php gd 缩略图,[PHP GD库]①0--缩略图封装的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。