⑴ 在php中圖像處理新建一個畫布用什麼函數
$newImage=imagecreatetruecolor($xSize,$ySize);
//增加一個白色的底,不然新建的畫布是黑色的
$white=imagecolorallocate($newImage,255,255,255);
imagefill($newImage,0,0,$white);
圖像處理也經常使用到imagemerge、getimagesize、imagecreatefromjpeg等函數,建議你看下這個鏈接,全是圖像處理的函數http://php.net/manual/zh/ref.image.php
⑵ php如何實現圖片的裁剪
php中裁剪圖片主要使用gd庫的imageresampled方法
$src_path='1.jpg';
//創建源圖的實例
$src=imagecreatefromstring(file_get_contents($src_path));
//裁剪開區域左上角的點的坐標
$x=100;
$y=12;
//裁剪區域的寬和高
$width=200;
$height=200;
//最終保存成圖片的寬和高,和源要等比例,否則會變形
$final_width=100;
$final_height=round($final_width*$height/$width);
//將裁剪區域復制到新圖片上,並根據源和目標的寬高進行縮放或者拉升
$new_image=imagecreatetruecolor($final_width,$final_height);
imageresampled($new_image,$src,0,0,$x,$y,$final_width,$final_height,$width,$height);
//輸出圖片
header('Content-Type:image/jpeg');
imagejpeg($new_image);
imagedestroy($src);
imagedestroy($new_image);
⑶ php 裁切圖片問題 求助
用JS吧,直接設置選擇大小,然後選取再截取即可!
⑷ php 圖象處理函數 imagestring 函數的運用
可以用imagettftext來生成,支持truetype字體
array imagettftext ( resource image, float size, float angle, int x, int y, int color, string fontfile, string text )
image
圖像資源。見 imagecreatetruecolor()。
size
字體大小。根據 GD 版本不同,應該以像素大小指定(GD1)或點大小(GD2)。
angle
角度製表示的角度,0 度為從左向右讀的文本。更高數值表示逆時針旋轉。例如 90 度表示從下向上讀的文本。
x
由 x,y 所表示的坐標定義了第一個字元的基本點(大概是字元的左下角)。這和 imagestring() 不同,其 x,y 定義了第一個字元的左上角。例如 "top left" 為 0, 0。
y
Y 坐標。它設定了字體基線的位置,不是字元的最底端。
color
顏色索引。使用負的顏色索引值具有關閉防鋸齒的效果。見 imagecolorallocate()。
fontfile
是想要使用的 TrueType 字體的路徑。
根據 PHP 所使用的 GD 庫的不同,當 fontfile 沒有以 / 開頭時則 .ttf 將被加到文件名之後並且會在庫定義字體路徑中嘗試搜索該文件名。
當使用的 GD 庫版本低於 2.0.18 時,一個空格字元 而不是分號將被用來作為不同字體文件的「路徑分隔符」。不小心使用了此特性將會導致一條警告信息:Warning: Could not find/open font。對受影響的版本來說唯一解決方案就是將字體移動到不包含空格的路徑中去。
很多情況下字體都放在腳本的同一個目錄下。下面的小技巧可以減輕包含的問題。 <?php
// Set the enviroment variable for GD
putenv('GDFONTPATH=' . realpath('.'));
// Name the font to be used (note the lack of the .ttf extension)
$font = 'SomeFont';
?>
text
文本字元串。
可以包含十進制數字化字元表示(形式為:€)來訪問字體中超過位置 127 的字元。UTF-8 編碼的字元串可以直接傳遞。
如果字元串中使用的某個字元不被字體支持,一個空心矩形將替換該字元。
imagettftext() 返回一個含有 8 個單元的數組表示了文本外框的四個角,順序為坐下角,右下角,右上角,左上角。這些點是相對於文本的而和角度無關,因此「左上角」指的是以水平方向看文字時其左上角。
例子 1. imagettftext() 例子
本例中的腳本將生成一個白色的 400x30 像素 PNG 圖像,其中有黑色(帶灰色陰影)Arial 字體寫的「Testing...」。
<?php
// Set the content-type
header("Content-type: image/png");
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>