Home >Backend Development >PHP Tutorial >PHP缩放图片函数

PHP缩放图片函数

WBOY
WBOYOriginal
2016-06-20 13:02:16918browse

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 />}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn