來源:http://www.ido321.com/887.html
5、PHP對圖像的旋轉
1:
2:旋轉之前
3:
4:
5:6: header("content-type","text/html;charset=utf-8");7:8: /*9: *圖片沿y軸旋轉,以png格式為例10: *@param string $filename 圖片的url11: */12: function turn_y($filename)13: {14: /*創建圖片資源*/15: $backy = imagecreatefrompng($filename);16:17: /*獲取大小*/18: $widthy = imagesx($backy);19: $heighty = imagesy($backy);20:21: /*創建新的圖片資源,保存翻轉後的圖片*/22: $newy = imagecreatetruecolor($widthy, $heighty);23:24: /*沿著y軸翻轉,就是將原圖從右向左按一個像素寬度向新資源中逐個復制*/25: for ($i=0; $i < $widthy; $i++) {26: imagecopy($newy,$backy,$widthy-$i-1,0,$i,0,1,$heighty);27: }28:29: /*保存翻轉後的圖片*/30: imagepng($newy,'https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709565162.png');31:32: /*釋放資源*/33: imagedestroy($backy);34: imagedestroy($newy);35: }36:37: /*38: *圖片沿x軸旋轉,以png格式為例39: *@param string $filename 圖片的url40: */41: function turn_x($filename)42: {43: /*創建圖片資源*/44: $backx = imagecreatefrompng($filename);45:46: /*獲取大小*/47: $widthx = imagesx($backx);48: $heightx = imagesy($backx);49:50: /*創建新的圖片資源,保存翻轉後的圖片*/51: $newx = imagecreatetruecolor($widthx, $heightx);52:53: /*沿著x軸翻轉,就是將原圖從上到下按一個像素寬度向新資源中逐個復制*/54: for ($i=0; $i < $heightx; $i++) {55: imagecopy($newx,$backx,0,$heightx-$i-1,0,$i,$widthx,1);56: }57:58: /*保存翻轉後的圖片*/59: imagepng($newx,'https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709565155.png');60:61: /*釋放資源*/62: imagedestroy($backx);63: imagedestroy($newx);64: }65: /*調用函數*/66: turn_y('https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709565169.png');67: turn_x('https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709565169.png');68: ?>69:70:沿著y軸旋轉
71:72:73:74:沿著x軸旋轉
75:76:效果