国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP生成縮略圖的實現

2024-05-04 23:04:45
字體:
來源:轉載
供稿:網友
php令我們驚喜的就是在圖形圖象處理方面要憂于asp,用gd庫php就可以輕松的實現縮略圖。這一篇文章我們的目的就是用gd來生成縮略圖,php可以把縮略圖直接生成輸送到瀏覽器也可以以文件的形式把其存儲到硬盤當中。



在生成縮略圖的過程當中我們需要用到gd庫當中的幾個函數:

getimagesize(string filename [,array var])),取得圖像的信息,返回值是一人array,包括幾項信息$var[0]----返回圖像的width,$var[1]----返回height,[2]返回圖像文件的type,[4]返回的是與<img src="">當中的wdith,height有關的width="",height=""信息。

imagex(resource image)

imagey(resource image)  返回圖像的寬和高

imagecopyresized(des img,src img,int des_x,int des_y,int src_x,int src_y,int des_w,int des_h,int src_w,int src_y)  復制并截取區域圖像

imagecreatetruecolor(int width,int height)  創建一個真彩圖

imagejpeg(resource image)

下面就是code:

<?php
# constants
define(image_base, '/var/www/html/mbailey/images'
);
define(max_width, 150
);
define(max_height, 150
);

# get image location
$image_file = str_replace('..', '', $_server['query_string'
]);
$image_path = image_base . "/$image_file"
;

# load image
$img = null
;
$ext = strtolower(end(explode('.', $image_path
)));
if (
$ext == 'jpg' || $ext == 'jpeg'
) {
    
$img = @imagecreatefromjpeg($image_path
);
} else if (
$ext == 'png'
) {
    
$img = @imagecreatefrompng($image_path
);
# only if your version of gd includes gif support
} else if ($ext == 'gif'
) {
    
$img = @imagecreatefrompng($image_path
);
}

# if an image was successfully loaded, test the image for size
if ($img
) {

    
# get image size and scale ratio
    
$width = imagesx($img
);
    
$height = imagesy($img
);
    
$scale = min(max_width/$width, max_height/$height
);

    
# if the image is larger than the max shrink it
    
if ($scale < 1
) {
        
$new_width = floor($scale*$width
);
        
$new_height = floor($scale*$height
);

        
# create a new temporary image
        
$tmp_img = imagecreatetruecolor($new_width, $new_height
);

        
# copy and resize old image into new image
        
imagecopyresized($tmp_img, $img, 0, 0, 0, 0
,
                         
$new_width, $new_height, $width, $height
);
        
imagedestroy($img
);
        
$img = $tmp_img
;
    }
}

# create error image if necessary
if (!$img
) {
    
$img = imagecreate(max_width, max_height
);
    
imagecolorallocate($img,0,0,0
);
    
$c = imagecolorallocate($img,70,70,70
);
    
imageline($img,0,0,max_width,max_height,$c2
);
    
imageline($img,max_width,0,0,max_height,$c2
);
}

# display the image
header("content-type: image/jpeg"
);
imagejpeg($img
);
?>



我們把上面的code存儲為test.php,然后通過test.php?image name的形式來訪問,結果會讓你驚喜的,因為在這里你看到了php的優點,它可以讓asp相形見絀。

上面的這段代碼當中我們通過end(explode(".",$image_path)來取得文件的擴展名,但是我感覺還是不理想。這樣是能夠取得文件的類型的,因為end()函數會跳到本array的最后一個單元,但是如果我們采用getimagesize()會取得更為強大的專門針對于圖像文件的類型。

本程序顯示的縮略圖是限制寬高都在150內,然后用min()函數來取得它們比值的最小值來計算縮略圖的寬和高,并且通過一系列的gd庫函數來取得相應的信息,并且呈現給瀏覽器,當然你也可以寫到你所使用的硬盤當中。

好了,這就是php的縮略圖功能,大家覺得有什么好的意見可以多多拍磚!
  • 網站運營seo文章大全
  • 提供全面的站長運營經驗及seo技術!
  • 發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 司法| 法库县| 吉首市| 佛山市| 迁西县| 中西区| 利川市| 西和县| 肥东县| 明溪县| 怀化市| 滁州市| 静安区| 潼关县| 井冈山市| 邢台市| 长宁县| 肇州县| 峨眉山市| 凭祥市| 双江| 兰溪市| 舟山市| 长乐市| 平邑县| 平利县| 宜川县| 民权县| 阿鲁科尔沁旗| 岑巩县| 砀山县| 五寨县| 雅江县| 大英县| 瑞安市| 台山市| 大足县| 玉溪市| 东辽县| 金阳县| 双辽市|