糯米文學吧

位置:首頁 > 計算機 > php語言

PHP用GD庫生成高質量的縮略圖片

php語言5.15K

PHP用GD庫生成高質量的縮略圖片,PHP一般情況下生成的縮略圖都比較不理想。今天試用PHP,GD庫來生成縮略圖。雖然並不100%完美。可是也應該可以滿足縮略圖的.要求了。

PHP用GD庫生成高質量的縮略圖片

以下是PHP源代碼()。

複製代碼 代碼如下:

<?php

$FILENAME="b";

// 生成圖片的寬度

$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{

標籤:PHP GD 縮略 高質量