可定制的PHP縮略圖生成程式(需要GD庫支持)
更新時間:2007年03月06日 00:00:00 作者:
經(jīng)典的PHP縮略圖生成程式,基于GD庫,可指定生成路徑及生成目標(biāo)的寬高細(xì)節(jié) 使用方法: 在支持GD庫的PHP環(huán)境中,將以下代碼另存為resize.php測試
經(jīng)典的PHP縮略圖生成程式,基于GD庫,可指定生成路徑及生成目標(biāo)的寬高細(xì)節(jié)
使用方法: 在支持GD庫的PHP環(huán)境中,將以下代碼另存為resize.php測試
<?
$FILENAME="image_name";
// 生成圖片的寬度
$RESIZEWIDTH=400;
// 生成圖片的高度
$RESIZEHEIGHT=400;
//生成圖片的路徑
$uploaddir="c:/winnt/temp";
function ResizeImage($im,$maxwidth,$maxheight,$name){
global $uploaddir;
$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,$uploaddir.$name . ".jpg");
ImageDestroy ($newim);
}else{
ImageJpeg ($im,$uploaddir.$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);
}
}
?>
<img src="<? echo($FILENAME.".jpg?reload=".rand(0,999999)); ?>"><br><br>
<form enctype="multipart/form-data" method="post">
<br>
<input type="file" name="image" size="50" value="瀏覽"><p>
<input type="submit" value="上傳圖片">
</form>
</body>
</html>
經(jīng)典的PHP縮略圖生成程式,基于GD庫,可指定生成路徑及生成目標(biāo)的寬高細(xì)節(jié)
使用方法: 在支持GD庫的PHP環(huán)境中,將以下代碼另存為resize.php測試
復(fù)制代碼 代碼如下:
<?
$FILENAME="image_name";
// 生成圖片的寬度
$RESIZEWIDTH=400;
// 生成圖片的高度
$RESIZEHEIGHT=400;
//生成圖片的路徑
$uploaddir="c:/winnt/temp";
function ResizeImage($im,$maxwidth,$maxheight,$name){
global $uploaddir;
$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,$uploaddir.$name . ".jpg");
ImageDestroy ($newim);
}else{
ImageJpeg ($im,$uploaddir.$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);
}
}
?>
<img src="<? echo($FILENAME.".jpg?reload=".rand(0,999999)); ?>"><br><br>
<form enctype="multipart/form-data" method="post">
<br>
<input type="file" name="image" size="50" value="瀏覽"><p>
<input type="submit" value="上傳圖片">
</form>
</body>
</html>
您可能感興趣的文章:
- PHP GD庫生成圖像的幾個函數(shù)總結(jié)
- php實現(xiàn)的支持imagemagick及gd庫兩種處理的縮略圖生成類
- 使用gd庫實現(xiàn)php服務(wù)端圖片裁剪和生成縮略圖功能分享
- PHP用GD庫生成高質(zhì)量的縮略圖片
- php的GD庫imagettftext函數(shù)解決中文亂碼問題
- PHP5中GD庫生成圖形驗證碼(有漢字)
- PHP基于GD庫的縮略圖生成代碼(支持jpg,gif,png格式)
- PHP中使用GD庫創(chuàng)建圓形餅圖的例子
- PHP中使用GD庫繪制折線圖 折線統(tǒng)計圖的繪制方法
- php 使用GD庫為頁面增加水印示例代碼
- php利用GD庫生成縮略圖示例
- php中使用gd庫實現(xiàn)下載網(wǎng)頁中所有圖片
- PHP基于GD庫的圖像處理方法小結(jié)
相關(guān)文章
Zend Framework入門教程之Zend_Session會話操作詳解
這篇文章主要介紹了Zend Framework入門教程之Zend_Session會話操作,結(jié)合實例形式詳細(xì)分析了Zend_Session會話操作的具體使用技巧,需要的朋友可以參考下2016-12-12PHP檢測移動設(shè)備類mobile detection使用實例
這篇文章主要介紹了PHP檢測移動設(shè)備類mobile detection使用實例,mobile detection可以檢測到絕大部分手機、平板電腦的操作系統(tǒng)、瀏覽器等數(shù)據(jù),需要的朋友可以參考下2014-04-04Yii中CArrayDataProvider和CActiveDataProvider區(qū)別實例分析
這篇文章主要介紹了Yii中CArrayDataProvider和CActiveDataProvider區(qū)別,結(jié)合實例形式分析了Yii中CArrayDataProvider和CActiveDataProvider的具體功能與用法,需要的朋友可以參考下2016-03-03PHP快速按行讀取CSV大文件的封裝類分享(也適用于其它超大文本文件)
這篇文章主要介紹了一個PHP快速按行讀取CSV大文件的封裝類,這個類同時也適用于其它體積較大的文本文件,需要的朋友可以參考下2014-04-04