PHP圖像操作教程:3D圖、縮放、旋轉(zhuǎn)、裁剪、添加水印

字號:


    圖片操作在網(wǎng)站的應(yīng)用相當(dāng)廣范,特別是現(xiàn)在互聯(lián)網(wǎng)高度發(fā)達(dá),很多內(nèi)容都是以圖片來顯示,現(xiàn)在我們來講講用php來操作上傳的圖片,3D圖片繪制、圖片縮放、圖片旋轉(zhuǎn)、圖片裁剪、圖片添加水印。
    1、利用php gd庫的函數(shù)繪制3D扇形統(tǒng)計圖
    代碼如下:
    <?php
    header("content-type","text/html;charset=utf-8");
    /*扇形統(tǒng)計圖*/
    $image = imagecreatetruecolor(100, 100); /*創(chuàng)建畫布*/
    /*設(shè)置畫布需要的顏色*/
    $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、對圖片進(jìn)行縮放
    代碼如下:
    <div>
    <h4>原圖大小</h4>
    <img src="1.png">
    < /div>
    < ?php
    header("content-type","text/html;charset=utf-8");
    /*
    *圖片縮放
    string $filename 圖片的url
    int $width 設(shè)置圖片縮放的最大寬度
    int $height 設(shè)置圖片縮放的最大高度
    */
    function thumb($filename,$width=130,$height=130)
    {
    /*獲取原圖的大小*/
    list($width_orig,$height_orig) = getimagesize($filename);
    /*根據(jù)參數(shù)$width和$height,換算出等比例的高度和寬度*/
    if($width && ($width_orig < $height_orig))
    {
    $width = ($height / $height_orig) * $width_orig;
    }
    else
    {
    $height = ($width / $width_orig) * $height_orig;
    }
    /*以新的大小創(chuàng)建畫布*/
    $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);
    }
    /*調(diào)用函數(shù)*/
    thumb('1.png');
    ?>
    < div>
    <h4>縮放后的大小</h4>
    <img src="test.png">
    < /div>
    3、PHP對圖像的裁剪
    代碼如下:
    <div>
    <h4>裁剪之前</h4>
    <img src="1.png">
    < /div>
    < ?php
    header("content-type","text/html;charset=utf-8");
    /*
    *圖片裁剪
    string $filename 圖片的url
    int $width 圖片裁剪寬度
    int $height 圖片裁剪高度
    int $x 裁剪圖片左邊開始的位置
    int $y 裁剪圖片頂邊開始的位置
    */
    function cut($filename,$x,$y,$width,$height)
    {
    /*獲取圖像資源,需要裁剪的圖片*/
    $image = imagecreatefrompng($filename);
    /*以新的大小創(chuàng)建畫布,保存裁剪后的圖片*/
    $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);
    }
    /*調(diào)用函數(shù)*/
    cut('1.png',20,20,80,80);
    ?>
    < div>
    <h4>裁剪之后</h4>
    <img src="test1.png">
    </div>
    4、PHP為圖像添加水印
    代碼如下:
    <div>
    <h4>沒有水印</h4>
    <img src="1.png">
    < /div>
    < ?php
    header("content-type","text/html;charset=utf-8");
    /*
    *給背景圖片添加水印,背景圖片格式png,水印格式gif
    string $filename 圖片的url
    string $water 水印圖片
    */
    function watermark($filename,$water)
    {
    /*獲取原圖的大小*/
    list($b_w,$b_h) = getimagesize($filename);
    /*獲取水印圖片的大小*/
    list($w_w,$w_h) = getimagesize($water);
    /*在背景圖片中放水印圖片的隨機(jī)起始位置*/
    $posX =rand(0,($b_w - $w_w));
    $posY =rand(0,($b_h - $w_h));
    /*獲取圖像資源,需要裁剪的圖片*/
    $back = imagecreatefrompng($filename);
    $water = imagecreatefromgif($water);
    /*使用Inagecopy函數(shù)復(fù)制水印圖片到指定位置*/
    imagecopy($back, $water, $posX, $posY, 0, 0, $w_w, $w_h);
    /*保存帶水印的圖片和命名*/
    imagepng($back,'test2.png');
    /*釋放資源*/
    imagedestroy($back);
    imagedestroy($water);
    }
    /*調(diào)用函數(shù)*/
    watermark('1.png','test.gif');
    ?>
    < div>
    <h4>加上水印</h4>
    <img src="test2.png">
    </div>