PHP映像操作教程:3D圖、縮放、旋轉、裁剪、添加浮水印

來源:互聯網
上載者:User

   圖片操作在網站的應用相當廣範,特別是現在互連網高度發達,很多內容都是以圖片來顯示,現在我們來講講用php來操作上傳的圖片,3D圖片繪製、圖片縮放、圖片旋轉、圖片裁剪、圖片添加浮水印。

  1、利用php gd庫的函數繪製3D扇形統計圖

代碼如下  
<?php
header("content-type","text/html;charset=utf-8");
/*扇形統計圖*/
$image = imagecreatetruecolor(100, 100); /*建立畫布*/
/*設定畫布需要的顏色*/
$white = imagecolorallocate($image,0xff,0xff,0xff);
$gray = imagecolorallocate($image, 0xc0, 0xc0, 0xc0);
$darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90);
$navy = imagecolorallocate($image, 0x00, 0x00, 0x80);
$darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50);
$red = imagecolorallocate($image, 0xff, 0x00, 0x00);
$darkred = imagecolorallocate($image, 0x90, 0x00, 0x00);

/*填充背景色*/
imagefill($image, 0, 0, $white);

/*3D製作*/
for($i = 60; $i > 50; $i--)
{
imagefilledarc($image, 50, $i, 100, 50, -160, 40, $darknavy, IMG_ARC_PIE);
imagefilledarc($image, 50, $i, 100, 50, 40, 75, $darkgray, IMG_ARC_PIE);
imagefilledarc($image, 50, $i, 100, 50, 75, 200, $darkred, IMG_ARC_PIE);
}

/*畫橢圓弧並填充*/
imagefilledarc($image, 50, 50, 100, 50, -160, 40, $darknavy, IMG_ARC_PIE);
imagefilledarc($image, 50, 50, 100, 50, 40, 75, $darkgray, IMG_ARC_PIE);
imagefilledarc($image, 50, 50, 100, 50, 75, 200, $darkred, IMG_ARC_PIE);


/*畫字串*/
imagestring($image, 3, 15, 55, "30%", $white);
imagestring($image, 3, 45, 35, "60%", $white);
imagestring($image, 3, 60, 60, "10%", $white);

/*輸出映像*/
header("content-type:image/png");
imagepng($image);

/*釋放資源*/
imagedestroy($image);
?>

  2、對圖片進行縮放

代碼如下  
<div>
<h4>原圖大小</h4>
<img src="1.png" style="border:1px solid red;">
< /div>

< ?php
header("content-type","text/html;charset=utf-8");

/*
*圖片縮放
*@param string $filename 圖片的url
*@param int $width 設定圖片縮放的最大寬度
*@param int $height 設定圖片縮放的最大高度
*/
function thumb($filename,$width=130,$height=130)
{
/*擷取原圖的大小*/
list($width_orig,$height_orig) = getimagesize($filename);

/*根據參數$width和$height,換算出等比例的高度和寬度*/
if($width && ($width_orig < $height_orig))
{
$width = ($height / $height_orig) * $width_orig;
}
else
{
$height = ($width / $width_orig) * $height_orig;
}

/*以新的大小建立畫布*/
$image_p = imagecreatetruecolor($width, $height);

/*擷取映像資源*/
$image = imagecreatefrompng($filename);

/*使用imagecopyresampled縮放*/
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

/*儲存縮放後的圖片和命名*/

imagepng($image_p,'test.png');

/*釋放資源*/

imagedestroy($image_p);
imagedestroy($image);
}

/*調用函數*/
thumb('1.png');
?>

< div>
<h4>縮放後的大小</h4>
<img src="test.png" style="border:1px solid red;">
< /div>

  3、PHP對映像的裁剪

代碼如下  
<div>
<h4>裁剪之前</h4>
<img src="1.png" style="border:1px solid red;">
< /div>

< ?php
header("content-type","text/html;charset=utf-8");
/*
*圖片裁剪
*@param string $filename 圖片的url
*@param int $width 圖片裁剪寬度
*@param int $height 圖片裁剪高度
*@param int $x 裁剪圖片左邊開始的位置
*@param int $y 裁剪圖片頂邊開始的位置
*/
function cut($filename,$x,$y,$width,$height)
{
/*擷取映像資源,需要裁剪的圖片*/
$image = imagecreatefrompng($filename);

/*以新的大小建立畫布,儲存裁剪後的圖片*/

$image_p = imagecreatetruecolor($width, $height);

/*使用imagecopyresampled縮放*/
imagecopyresampled($image_p, $image, 0, 0, $x, $y, $width, $height, $width, $height);
/*儲存裁剪後的圖片和命名*/
imagepng($image_p,'test1.png');
/*釋放資源*/
imagedestroy($image_p);
imagedestroy($image);
}

/*調用函數*/
cut('1.png',20,20,80,80);
?>

< div>
<h4>裁剪之後</h4>
<img src="test1.png" style="border:1px solid red;">
</div>

  4、PHP為映像添加浮水印

代碼如下  
<div>
<h4>沒有浮水印</h4>
<img src="1.png" style="border:1px solid red;">
< /div>

< ?php
header("content-type","text/html;charset=utf-8");
/*
*給背景圖片添加浮水印,背景圖片格式png,浮水印格式gif
*@param string $filename 圖片的url
*@param string $water 浮水印圖片
*/
function watermark($filename,$water)
{
/*擷取原圖的大小*/
list($b_w,$b_h) = getimagesize($filename);
/*擷取浮水印圖片的大小*/
list($w_w,$w_h) = getimagesize($water);
/*在背景圖片中放浮水印圖片的隨機起始位置*/
$posX =rand(0,($b_w - $w_w));
$posY =rand(0,($b_h - $w_h));
/*擷取映像資源,需要裁剪的圖片*/
$back = imagecreatefrompng($filename);
$water = imagecreatefromgif($water);
/*使用Inagecopy函數複製浮水印圖片到指定位置*/
imagecopy($back, $water, $posX, $posY, 0, 0, $w_w, $w_h);
/*儲存帶浮水印的圖片和命名*/
imagepng($back,'test2.png');
/*釋放資源*/
imagedestroy($back);
imagedestroy($water);
}

/*調用函數*/
watermark('1.png','test.gif');
?>

< div>
<h4>加上浮水印</h4>
<img src="test2.png" style="border:1px solid red;">
</div>

聯繫我們

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