首頁 >後端開發 >php教程 >PHP缩放图片函数

PHP缩放图片函数

WBOY
WBOY原創
2016-06-20 13:02:16921瀏覽

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