本文執行個體為大家分享了php gd等比例縮放壓縮圖片函數,供大家參考,具體內容如下
<?php /** * desription 判斷是否gif動畫 * @param sting $image_file圖片路徑 * @return boolean t 是 f 否 */ function check_gifcartoon($image_file){ $fp = fopen($image_file,'rb'); $image_head = fread($fp,1024); fclose($fp); return preg_match("/".chr(0x21).chr(0xff).chr(0x0b).'NETSCAPE2.0'."/",$image_head)?false:true; } /** * desription 壓縮圖片 * @param sting $imgsrc 圖片路徑 * @param string $imgdst 壓縮後儲存路徑 */ function compressed_image($imgsrc,$imgdst){ list($width,$height,$type)=getimagesize($imgsrc); $new_width = ($width>600?600:$width)*0.9; $new_height =($height>600?600:$height)*0.9; switch($type){ case 1: $giftype=check_gifcartoon($imgsrc); if($giftype){ header('Content-Type:image/gif'); $image_wp=imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromgif($imgsrc); imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); //75代表的是品質、壓縮圖片容量大小 imagejpeg($image_wp, $imgdst,75); imagedestroy($image_wp); } break; case 2: header('Content-Type:image/jpeg'); $image_wp=imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromjpeg($imgsrc); imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); //75代表的是品質、壓縮圖片容量大小 imagejpeg($image_wp, $imgdst,75); imagedestroy($image_wp); break; case 3: header('Content-Type:image/png'); $image_wp=imagecreatetruecolor($new_width, $new_height); $image = imagecreatefrompng($imgsrc); imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); //75代表的是品質、壓縮圖片容量大小 imagejpeg($image_wp, $imgdst,75); imagedestroy($image_wp); break; } }
php and gd 函數參考表
gd_info 取得當前安裝的 GD 庫的資訊
getimagesize 取得映像大小
getimagesizefromstring Get the size of an image from a string
image_type_to_extension 取得映像類型的檔案尾碼
image_type_to_mime_type 取得 getimagesize,exif_read_data,exif_thumbnail,exif_imagetype 所返回的映像類型的 MIME 類型
image2wbmp 以 WBMP 格式將映像輸出到瀏覽器或檔案
imageaffine Return an image containing the affine tramsformed src image, using an optional clipping area
imageaffinematrixconcat Concat two matrices (as in doing many ops in one go)
imageaffinematrixget Return an image containing the affine tramsformed src image, using an optional clipping area
imagealphablending 設定映像的混色模式
imageantialias 是否使用消除鋸齒(antialias)功能
imagearc 畫橢圓弧
imagechar 水平地畫一個字元
imagecharup 垂直地畫一個字元
imagecolorallocate 為一幅映像分配顏色
imagecolorallocatealpha 為一幅映像分配顏色 + alpha
imagecolorat 取得某像素的色彩索引值
imagecolorclosest 取得與指定的顏色最接近的顏色的索引值
imagecolorclosestalpha 取得與指定的顏色加透明度最接近的顏色
imagecolorclosesthwb 取得與給定顏色最接近的色度的黑白色的索引
imagecolordeallocate 取消映像顏色的分配
imagecolorexact 取得指定顏色的索引值
imagecolorexactalpha 取得指定的顏色加透明度的索引值
imagecolormatch 使一個映像中調色盤版本的顏色與真彩色版本更能匹配
imagecolorresolve 取得指定顏色的索引值或有可能得到的最接近的替代值
imagecolorresolvealpha 取得指定顏色 + alpha 的索引值或有可能得到的最接近的替代值
imagecolorset 給指定調色盤索引設定顏色
imagecolorsforindex 取得某索引的顏色
imagecolorstotal 取得一幅映像的調色盤中顏色的數目
imagecolortransparent 將某個顏色定義為透明色
imageconvolution 用係數 div 和 offset 申請一個 3x3 的卷積矩陣
imagecopy 拷貝映像的一部分
imagecopymerge 拷貝併合並映像的一部分
imagecopymergegray 用灰階拷貝併合並映像的一部分
imagecopyresampled 重採樣拷貝部分映像並調整大小
imagecopyresized 拷貝部分映像並調整大小
imagecreate 建立一個基於調色盤的映像
imagecreatefromgd2 從 GD2 檔案或 URL 建立一映像
imagecreatefromgd2part 從給定的 GD2 檔案或 URL 中的部分建立一映像
imagecreatefromgd 從 GD 檔案或 URL 建立一映像
imagecreatefromgif 由檔案或 URL 建立一個新圖象。
imagecreatefromjpeg 由檔案或 URL 建立一個新圖象。
imagecreatefrompng 由檔案或 URL 建立一個新圖象。
imagecreatefromstring 從字串中的映像流建立一映像
imagecreatefromwbmp 由檔案或 URL 建立一個新圖象。
imagecreatefromwebp 由檔案或 URL 建立一個新圖象。
imagecreatefromxbm 由檔案或 URL 建立一個新圖象。
imagecreatefromxpm 由檔案或 URL 建立一個新圖象。
imagecreatetruecolor 建立一個真彩色映像
imagecrop Crop an image using the given coordinates and size, x, y, width and height
imagecropauto Crop an image automatically using one of the available modes
imagedashedline 畫一虛線
imagedestroy 銷毀一映像
imageellipse 畫一個橢圓
imagefill 地區填充
imagefilledarc 畫一橢圓弧且填充
imagefilledellipse 畫一橢圓並填充
imagefilledpolygon 畫一多邊形並填充
imagefilledrectangle 畫一矩形並填充
imagefilltoborder 地區填充到指定顏色的邊界為止
imagefilter 對映像使用過濾器
imageflip Flips an image using a given mode
imagefontheight 取得字型高度
imagefontwidth 取得字型寬度
imageftbbox 給出一個使用 FreeType 2 字型的文字框
imagefttext 使用 FreeType 2 字型將文本寫入映像
imagegammacorrect 對 GD 映像應用 gamma 修正
imagegd2 將 GD2 映像輸出到瀏覽器或檔案
imagegd 將 GD 映像輸出到瀏覽器或檔案
imagegif 輸出圖象到瀏覽器或檔案。
imagegrabscreen Captures the whole screen
imagegrabwindow Captures a window
imageinterlace 啟用或禁止隔行掃描
imageistruecolor 檢查映像是否為真彩色映像
imagejpeg 輸出圖象到瀏覽器或檔案。
imagelayereffect 設定 alpha 混色標誌以使用綁定的 libgd 分層效果
imageline 畫一條線段
imageloadfont 載入一新字型
imagepalettecopy 將調色盤從一幅映像拷貝到另一幅
imagepalettetotruecolor Converts a palette based image to true color
imagepng 以 PNG 格式將映像輸出到瀏覽器或檔案
imagepolygon 畫一個多邊形
imagepsbbox 給出一個使用 PostScript Type1 字型的文本方框
imagepsencodefont 改變字型中的字元編碼向量
imagepsextendfont 擴充或精簡字型
imagepsfreefont 釋放一個 PostScript Type 1 字型所佔用的記憶體
imagepsloadfont 從檔案中載入一個 PostScript Type 1 字型
imagepsslantfont 傾斜某字型
imagepstext 用 PostScript Type1 字型把文本字串畫在映像上
imagerectangle 畫一個矩形
imagerotate 用給定角度旋轉映像
imagesavealpha 設定標記以在儲存 PNG 映像時儲存完整的 Alpha 色板資訊(與單一透明色相反)
imagescale Scale an image using the given new width and height
imagesetbrush 設定畫線用的畫筆映像
imagesetinterpolation Set the interpolation method
imagesetpixel 畫一個單一像素
imagesetstyle 設定畫線的風格
imagesetthickness 設定畫線的寬度
imagesettile 設定用於填充的貼圖
imagestring 水平地畫一行字串
imagestringup 垂直地畫一行字串
imagesx 取得映像寬度
imagesy 取得映像高度
imagetruecolortopalette 將真彩色映像轉換為調色盤映像
imagettfbbox 取得使用 TrueType 字型的文本的範圍
imagettftext 用 TrueType 字型向映像寫入文本
imagetypes 返回當前 PHP 版本所支援的映像類型
imagewbmp 以 WBMP 格式將映像輸出到瀏覽器或檔案
imagewebp Output an WebP image to browser or file
imagexbm 將 XBM 映像輸出到瀏覽器或檔案
iptcembed 將二進位 IPTC 資料嵌入到一幅 JPEG 映像中
iptcparse 將二進位 IPTC 塊解析為單個標記
jpeg2wbmp 將 JPEG 影像檔轉換為 WBMP 影像檔
png2wbmp 將 PNG 影像檔轉換為 WBMP 影像檔
以上就是本文的全部內容,希望對大家學習PHP程式設計有所協助。