PHP生成缩略图有实现过,但是生成填充白边的实现过吗?-(附代码)

PHP生成缩略图有实现过,但是生成填充白边的实现过吗?-(附代码)

PHP生成缩略图,相信很多人都实现过吧,没有的友友也看看吧。最近在坐一个生成缩略图的功能,还要要求上传的图片没有和限制的宽高的话,自动补白边,以下是自己实践过的例子,和大家分享一下,我主要用的还是laravel框架的,以下方法适用其他的框架,原生的也是可以的。其实懂PHP的友友,用在哪里都是可以的

$source_path:原图的路径

$NewImagePath:生成缩略图路径

$target_width:缩略图宽度

$target_height:缩略图高度

 

<?php function getCropper($source_path,$NewImagePath, $target_width, $target_height) { $source_info = getimagesize($source_path); $source_width = $source_info[0]; $source_height = $source_info[1]; $source_mime = $source_info['mime']; $source_ratio = $source_height / $source_width; $target_ratio = $target_height / $target_width; // 源图过高 if ($source_ratio > $target_ratio)
        {
            $cropped_width  = $source_width;
            $cropped_height = $source_width * $target_ratio;
            $source_x = 0;
            $source_y = ($source_height - $cropped_height) / 2;
        }
        // 源图过宽
        elseif ($source_ratio < $target_ratio)
        {
            $cropped_width  = $source_height / $target_ratio;
            $cropped_height = $source_height;
            $source_x = ($source_width - $cropped_width) / 2;
            $source_y = 0;
        }
        // 源图适中
        else
        {
            $cropped_width  = $source_width;
            $cropped_height = $source_height;
            $source_x = 0;
            $source_y = 0;
        }

        switch ($source_mime)
        {
            case 'image/gif':
                $source_image = imagecreatefromgif($source_path);
                break;

            case 'image/jpeg':
                $source_image = imagecreatefromjpeg($source_path);
                break;

            case 'image/png':
                $source_image = imagecreatefrompng($source_path);
                break;

            default:
                return false;
                break;
        }

        $target_image  = imagecreatetruecolor($target_width, $target_height);
        $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);

        // 图片裁剪
        imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
        // 图片缩放
        imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);


        header('Content-Type: image/jpeg');
        imagejpeg($target_image,$NewImagePath,100);
        imagedestroy($source_image);
        imagedestroy($target_image);
        imagedestroy($cropped_image);

    }

以下方法是生成缩略图,填充白边的方法

<?php //生成缩略图,填充白边 function getCrops($src_path,$NewImagePath,$width,$height){ //源图对象 $src_image = imagecreatefromstring(file_get_contents($src_path)); $source_info = getimagesize($src_path); $source_mime = $source_info['mime']; $src_width = imagesx($src_image); $src_height = imagesy($src_image); switch ($source_mime) { case 'image/gif': $src_image = imagecreatefromgif($src_path); break; case 'image/jpeg': $src_image = imagecreatefromjpeg($src_path); break; case 'image/png': $src_image = imagecreatefrompng($src_path); break; default: return false; break; } //生成等比例的缩略图 //$tmp_image_width = 0; //$tmp_image_height = 0; if ($src_width / $src_height >= $width / $height) {

            $tmp_image_width = $width;
            $tmp_image_height = round($tmp_image_width * $src_height / $src_width);

        } else {

            $tmp_image_height = $height;
            $tmp_image_width = round($tmp_image_height * $src_width / $src_height);
        }

        $tmpImage = imagecreatetruecolor($tmp_image_width, $tmp_image_height);
        imagecopyresampled($tmpImage, $src_image, 0, 0, 0, 0, $tmp_image_width, $tmp_image_height, $src_width, $src_height);

        //添加白边
        $final_image = imagecreatetruecolor($width, $height);
        $color = imagecolorallocate($final_image, 255, 255, 255);
        imagefill($final_image, 0, 0, $color);
        $x = round(($width - $tmp_image_width) / 2);
        $y = round(($height - $tmp_image_height) / 2);
        imagecopy($final_image, $tmpImage, $x, $y, 0, 0, $tmp_image_width, $tmp_image_height);

        //输出图片
        header('Content-Type: image/jpeg');
        imagejpeg($final_image,$NewImagePath,100);
        imagedestroy($src_image);
        imagedestroy($final_image);


    }

本文来自投稿,不代表重蔚自留地立场,如若转载,请注明出处https://www.cwhello.com/9718.html

如有侵犯您的合法权益请发邮件951076433@qq.com联系删除

(0)
重蔚的头像重蔚管理团队
上一篇 2018年8月27日 00:00
下一篇 2018年8月27日 08:54

相关推荐

  • php自带函数strip_tags去除html标签

    strip_tags() 函数剥去 HTML、XML 以及 PHP 的标签。 语法: string strip_tags(string,allow);传回值: 字串 参数 描述string 必需。规定要检查的字符串。allow 可选。规定允许的标签。这些标签不会被删除。提示和注…

    2018年3月14日
    0394
  • PHP如何实现微信小程序中的团购功能。

    随着移动互联网的普及和微信生态系统的不断扩大,微信小程序的使用越来越广泛。微信小程序有很多种应用场景,其中一个比较常见的场景就是商户开发平台,通过小程序实现商品的展示和销售。在这个过程中,团购功能也…

    2023年6月3日
    03
  • 如何访问成员属性

    如何访问成员属性 类名的规范说明 类名不区分大小写. 类名命名规范 关于函数,变量,常量,接口等等的命名规范,请参考手册. 对象传递方式(重点, 难点) 先请大家看一段代码,从而引起思考 注意当 对象传递方式是 $p…

    2018年4月9日
    0190
  • 重蔚自留地php学习第三十九天——mysql事物触发器函数过程

    数据备份 将数据里的数据进行保存到外部文件,从而在数据库内部数据丢失或者出错的情况下能够通过备份文件进行还原操作,从而将损失降低到最小。 对单表内的纯数据进行备份 将表中的数据(不包含结构,没有字段头信…

    2018年10月23日 MySQL自学教程
    0267
  • php如何使用Requests进行HTTP请求。

    在Web开发中,HTTP请求是一个非常重要的环节。在PHP开发中,有很多种方式可以进行HTTP请求,其中一种比较好用的就是使用Requests库进行请求。本文将介绍如何在PHP中使用Requests进行HTTP请求。 什么是Requests库? …

    2023年6月3日
    02
  • PHP结合MySQL实现千万级数据处理

    mysql分表思路 一张一亿的订单表,可以分成五张表,这样每张表就只有两千万数据,分担了原来一张表的压力,分表需要根据某个条件进行分,这里可以根据地区来分表,需要一个中间件来控制到底是去哪张表去找到自己想…

    2022年6月23日 PHP自学教程
    0146
  • PHP中的对象存储。

    随着互联网技术的不断发展,越来越多的企业和开发者开始选择使用对象存储来存储和管理大量的数据。对象存储是一种存储数据的方式,它将数据存储为对象,每个对象都有唯一的标识符并且可以被随时访问。相比传统的文…

    2023年5月30日
    01
  • 掌握PHP 爬取网页的主要方法

    主要流程就是获取整个网页,然后正则匹配(关键的)。 PHP抓取页面的主要方法,有几种方法是网上前辈的经验,现在还没有用到的,先存下来以后试试。 1.file()函数 2.file_get_contents()函数 3.fopen()->fread()-…

    2022年6月14日
    0135

联系我们

QQ:951076433

在线咨询:点击这里给我发消息邮件:951076433@qq.com工作时间:周一至周五,9:30-18:30,节假日休息