這篇文章主要介紹了php+js如何實(shí)現(xiàn)裁剪任意形狀圖片,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
成都創(chuàng)新互聯(lián)2013年至今,先為鳳臺(tái)等服務(wù)建站,鳳臺(tái)等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為鳳臺(tái)企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。1、能夠嵌入動(dòng)態(tài)文本于HTML頁(yè)面。2、對(duì)瀏覽器事件做出響應(yīng)。3、讀寫HTML元素。4、在數(shù)據(jù)被提交到服務(wù)器之前驗(yàn)證數(shù)據(jù)。5、檢測(cè)訪客的瀏覽器信息。6、控制cookies,包括創(chuàng)建和修改等。7、基于Node.js技術(shù)進(jìn)行服務(wù)器端編程。
最近做到相關(guān)的項(xiàng)目,由于項(xiàng)目使用html2canvas,但是不支持css mask屬性,故,利用php后臺(tái)來(lái)裁剪。
準(zhǔn)備兩張圖片,一張是鏤空PNG圖案,一張是任意純色圖片。
便能夠在純色圖片上裁剪出鏤空的圖案為PNG文件。
見下圖。
首先兩張PNG圖片:
生成圖片
JS片段:
html2canvas($(".head1pic"), { onrendered: function(canvas) { url = canvas.toDataURL("image/png", 1.0); sourcePic = "assets/images/demo.png"; maskPic = "assets/images/jinmao.png"; cropPicName = "cropDog1"; // ajax php截圖 $.ajax({ type: 'post', url: 'getpicture', data: { "sourcePic": sourcePic, "maskPic": maskPic, "cropPicName": cropPicName }, success: function(data) { $(".page2Bg")[0].setAttribute("src", "assets/images/crop/cropDog1.png"); }, error: function(data) { console.log(data) } }); } });
PHP的片段:
public function actionGetpicture() { $request = Yii::$app->request; $sourcePic=$request->post('sourcePic'); $maskPic=$request->post('maskPic'); $cropPicName=$request->post('cropPicName'); // $sourcePic="http://bings.local.com/bi_ngs2_2/assets/images/yinpian1/page2Bg4.png"; // $maskPic="http://bings.local.com/bi_ngs2_2/assets/images/jinmao.png"; $source = imagecreatefrompng( $sourcePic ); $mask = imagecreatefrompng( $maskPic); // Apply mask to source // imagealphamask( $source, $mask ); $this->imagealphamask ($source, $mask ); // Output header( "Content-type: image/png"); // 生成截取后的圖片并保存在本地 imagepng( $source,"assets/images/crop/".$cropPicName.".png" ); //銷毀圖片內(nèi)存 imagedestroy($source); } public function imagealphamask( &$picture, $mask ) { // Get sizes and set up new picture $xSize = imagesx( $picture ); $ySize = imagesy( $picture ); $newPicture = imagecreatetruecolor( $xSize, $ySize ); imagesavealpha( $newPicture, true ); imagefill( $newPicture, 0, 0, imagecolorallocatealpha( $newPicture, 100, 100, 0, 127 ) ); // Resize mask if necessary // if( $xSize != imagesx( $mask ) || $ySize != imagesy( $mask ) ) { // $tempPic = imagecreatetruecolor( $xSize, $ySize ); // imagecopyresampled( $tempPic, $mask, 0, 0, 0, 0, $xSize, $ySize, imagesx( $mask ), imagesy( $mask ) ); // imagedestroy( $mask ); // $mask = $tempPic; // } // Perform pixel-based alpha map application for( $x = 0; $x < $xSize; $x++ ) { for( $y = 0; $y < $ySize; $y++ ) { $alpha = imagecolorsforindex( $mask, imagecolorat( $mask, $x, $y ) ); //small mod to extract alpha, if using a black(transparent) and white //mask file instead change the following line back to Jules's original: // $alpha = 127 - floor($alpha['black'] / 2); //or a white(transparent) and black mask file: // $alpha = floor($alpha['black'] / 2); $alpha = $alpha['alpha']; $color = imagecolorsforindex( $picture, imagecolorat( $picture, $x, $y ) ); //preserve alpha by comparing the two values if ($color['alpha'] > $alpha) $alpha = $color['alpha']; //kill data for fully transparent pixels if ($alpha == 127) { $color['red'] = 0; $color['blue'] = 0; $color['green'] = 0; } imagesetpixel( $newPicture, $x, $y, imagecolorallocatealpha( $newPicture, $color[ 'red' ], $color[ 'green' ], $color[ 'blue' ], $alpha ) ); } } // Copy back to original picture imagedestroy( $picture ); $picture = $newPicture; }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“php+js如何實(shí)現(xiàn)裁剪任意形狀圖片”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!