PHP映像函數

來源:互聯網
上載者:User

標籤:10個   html   zed   style   顏色   idt   sina   res   實現   

http://blog.sina.com.cn/s/blog_68b56adb0100vq1a.html

相簿程式、圖片縮圖的產生是必不可少的一個功能,用PHP的GD函數為圖片產生縮圖是很簡單的,核心就一個 

imagecopyresampled函數。主要麻煩的是要考慮圖片的各種長、寬、縮放比等等東西。

 

   如果對縮圖的品質要求不高可以使用imagecopyresized()函數,imagecopyresize()所產生的映像比較粗糙,但是速度較快;imagecopyresampled()函數是GD 2.x後新增加的函數,字面上的意思是會對圖片進行重新採樣(resampling),GD是採用插值演算法產生更平滑的映像,但是速度相對imagecopyresize()函數來說要慢一些。
     imagecopyresample() 共有10個參數,前面兩個是目標檔案和源檔案,後面4個分別是dst和src的width,height,中間的 src_x,src_y,dst_x,dst_y看得比較暈。。例子裡面都是設定為0,後來試了下,發現把這4個參數寫成 dst_start_x, dst_start_y, src_start_x, src_start_y的話就很好理解了。舉個例子說吧。假如要在縮圖上下各留10 pixel的空白,那就可以用imagecopyresampled($dst, $src, dst_start_x, dst_start_y + 10, src_start_x, src_start_y, dst_width, dst_height – 20, src_width, src_height)來實現。

用imagecreatetruecolor(int x,int y)建立的是一幅大小為 x和 y的黑色映像(預設為黑色),如想改變背景顏色則需要用填充顏色函數imagefill($img,0,0,$color);   

imagecreate 建立一個空白映像資源,用imagecolorAllocate()添加背景色

 

<?php
$img =imagecreatetruecolor(100,100); 

   //建立真彩映像資源
$color =imagecolorAllocate($img,200,200,200);  //分配一個灰色
imagefill($img,0,0,$color);                // 從左上方開始填充灰色
header(‘content-type:image/jpeg‘);  //jpg格式
imagejpeg($img);                              //顯示灰色的方塊
?>

<?php
$img = imagecreate(100,100);
$color = imagecolorallocate($img,200,200,200);
header(‘content-type:image/jpeg‘); 

 //jpg格式
imagejpeg($img);                              //顯示灰色的方塊
?>

PHP映像函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.