利用PHP实现图片等比例放大和缩小的方法详解

yipeiwu_com6年前PHP代码库
复制代码 代码如下:

    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 "<img src=\"22.jpg\"".resizeimage("22.jpg",$mySize)."/>";

相关文章

PHP 配置文件中open_basedir选项作用

如下是php.ini中的原文说明以及默认配置: ; open_basedir, if set, limits all file operations to the defined dir...

PHP正则表达式处理函数(PCRE 函数)实例小结

本文实例讲述了PHP正则表达式处理函数。分享给大家供大家参考,具体如下: 有时候在一些特定的业务场景中需要匹配,或者提取一些关键的信息,例如匹配网页中的一些链接, 提取一些数据时,可能会...

PHP中fwrite与file_put_contents性能测试代码

function microtimeFloat() {    list($usec,$sec) = explode(" ", microtime());&n...

PHP实现带重试功能的curl连接示例

本文实例讲述了PHP实现带重试功能的curl连接方法。分享给大家供大家参考,具体如下: /** * @param string $url 访问链接 * @param strin...

PHP漏洞全解(详细介绍)

PHP漏洞全解(详细介绍)

针对PHP的网站主要存在下面几种攻击方式: 1、命令注入(Command Injection) 2、eval注入(Eval Injection) 3、客户端脚本攻击(Script In...