首页  >  文章  >  后端开发  >  PHP缩放图片函数

PHP缩放图片函数

WBOY
WBOY原创
2016-06-20 13:02:16902浏览

function resize_image($filename, $tmpname, $xmax, $ymax)  <br />{  <br />    $ext = explode(".", $filename);  <br />    $ext = $ext[count($ext)-1];  <br />   <br />    if($ext == "jpg" || $ext == "jpeg")  <br />        $im = imagecreatefromjpeg($tmpname);  <br />    elseif($ext == "png")  <br />        $im = imagecreatefrompng($tmpname);  <br />    elseif($ext == "gif")  <br />        $im = imagecreatefromgif($tmpname);  <br />       <br />    $x = imagesx($im);  <br />    $y = imagesy($im);  <br />       <br />    if($x <= $xmax && $y <= $ymax)  <br />        return $im;  <br />   <br />    if($x >= $y) {  <br />        $newx = $xmax;  <br />        $newy = $newx * $y / $x;  <br />    }  <br />    else {  <br />        $newy = $ymax;  <br />        $newx = $x / $y * $newy;  <br />    }  <br />       <br />    $im2 = imagecreatetruecolor($newx, $newy);  <br />    imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);  <br />    return $im2;   <br />}


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn