php在圖片上產生文字代碼

來源:互聯網
上載者:User

在php教程如果想利用它圖片處理函數就必須在php.ini裡面的gd庫開啟哦,
*/
//發送標頭檔
header("content-type: image/png");
//建立映像,如果失敗輸出內容
$im=@imagecreate(150,50) or die("cannot initialize new gd image stream");
//定義背景顏色
$background_color=imagecolorallocate($im,255,255,255);
//定義文字顏色
$text_color=imagecolorallocate($im,233,14,91);
//在映像上畫出檔案
imagestring($im,3,5,5,"hello world",$text_color);
//輸出影像檔
imagepng($im);
//銷毀映像
imagedestroy($im);
/*
該代碼的執行結果如圖22.5所示:
*/

// 2圖片等比例縮小

//定義一個檔案
$filename='1.jpg';
//定義縮放百分比
$percent=0.5;
//輸出標頭檔
header('content-type: image/jpeg');
//擷取新的大小
list($width,$height)=getimagesize($filename);
$newwidth=$width * $percent;
$newheight=$height * $percent;
//建立繪圖區域,並載入映像
$thumb=imagecreatetruecolor($newwidth,$newheight);
$source=imagecreatefromjpeg($filename);
//重新調整大小
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//輸出映像
imagejpeg($thumb);
/*
執行該代碼,將把原映像縮放50%,並以新映像輸出
*/

//在圖片上寫文字

//定義內容
$data='ivborw0kggoaaaansuheugaaabwaaaascamaaab/2u7waaaabl'.
      'bmveuaaad///+l2z/daaaasuleqvr4xqwquqoaiaxc2/0vxzdr'.
      'ex4ijtrkb7lobnustxsb0jixiamssqnwlsv+wulf4avk9flq2r'.
      '8a5hse35q3eo2xp1a1wqkzsgetvdtkdqaaaabjru5erkjggg==';
//對內容進行base64編碼
$data=base64_decode($data);
//根據字串建立映像
$im=imagecreatefromstring($data);
if($im!== false)
{
  //如果成功建立,則輸出映像
  header('content-type: image/png');
  imagepng($im);
}
else
{
  //如果建立失敗,則輸出內容
  echo 'an error occured.';
}
/*
該代碼的執行結果如圖:22.4所示:
*/

//在圖片上寫文字

header("content-type: image/png");
//建立映像,如果失敗輸出內容
$im=@imagecreate(100,50) or die("cannot initialize new gd image stream");
//定義背景顏色
$background_color=imagecolorallocate($im,255,255,255);
//定義文字顏色
$text_color=imagecolorallocate($im,233,14,91);
//在映像上畫出檔案
imagestring($im,1,5,5,"a simple text string",$text_color);
//輸出影像檔
imagepng($im);
//銷毀映像
imagedestroy($im);
/*
執行該代碼將產生一個jpeg映像。
並輸出指定字串
*/

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.