• 技术文章 >后端开发 >php教程

    Php Image Resize图片大小调整的函数代码_PHP教程

    2016-07-21 15:32:27原创971
    复制代码 代码如下:

    function my_image_resize($src_file, $dst_file, $dst_width=32, $dst_height=32) {
    if($dst_width <1 || $dst_height <1) {
    echo "params width or height error !";
    exit();
    }
    if(!file_exists($src_file)) {
    echo $src_file . " is not exists !";
    exit();
    }

    $type=exif_imagetype($src_file);
    $support_type=array(IMAGETYPE_JPEG , IMAGETYPE_PNG , IMAGETYPE_GIF);

    if(!in_array($type, $support_type,true)) {
    echo "this type of image does not support! only support jpg , gif or png";
    exit();
    }

    switch($type) {
    case IMAGETYPE_JPEG :
    $src_img=imagecreatefromjpeg($src_file);
    break;
    case IMAGETYPE_PNG :
    $src_img=imagecreatefrompng($src_file);
    break;
    case IMAGETYPE_GIF :
    $src_img=imagecreatefromgif($src_file);
    break;
    default:
    echo "Load image error!";
    exit();
    }
    $src_w=imagesx($src_img);
    $src_h=imagesy($src_img);
    $ratio_w=1.0 * $dst_width/$src_w;
    $ratio_h=1.0 * $dst_height/$src_h;
    if ($src_w<=$dst_width && $src_h<=$dst_height) {
    $x = ($dst_width-$src_w)/2;
    $y = ($dst_height-$src_h)/2;
    $new_img=imagecreatetruecolor($dst_width,$dst_height);
    imagecopy($new_img,$src_img,$x,$y,0,0,$dst_width,$dst_height);
    switch($type) {
    case IMAGETYPE_JPEG :
    imagejpeg($new_img,$dst_file,100);
    break;
    case IMAGETYPE_PNG :
    imagepng($new_img,$dst_file);
    break;
    case IMAGETYPE_GIF :
    imagegif($new_img,$dst_file);
    break;
    default:
    break;
    }
    } else {
    $dstwh = $dst_width/$dst_height;
    $srcwh = $src_w/$src_h;
    if ($ratio_w <= $ratio_h) {
    $zoom_w = $dst_width;
    $zoom_h = $zoom_w*($src_h/$src_w);
    } else {
    $zoom_h = $dst_height;
    $zoom_w = $zoom_h*($src_w/$src_h);
    }

    $zoom_img=imagecreatetruecolor($zoom_w, $zoom_h);
    imagecopyresampled($zoom_img,$src_img,0,0,0,0,$zoom_w,$zoom_h,$src_w,$src_h);
    $new_img=imagecreatetruecolor($dst_width,$dst_height);
    $x = ($dst_width-$zoom_w)/2;
    $y = ($dst_height-$zoom_h)/2+1;
    imagecopy($new_img,$zoom_img,$x,$y,0,0,$dst_width,$dst_height);
    switch($type) {
    case IMAGETYPE_JPEG :
    imagejpeg($new_img,$dst_file,100);
    break;
    case IMAGETYPE_PNG :
    imagepng($new_img,$dst_file);
    break;
    case IMAGETYPE_GIF :
    imagegif($new_img,$dst_file);
    break;
    default:
    break;
    }
    }
    }

    www.bkjia.comtruehttp://www.bkjia.com/PHPjc/322825.htmlTechArticle复制代码 代码如下: function my_image_resize($src_file, $dst_file, $dst_width=32, $dst_height=32) { if($dst_width 1 || $dst_height 1) { echo "params width or height error !"...

    php入门到就业线上直播课:查看学习

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

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    专题推荐:Php Image Resize 图片 大小 调整 函数 代码 复制 代码 如下 function image re
    上一篇:PHP企业级应用之常见缓存技术篇_PHP教程 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• 你知道如何用PHP实现多进程吗• PHP与MySQL连接的方法总结• 求解:phpcms模板怎样转码?该怎么解决• php 之 cookie 跟 session 简单解读(笔记)• php怎的快捷知道某个函数在哪个文件用过
    1/1

    PHP中文网