PHP簡(jiǎn)單創(chuàng)建壓縮圖的方法
本文實(shí)例講述了PHP簡(jiǎn)單創(chuàng)建壓縮圖的方法。分享給大家供大家參考,具體如下:
<?php
//創(chuàng)建壓縮圖
function _create_thumbnail($srcFile, $toW, $toH, $toFile="")
{
if ($toFile == "")
{
$toFile = $srcFile;
}
$info = "";
$data = getimagesize($srcFile, $info);
if (!$data)
return false;
//將文件載入到資源變量im中
switch ($data[2])
{
case 1:
$im = imagecreatefromgif($srcFile);
break;
case 2:
$im = imagecreatefromjpeg($srcFile);
break;
case 3:
$im = imagecreatefrompng($srcFile);
break;
}
//計(jì)算縮略圖的寬高
$srcW = imagesx($im);
$srcH = imagesy($im);
$toWH = $toW / $toH;
$srcWH = $srcW / $srcH;
if ($toWH <= $srcWH)
{
$ftoW = $toW;
$ftoH = (int)($ftoW * ($srcH / $srcW));
}
else
{
$ftoH = $toH;
$ftoW = (int)($ftoH * ($srcW / $srcH));
}
if (function_exists("imagecreatetruecolor"))
{
$ni = imagecreatetruecolor($ftoW, $ftoH); //新建一個(gè)真彩色圖像
if ($ni)
{
//重采樣拷貝部分圖像并調(diào)整大小 可保持較好的清晰度
imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
}
else
{
//拷貝部分圖像并調(diào)整大小
$ni = imagecreate($ftoW, $ftoH);
imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
}
}
else
{
$ni = imagecreate($ftoW, $ftoH);
imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
}
//保存到文件 統(tǒng)一為.png格式
imagepng($ni, $toFile); //以 PNG 格式將圖像輸出到瀏覽器或文件
ImageDestroy($ni);
ImageDestroy($im);
}
?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- 將PHP程序中返回的JSON格式數(shù)據(jù)用gzip壓縮輸出的方法
- php使用gzip壓縮傳輸js和css文件的方法
- php實(shí)現(xiàn)壓縮多個(gè)CSS與JS文件的方法
- php壓縮HTML函數(shù)輕松實(shí)現(xiàn)壓縮html/js/Css及注意事項(xiàng)
- PHP根據(jù)傳入?yún)?shù)合并多個(gè)JS和CSS文件的簡(jiǎn)單實(shí)現(xiàn)
- php合并js請(qǐng)求的例子
- php簡(jiǎn)單壓縮css樣式示例
- 基于PHP實(shí)現(xiàn)等比壓縮圖片大小
- php上傳圖片并壓縮的實(shí)現(xiàn)方法
- PHP實(shí)現(xiàn)圖片上傳并壓縮
- php實(shí)現(xiàn)壓縮合并js的方法【附demo源碼下載】
相關(guān)文章
PHP函數(shù)microtime()用法與說(shuō)明
這篇文章主要介紹了PHP函數(shù)microtime()用法與說(shuō)明,有需要的朋友可以參考一下2013-12-12
PHP向?yàn)g覽器輸出內(nèi)容的4個(gè)函數(shù)總結(jié)
這篇文章主要介紹了PHP向?yàn)g覽器輸出內(nèi)容的4個(gè)函數(shù)總結(jié),本文總結(jié)的就是print()、echo()、printf()、sprintf()這4個(gè)輸出函數(shù),需要的朋友可以參考下2014-11-11
discuz authcode 經(jīng)典php加密解密函數(shù)解析
康盛的 authcode 函數(shù)可以說(shuō)對(duì)中國(guó)的PHP界作出了重大貢獻(xiàn)。包括康盛自己的產(chǎn)品,以及大部分中國(guó)使用PHP的公司都用這個(gè)函數(shù)進(jìn)行加密,authcode 是使用異或運(yùn)算進(jìn)行加密和解密。2010-02-02

