PHP用GD庫生成高質(zhì)量的縮略圖片
更新時(shí)間:2011年03月09日 22:47:49 作者:
PHP用GD庫生成高質(zhì)量的縮略圖片,PHP一般情況下生成的縮略圖都比較不理想。今天試用PHP,GD庫來生成縮略圖。雖然并不100%完美??墒且矐?yīng)該可以滿足縮略圖的要求了。
以下是PHP源代碼(ResizeImage.php)。
<?php
$FILENAME="image.thumb";
// 生成圖片的寬度
$RESIZEWIDTH=400;
// 生成圖片的高度
$RESIZEHEIGHT=400;
function ResizeImage($im,$maxwidth,$maxheight,$name){
$width = imagesx($im);
$height = imagesy($im);
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$RESIZEWIDTH=true;
}
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$RESIZEHEIGHT=true;
}
if($RESIZEWIDTH && $RESIZEHEIGHT){
if($widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif($RESIZEWIDTH){
$ratio = $widthratio;
}elseif($RESIZEHEIGHT){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
ImageJpeg ($newim,$name . ".jpg");
ImageDestroy ($newim);
}else{
ImageJpeg ($im,$name . ".jpg");
}
}
if($_FILES['image']['size']){
if($_FILES['image']['type'] == "image/pjpeg"){
$im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
}elseif($_FILES['image']['type'] == "image/x-png"){
$im = imagecreatefrompng($_FILES['image']['tmp_name']);
}elseif($_FILES['image']['type'] == "image/gif"){
$im = imagecreatefromgif($_FILES['image']['tmp_name']);
}
if($im){
if(file_exists("$FILENAME.jpg")){
unlink("$FILENAME.jpg");
}
ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME);
ImageDestroy ($im);
}
}
?>
以下是測(cè)試代碼(demo.php)
<?php
include('ResizeImage.php');
if(!empty($_POST)){
echo($FILENAME.".jpg?cache=".rand(0,999999));
}
?>
<form name="test" action="?submit=true" enctype="multipart/form-data" method="post" >
<input type="file" name="image" size="50" value="瀏覽"><p>
<input type="submit" value="上傳圖片">
</form>
復(fù)制代碼 代碼如下:
<?php
$FILENAME="image.thumb";
// 生成圖片的寬度
$RESIZEWIDTH=400;
// 生成圖片的高度
$RESIZEHEIGHT=400;
function ResizeImage($im,$maxwidth,$maxheight,$name){
$width = imagesx($im);
$height = imagesy($im);
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$RESIZEWIDTH=true;
}
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$RESIZEHEIGHT=true;
}
if($RESIZEWIDTH && $RESIZEHEIGHT){
if($widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif($RESIZEWIDTH){
$ratio = $widthratio;
}elseif($RESIZEHEIGHT){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
ImageJpeg ($newim,$name . ".jpg");
ImageDestroy ($newim);
}else{
ImageJpeg ($im,$name . ".jpg");
}
}
if($_FILES['image']['size']){
if($_FILES['image']['type'] == "image/pjpeg"){
$im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
}elseif($_FILES['image']['type'] == "image/x-png"){
$im = imagecreatefrompng($_FILES['image']['tmp_name']);
}elseif($_FILES['image']['type'] == "image/gif"){
$im = imagecreatefromgif($_FILES['image']['tmp_name']);
}
if($im){
if(file_exists("$FILENAME.jpg")){
unlink("$FILENAME.jpg");
}
ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME);
ImageDestroy ($im);
}
}
?>
以下是測(cè)試代碼(demo.php)
復(fù)制代碼 代碼如下:
<?php
include('ResizeImage.php');
if(!empty($_POST)){
echo($FILENAME.".jpg?cache=".rand(0,999999));
}
?>
<form name="test" action="?submit=true" enctype="multipart/form-data" method="post" >
<input type="file" name="image" size="50" value="瀏覽"><p>
<input type="submit" value="上傳圖片">
</form>
相關(guān)文章
PHP讀取CURL模擬登錄時(shí)生成Cookie文件的方法
這篇文章主要介紹了PHP讀取CURL模擬登錄時(shí)生成Cookie文件的方法,包括了curl的使用及cookie的操作,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-11-11
PHP 實(shí)現(xiàn)base64編碼文件上傳出現(xiàn)問題詳解
這篇文章主要介紹了PHP 實(shí)現(xiàn)base64編碼文件上傳出現(xiàn)問題詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
highchart數(shù)據(jù)源縱軸json內(nèi)的值必須是int(詳解)
下面小編就為大家?guī)硪黄猦ighchart數(shù)據(jù)源縱軸json內(nèi)的值必須是int(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
PHP數(shù)組與對(duì)象之間使用遞歸實(shí)現(xiàn)轉(zhuǎn)換的方法
這篇文章主要介紹了PHP數(shù)組與對(duì)象之間使用遞歸實(shí)現(xiàn)轉(zhuǎn)換的方法,涉及php數(shù)組與對(duì)象的相關(guān)操作技巧,需要的朋友可以參考下2015-06-06
WordPress中給文章添加自定義字段及后臺(tái)編輯功能區(qū)域
這篇文章主要介紹了WordPress中給文章添加自定義字段及后臺(tái)編輯區(qū)域的相關(guān)函數(shù),分別簡(jiǎn)單講了add_post_meta和add_meta_box 的用法,需要的朋友可以參考下2015-12-12

