PHP簡單圖片操作


<?php
//PHP操作圖片需打開配置文件中 extension=php_gd2.dll
//==================================================生成圖片
$imgname = "./test.png";
//根據給定路徑圖片名或 web 路徑創建 png 圖片模板對象,圖片不存在會報錯,需加 @
$im = @imagecreatefrompng ( $imgname );
//對應的創建jpg圖片的方法為 imagecreatefromjpeg ,例
//$im = imagecreatefromjpeg("http://wx.qlogo.cn/mmopen/Q3auHgzwzM4fA6602v35iaPhicn4NerIoHhHIsM6uBFjDnbn6saxic3QJBibmibterqicNwiajic1ff8Y2sPhj1ictV0wMw/096");
if (! $im) {
    //=======================創建圖片對象
    //如果指定圖片不存在,則創建指定大小的空圖片模板對象,寬800,高500
    $im = ImageCreate ( 800, 500 );
    //第二種創建圖片方法
    $im = ImageCreateTrueColor(800, 500);
    
    //=======================創建顏色對象
    //依據一個模板對象,生成顏色對象,0為red值,100為green值,30為blue值
    //ImageCreate 創建的對象在此會直接將顏色填充至模板對象中,ImageCreateTrueColor 創建的對象則只創建顏色對象,不填充
    $bgc = ImageColorAllocate ( $im, 0, 100, 30 );
    $tc  = ImageColorAllocate ( $im, 0, 0, 0 );
    
    //=======================填充顏色
    //為圖片對象填充矩形顏色,如果空模板對象不填充顏色則為黑色
    //100,50對應矩形填充區域左上角的橫縱坐標,150,200對應矩形右下角的橫縱坐標
    //這里坐標對ImageCreate模板對象不起作用,全局填充,對ImageCreateTrueColor起作用。
    ImageFilledRectangle ($im, 100, 50, 150, 200, $bgc);
    //第二種填充方法200,100同樣為左上角橫縱坐標,但這里坐標對兩種方法創建的模板對象都不起作用,完全填充
    ImageFill($im, 200, 100, $bgc);
    
    //=======================填充文字
    //在模板對象中添加文字兩種方法
    //40字體粗度,50字體左邊距距離,5字體上邊距距離,$tc字體顏色,這種方式只能填充英語,填中文亂碼
    ImageString ( $im, 40, 50, 5, "just is English code", $tc );
    //添加中文18為字體大小,0字體旋轉程度,100左邊距距離,200上邊距距離,項目目錄下要有"MSYH.TTF"這個字體文件
    imagettftext($im, 18, 0, 100, 200, $tc, "MSYH.TTF", "填充中文");
}



//==================================================合成圖片
// 載入圖象
$img1 = imagecreatefromjpeg("image1.jpg");
$img2 = imagecreatefromjpeg("064.jpg");

//方法一合成圖象 ,將參數二合成到參數一中,500離主圖片左邊距,400離主圖片上邊距,0新合圖片內右邊距偏移量,10新合圖片內下邊距偏移量, 243合到參數一后所占位置寬,90合到參數一后所占位置高
//imagecopy($img1,$img2,500,400,0,10,243,90);
//得到原始大圖片尺寸
$imgage  = getimagesize("064.jpg");
$src_W = $imgage[0]; //獲取原圖片寬度
$src_H = $imgage[1]; //獲取原圖片高度
//方法二合成圖象並改變大小,500,400,0,10,與imagecopy作用一致,200分別是縮小后寬,高,$src_W,$src_H原圖寬,高
//imagecopyresampled($img1, $img2, 500,400,0,10, 200, 200, $src_W, $src_H);
//方法三,並且合透明圖必須用此方法,別的合成方法合成后該透明的地方無法透明
//500,400,0,10, 243,90,與imagecopy作用一致,100表示參數二在參數一中透明程度,范圍0~100,0完全透明,100完全不透明
imagecopymerge($img1, $img2, 500,400,0,10, 243,90, 100);



//==================================================合成圓角圖片
//每次生成圓的四個角中一個角,中心透明,邊角有色的圖片,用於合成
function get_lt($halfWidth) {
    //根據圓形弧度創建一個正方形的圖像
    $img     = imagecreatetruecolor($halfWidth, $halfWidth);
    //圖像的背景
    $bgcolor = imagecolorallocate($img, 223, 0, 0);
    //填充顏色
    imagefill($img, 0, 0, $bgcolor);
    //定義圓中心顏色
    $fgcolor = imagecolorallocate($img, 0, 0, 0);
    // $halfWidth,$halfWidth:以圖像的右下角開始畫弧
    // $halfWidth*2, $halfWidth*2:已寬度、高度畫弧
    // 180, 270:指定了角度的起始和結束點
    // fgcolor:指定畫弧內的顏色
    imagefilledarc($img, $halfWidth, $halfWidth, $halfWidth*2, $halfWidth*2, 180, 270, $fgcolor, IMG_ARC_PIE);
    //將圖片中指定色設置為透明
    imagecolortransparent($img, $fgcolor);
    //變換角度
    // $img    = imagerotate($img, 90, 0);
    // $img    = imagerotate($img, 180, 0);
    // $img    = imagerotate($img, 270, 0);
    // header('Content-Type: image/png');
    // imagepng($img);
    return $img;
}

$img1 = imagecreatefrompng("vote_2.png");
$img2 = imagecreatefromjpeg("064.jpg");
//獲取圖片尺寸
$imgSize      = getimagesize("064.jpg");
$image_width  = $imgSize[0];
$image_height = $imgSize[1];

//圓角長度,最好是圖片寬,高的一半
$halfWidth     = $image_width / 2;

//獲取四分之一透明圓角
$lt_img    = get_lt($halfWidth);

//改造$img2 左上角圓角透明
imagecopymerge($img2, $lt_img, 0, 0, 0, 0, $halfWidth, $halfWidth, 100);
//旋轉圖片
$lb_corner    = imagerotate($lt_img, 90, 0);
//左下角
imagecopymerge($img2, $lb_corner, 0, $image_height - $halfWidth, 0, 0, $halfWidth, $halfWidth, 100);
//旋轉圖片
$rb_corner    = imagerotate($lt_img, 180, 0);
//右上角
imagecopymerge($img2, $rb_corner, $image_width - $halfWidth, $image_height - $halfWidth, 0, 0, $halfWidth, $halfWidth, 100);
//旋轉圖片
$rt_corner    = imagerotate($lt_img, 270, 0);
//右下角
imagecopymerge($img2, $rt_corner, $image_width - $halfWidth, 0, 0, 0, $halfWidth, $halfWidth, 100);

//生成紅色
$red = imagecolorallocate($img2, 223, 0, 0);
//去除參數二中紅色設成透明
imagecolortransparent($img2, $red);

imagecopymerge($img1, $img2, 117,37,0,0, 64, 64, 100);



//設定http輸出格式
//header ( "Content-type: image/png" );
header("Content-type: image/jpeg");

//將二進制文件流輸出到網頁
//imagePng ( $im );
//如果是jpg二進制流用 imagejpeg 輸出圖象,並且后面加路徑則直接生成保存圖片,不再在頁面輸出
//如果已有圖片則覆蓋生成
//imagejpeg($img1, "new.jpg");
imagejpeg($img1);

//注銷模板對象
ImageDestroy($im);
ImageDestroy($img1);
ImageDestroy($img2);
?> 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM