PHP簡單創(chuàng)建壓縮圖的方法
本文實例講述了PHP簡單創(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; } //計算縮略圖的寬高 $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); //新建一個真彩色圖像 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ù)學運算技巧總結(jié)》、《php日期與時間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
PHP向瀏覽器輸出內(nèi)容的4個函數(shù)總結(jié)
這篇文章主要介紹了PHP向瀏覽器輸出內(nèi)容的4個函數(shù)總結(jié),本文總結(jié)的就是print()、echo()、printf()、sprintf()這4個輸出函數(shù),需要的朋友可以參考下2014-11-11discuz authcode 經(jīng)典php加密解密函數(shù)解析
康盛的 authcode 函數(shù)可以說對中國的PHP界作出了重大貢獻。包括康盛自己的產(chǎn)品,以及大部分中國使用PHP的公司都用這個函數(shù)進行加密,authcode 是使用異或運算進行加密和解密。2010-02-02