Home > Backend Development > PHP Tutorial > Detailed explanation of how to enlarge and reduce images in equal proportions using PHP_PHP Tutorial

Detailed explanation of how to enlarge and reduce images in equal proportions using PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:08:34
Original
1032 people have browsed it

复制代码 代码如下:

    function resizeimage($srcfile,$mySize){
    $size=getimagesize($srcfile);
    switch($size[2]){
    case 1:
    $img=imagecreatefromgif($srcfile);
    break;
    case 2:
    $img=imagecreatefromjpeg($srcfile);
    break;
    case 3:
    $img=imagecreatefrompng($srcfile);
    break;
    }
    //源图片的宽度和高度
    $oldImg['w']=imagesx($img);
    $oldImg['h']=imagesy($img);
    if ($oldImg['w']<=$mySize['w'] && $oldImg['h']<156){
    $rate=1;
    }elseif ($oldImg['w']>$mySize['w'] && $oldImg['h']<$mySize['h']){
    $rate=$mySize['w']/$oldImg['w'];
    }elseif ($oldImg['w']<$mySize['w'] && $oldImg['h']>$mySize['h']){
    $rate=$mySize['h']/$oldImg['h'];
    }elseif ($oldImg['w']>$mySize['w'] && $oldImg['h']>$mySize['h']){
    $rate1=$mySize['w']/$oldImg['w'];
    $rate2=$mySize['h']/$oldImg['h'];
    if ($rate1>$rate2){$rate=$rate2;}else{$rate=$rate1;}
    }
    $newImg['w']=$oldImg['w']*$rate;
    $newImg['h']=$oldImg['h']*$rate;
    return "width=".$newImg['w']." height=".$newImg['h'];
    }

应用实例
复制代码 代码如下:

   $mySize=array('w'=>143,'h'=>156);
   $imgSize=resizeimage("22.jpg",$mySize);
   echo "";

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/327458.htmlTechArticle复制代码 代码如下: function resizeimage($srcfile,$mySize){ $size=getimagesize($srcfile); switch($size[2]){ case 1: $img=imagecreatefromgif($srcfile); break; case 2: $img=im...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template