php產生驗證碼,縮圖及浮水印圖的類執行個體詳解

來源:互聯網
上載者:User
這篇文章主要給大家分享的是php產生驗證碼,縮圖,及浮水印圖的類,十分的實用,有需要的小夥伴可以參考下

<?phpclass image{  session_start();  //驗證碼類  static public function verify($code,$width=75,$height=25,$n=4){    header("content-type:image/png");    // 建立畫布    $img=imagecreatetruecolor($width,$height);    // 設定背景色    $bgcolor=imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),rand(200,255));    // 將背景色填充    imagefill($img,0,0,$bgcolor);    // 繪製五條弧線    for($i=0;$i<5;$i++){      $arccolor=imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),rand(200,255));      imagearc($img,mt_rand(5,($width-5)),mt_rand(5,($height-5)),mt_rand(5,($width-5)),mt_rand(5,($height-5)),mt_rand(0,360),mt_rand(0,360),$arccolor);    }    // 繪製一百個點    for($i=0;$i<100;$i++){      $pixelcolor=imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),rand(200,255));      imagesetpixel($img,mt_rand(1,($width-1)),mt_rand(1,($height-1)),$pixelcolor);    }    // 繪製五條線段    for($i=0;$i<5;$i++){      $linecolor=imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),rand(200,255));      imageline($img,mt_rand(1,($width-1)),mt_rand(1,($height-1)),mt_rand(1,($width-1)),mt_rand(1,($height-1)),$linecolor);    }    // 設定邊框顏色    $bdcolor=imagecolorallocate($img,mt_rand(150,200),mt_rand(150,200),rand(150,200));    // 繪製一個矩形無填充邊框    imagerectangle($img,0,0,($width-1),($height-1),$bdcolor);    // 設定驗證碼字串    $str='';    for($i=1;$i<=$n;$i++){      $str.=substr(str_shuffle($code),0,1);    }    $_SESSION['a']=$str;    //    for($i=0;$i<$n;$i++){      $textcolor=imagecolorallocate($img,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150));      imagettftext($img,1/($n+1)*$width,mt_rand(-20,20),((1-$n/(5+$n))*$width/($n-1)+$i*1/($n+1)*$width),1/2*($height+3/5*$height),$textcolor,'1.ttf',substr($str,$i,1));    }    // 列印映像    imagepng($img);    // 釋放資源    imagedestroy($img);    return $str;  }  //縮圖類  /*  *$source需要縮減的圖片  */  static public function thumbnail($source,$deletesource=false,$width=180){    $info=getimagesize($source);    //getimagesize方法獲得映像的詳細資料    $createFun=str_replace('/','createfrom',$info['mime']);    //將getimagesize中的['mime']中的/替換,變為imagecreatefromjpeg方法    $src=$createFun($source);    //建立映像    $dst_w=$width;    //縮圖的寬    $dst_h=$width/$info[0]*$info[1];    //計算縮圖的高    $dst=imagecreatetruecolor($dst_w,$dst_h);    //建立一個真彩色映像    imagecopyresampled($dst,$src,0,0,0,0,$dst_w,$dst_h,$info['0'],$info['1']);    //重採樣拷貝部分映像並調整大小    $saveFun=str_replace('/','',$info['mime']);    //設定儲存函數    $ext=strrchr($source,'.');    //strrchr尋找指定字元在字串中的最後一次出現,返回包括其自身在內的剩餘的字串    $thumbnailName=str_replace($ext,'',$source).'_thumbnail'.$ext;    //設定儲存路徑    if(!$deletesource){      $saveFun($dst,$thumbnailName);      //儲存映像    }else{      $saveFun($dst,$thumbnailName);      unlink($source);      //刪除原圖    }    imagedestroy($src);    //銷毀資源    imagedestroy($dst);    //銷毀資源    return $thumbnailName;   }  //浮水印圖類  /*  *$srcing浮水印圖  *$dsting原圖  */  static public function watermark($dstimg,$srcimg){    $dstinfo=getimagesize($dstimg);    //getimagesize方法擷取該圖片的詳細資料(該方法返回的是一個詳細的數組)    $srcinfo=getimagesize($srcimg);    $createdst=str_replace('/','createfrom',$dstinfo['mime']);    //將getimagesize方法返回的數組中的mime值中的/替換,結果為imagecreatefromjpeg    $createsrc=str_replace('/','createfrom',$srcinfo['mime']);    $dst=$createdst($dstimg);    //替換後為imagecreatefromjpeg函數,建立一個映像    $dst_w=imagesx($dst);    //擷取大圖的寬    $dst_h=imagesy($dst);    //擷取大圖的高    $src=$createsrc($srcimg);    //替換後為imagecreatefromjpeg函數,建立一個映像    $src_w=imagesx($src);    //擷取浮水印圖的寬    $src_h=imagesy($src);    //擷取浮水印圖的高    $watermaker=imagecopy($dst,$src,($dst_w-$src_w),($dst_h-$src_h),0,0,$src_w,$src_h);    //將浮水印圖複製在大圖上    $saveFun=str_replace('/','',$dstinfo['mime']);    //將getimagesize返回數組中的['mime']中的斜杠替換為空白,替換後的值為imagejpeg,即為儲存函數    $ext=strrchr($dstimg,'.');    //strrchr尋找指定字元在字串中的最後一次出現,返回包括其自身在內的剩餘的字串    $watermaker=str_replace($ext,'',$dstimg).'_water'.time().$ext;    //儲存名    $saveFun($dst,$watermaker);    //儲存圖片,第一個參數為要儲存的圖片,第二個參數為儲存路徑或儲存名    imagedestroy($dst);    //銷毀資源    imagedestroy($src);    //銷毀資源    return $watermaker;   } }?>

總結:以上就是本篇文的全部內容,希望能對大家的學習有所協助。

聯繫我們

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