在开发的时间,我们经常遇到需要把图片旋转的功能,今天麦讯网络分享一个封装的函数。
/*** 图片旋转* @param $src 图片地址* @param $direction 1顺时针90 2 逆时针90* @return string*/function imgturn($src, $direction = 1){$ext = pathinfo($src)['extension'];switch ($ext) {case 'gif':$img = imagecreatefromgif($src);break;case 'jpg':case 'jpeg':$img = imagecreatefromjpeg($src);break;case 'png':$img = imagecreatefrompng($src);break;default:die('图片格式错误!');break;}$width = imagesx($img);$height = imagesy($img);$img2 = imagecreatetruecolor($height, $width);//顺时针旋转90度if($direction == 1){for ($x = 0; $x < $width; $x++) {for($y=0; $y<$height; $y++) {imagecopy($img2, $img, $height - 1 - $y, $x, $x, $y, 1, 1);}}}else if($direction == 2) {//逆时针旋转90度for ($x = 0; $x < $height; $x++) {for($y = 0; $y < $width; $y++) {imagecopy($img2, $img, $x, $y, $width - 1 - $y, $x, 1, 1);}}}switch ($ext) {case 'jpg':case "jpeg":imagejpeg($img2, $src, 100);break;case "gif":imagegif($img2, $src, 100);break;case "png":imagepng($img2, $src, 100);break;default:die('图片格式错误!');break;}imagedestroy($img);imagedestroy($img2);}