[php] 處理映像

來源:互聯網
上載者:User

標籤:des   blog   io   ar   color   使用   sp   for   on   

<?php/* 處理映像 *//*  {php5} 生動影像的處理更容易. 在 php.ini中就包含了GD擴充包, 去掉 其中的注釋即可.  extension=php_gd2.dll 其中 包含了 支援真彩影像處理的一些有用的JPG功能.   一般產生的圖形, 通過PHP的文檔格式存放; 但可以通過HTML的圖片插入方式SRC 來直接擷取動態圖形. 比如: 驗證碼 / 浮水印 / 縮圖 ...     *  *//* {建立映像}的一般流程: 1. 設定標題.(告訴瀏覽器你要身材的MIME的類型)2. 建立 {映像地區}, 後面的操作要基於此映像地區.* imagecreatetruecolor 一般要加上@符號,避免出錯.* 傳回值是一個資源控制代碼沒有填充的時候,背景是黑色的(預設).* imagecreate  3. 在{映像地區}上,繪製填充背景.首先, 要有個 {顏色填充器}imagecolorallocate -- 為一幅映像分配顏色imagecolorallocatealpha -- 為一幅映像分配顏色 + alpha然後, 填充整個映像背景imagefill -- 地區映像填充imagefilledarc -- 畫一橢圓弧且填充imagefilledellipse -- 畫一橢圓並填充imagefilledpolygon -- 畫一多邊形並填充imagefilledrectangle -- 畫一矩形並填充4. 在{背景}上繪製{圖形輪廓輸入文本}5. 輸出最終 映像.6. 清除所有資源.7. 其他頁面調用映像.一般 產生的映像 可以是 png, jpg, gif, bmp,jpeg, wbmpheader("Content-Type:text/html");// 一般網頁類型是text/html, 預設不用寫       * */?><img src="demo4.php" alt="PHP建立的映像" />

  

<?php header("Content-Type:image/png");// 設定  標題 指定MIME類型$im=imagecreatetruecolor(200, 200);// 建立 一個 空白的  映像地區/* 設定一個顏色, 用它填充映像地區的背景*/$blue=imagecolorallocate($im, 0, 102, 255);imagefill($im, 0, 0, $blue);/* 在映像輪廓上繪製文本 */// 白色文字// 白色$white=imagecolorallocate($im, 255, 255, 255);// 畫兩條對角線imageline($im, 0, 0, 200, 200, $white);imageline($im, 200, 0, 0, 200, $white);imagestring($im, 5, 80, 20, "Mr.Lee", $white);/* 輸出  最終的映像 */imagepng($im);/* 清除所有的佔用 資源 */imagedestroy($im);// 其他頁面就可以調用此頁面建立的映像了?>

  

<?phpheader("Content-Type:image/png");/*產生隨機數字串*/$nmsg="";for ($i=0; $i < 4; $i++) { // 4位的隨機數$nmsg.=dechex(rand(0,15));}// echo $nmsg;// 準備映像$im=imagecreatetruecolor(75,25);// 一般的,一個數字高25$blue=imagecolorallocate($im, 0, 102, 255);$white=imagecolorallocate($im, 255, 255, 255);// 填充映像內容imagefill($im, 0, 0, $white);imagestring($im, 5, 20, 4, $nmsg, $blue);/*輸出映像,並銷毀被使用的資源*/imagepng($im);imagedestroy($im);?>

  

<?phpheader("Content-Type:image/jpg");/*給映像添加一個浮水印*/$im=imagecreatefromjpeg("2.jpg");$white=imagecolorallocate($im, 255, 255, 255);imagestring($im, 15, 10, 10, "This is a picture!", $white);// imagettftext($im, 5, 0, 10, 100, $white, "simsun.ttf", "陽光都是中文");imagejpeg($im);imagedestroy($im);?>

  

<?phpob_clean();//如果輸出的映像不顯示, 那就在這裡清除輸出緩衝區header("Content-type:image/png");$im=imagecreatetruecolor(200, 200);$white=imagecolorallocate($im, 255, 255, 255);$blue=imagecolorallocate($im, 0, 102, 255);imagefill($im, 0, 0, $blue);// $text=iconv("gbk", "utf-8", "鎷夋柉鐨勭鏋楄垂鏂?");$font="C:\\Windows\\Fonts\\consola.ttf";imagettftext($im, 20, 45, 20, 100, $white, $font, "hello");/*這裡的第四第五個參數,用來顯示位置,注意不要用0,否則找不到輸出的常值內容*/imagestring($im, 10, 0, 5, "This is a boy.", $white);imagepng($im);imagedestroy($im);?>

  

<?phpob_clean();//如果輸出的映像不顯示, 那就在這裡清除輸出緩衝區header("Content-type:image/png");$im=imagecreatetruecolor(200, 200);$white=imagecolorallocate($im, 255, 255, 255);$blue=imagecolorallocate($im, 0, 102, 255);imagefill($im, 0, 0, $blue);// $text=iconv("gbk", "utf-8", "鎷夋柉鐨勭鏋楄垂鏂?");$font="C:\\Windows\\Fonts\\simsun.ttc";imagettftext($im, 20, 45, 20, 100, $white, $font, "hello");/* imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text) *//*這裡的第四$x第五$y個參數,用來顯示位置,注意不要用0,否則找不到輸出的常值內容*/imagestring($im, 10, 0, 5, "This is a boy.", $white);$text="這是中文的";// $text=iconv("utf-8", "utf-8", "這是中午的");imagettftext($im, 25, 0, 20, 150, $white, $font, $text);/* 這個中文的輸出是正確的.{現在檔案編碼是utf8,html嵌入檔案輸出的文字編碼是utf8} */imagepng($im);imagedestroy($im);?>

  

<?php /* 說說怎麼用eclipse的問題 *//*  * 首先是介面: 有用的就是編輯器部分和專案管理器(其他的都關閉吧) *  * 其次: 建立項目的問題. * 首先了最好把工作區放到你的檔案夾根目錄;然後,每個建立的項目只要設定好項目名,讓epp自動組建檔案夾就行了. *  *   *  */// 下面將會練習微縮圖// echo __DIR__;list($w,$h)=getimagesize("2.jpg");$nw=$w*0.7;$nh=$h*0.7;$im_src=imagecreatefromjpeg("2.jpg");$im=imagecreatetruecolor($nw, $nh);imagecopyresampled($im, $im_src, 0, 0, 0, 0, $nw, $nh, $w, $h);ob_clean();header("Content-type:image/png");imagepng($im);// imagepng($im_src);imagedestroy($im);imagedestroy($im_src);?>

  

 

[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.