PHP使用GD庫生成檔案

2021-09-12 01:44:55 字數 2581 閱讀 9478

<?php

$xgcs_result = $this->get_xgcs_result($key);

if (!empty($xgcs_result) && isset($xgcs_result['type']) && isset($xgcs_result['content']) && isset($xgcs_result['image']) && $name)

$file_name = sha1(microtime() . $_server['remote_addr'] . "sdgjasdruisodgjketidfg" . rand(1, 10000000) . rand(1, 10000000)) . '.png';//生成的名稱

$pic_path = $path . '/' . $file_name;//生成完整路徑

$pic_width = 420;//生成的寬高

$pic_height = 700;

$local_image = sitepath . "static/activity/img/xinggeceshi/" . $xgcs_result['image'];

$src_image = imagecreatefrompng($local_image);

$src_width = imagesx($src_image);

$src_height = imagesy($src_image);

//建立

//$dest_image = imagecreate($pic_width, $pic_height);

$dest_image = @imagecreatetruecolor($pic_width, $pic_height) or die('cannot initialize new gd image stream');

//建立顏色

$result = imagecopy($dest_image, $src_image, 0, 0, 0, 0, $pic_width, $pic_height);

$white = imagecolorallocate($dest_image, 255, 255, 255);

imagefilledrectangle($dest_image, 0, $src_height, $pic_width, $pic_height, $white);

$text_color = imagecolorallocate($dest_image, 102, 102, 102);

$tip_length = mb_strlen($tip_string);

$tip_num = 20;

$row = ceil($tip_length / $tip_num);

$font_size = 14;//文字大小

$fontpath = rootpath . 'sitedata/fonts/font2.ttf';//楷體

$heitipath = rootpath . 'sitedata/fonts/simhei.ttf';//黑體

//var_dump($tip_string);

$name_length_ext = strlen($name);

$name = mb_convert_encoding($name, "utf-8", "gbk");

$name_color = imagecolorallocate($dest_image, 77, 61, 68);

$name_length = strlen($name);

$name_right = mb_convert_encoding("的測試結果是:", "utf-8", "gbk");

$name_right_color = imagecolorallocate($dest_image, 151, 153, 162);

if ($name_length > 12) else

$type = mb_convert_encoding($xgcs_result['type'], "utf-8", "gbk");

$type_length = strlen($type);

$type_color = imagecolorallocate($dest_image, 255, 87, 80);

$type_right = mb_convert_encoding("型血", "utf-8", "gbk");

$type_right_color = imagecolorallocate($dest_image, 255, 87, 80);

imagettftext($dest_image, 20, 0, 18, $src_height + 90, $type_color, $heitipath, $type);

imagettftext($dest_image, $font_size, 0, 18 + 18 * $type_length, $src_height + 90, $type_right_color, $fontpath, $type_right);

for ($i = 0; $i < $row; $i++)

if (imagepng($dest_image, $pic_path)) else

}

以上**是在做一次活動海報生成時使用的。

PHP 生成檔案快取

php 快取有幾種方式 本人目前接觸過兩種快取方式 1 全頁面靜態化快取 將頁面全部生成為html靜態頁面,使用者訪問時直接訪問靜態頁面,不走php伺服器的解析流程,實現方法 輸出快取 ob start 開啟 輸出控制緩衝 do some code 要執行的 content ob get conte...

php 使用GD庫生成驗證碼

gd庫是php進行圖象操作乙個很強大的庫。先在php.ini裡增加一行引用 extension php gd2.dll 重啟apache。做乙個測試頁 var dump gd info 輸出資料表明gd庫引用成功。表單auth.html 驗證碼title head body h1 請輸入驗證碼 h1...

PHP利用GD庫生成驗證碼

check code function check code width 100 height 50 num 4 type jpeg 將生成的num個字元拼接起來 string.sprintf c ascii 隨機生成淺色背景 imagefilledrectangle img,0 0,width h...