- 論壇徽章:
- 0
|
參數(shù)說明:
$im 圖片對象,應(yīng)用函數(shù)之前,你需要用imagecreatefromjpeg()讀取圖片對象,如果PHP環(huán)境支持PNG,GIF,也可使用imagecreatefromgif(),imagecreatefrompng();
$maxwidth 定義生成圖片的最大寬度(單位:像素)
$maxheight 生成圖片的最大高度(單位:像素)
$name 生成的圖片名
$filetype 最終生成的圖片類型(.jpg/.png/.gif)
特別說明:
GD庫1.6.2版以前支持GIF格式,但因GIF格式使用LZW演算法牽涉專利權(quán),因此在GD1.6.2版之后不支持GIF的格式。如果你是WINDOWS的環(huán)境,你只要進入PHP.INI文件找到extension=php_gd2.dll,將#去除,重啟APACHE即可,如果你是Linux環(huán)境,又想支持GIF,PNG,JPEG,你需要去下載libpng,zlib,以及freetype字體并安裝。
[PHP]代碼- function resizeImage($im,$maxwidth,$maxheight,$name,$filetype)
- {
- $pic_width = imagesx($im);
- $pic_height = imagesy($im);
- if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight))
- {
- if($maxwidth && $pic_width>$maxwidth)
- {
- $widthratio = $maxwidth/$pic_width;
- $resizewidth_tag = true;
- }
- if($maxheight && $pic_height>$maxheight)
- {
- $heightratio = $maxheight/$pic_height;
- $resizeheight_tag = true;
- }
- if($resizewidth_tag && $resizeheight_tag)
- {
- if($widthratio<$heightratio)
- $ratio = $widthratio;
- else
- $ratio = $heightratio;
- }
- if($resizewidth_tag && !$resizeheight_tag)
- $ratio = $widthratio;
- if($resizeheight_tag && !$resizewidth_tag)
- $ratio = $heightratio;
- $newwidth = $pic_width * $ratio;
- $newheight = $pic_height * $ratio;
- if(function_exists("imagecopyresampled"))
- {
- $newim = imagecreatetruecolor($newwidth,$newheight);
- imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
- }
- else
- {
- $newim = imagecreate($newwidth,$newheight);
- imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
- }
- $name = $name.$filetype;
- imagejpeg($newim,$name);
- imagedestroy($newim);
- }
- else
- {
- $name = $name.$filetype;
- imagejpeg($im,$name);
- }
- }
復(fù)制代碼 |
|