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循环输出某个文件夹下所有子文件以及子文件夹

    静态变量 一般用于函数内,需要使用static关键字定义,静态变量的赋值语句只会执行一次。 第一次执行display,会执行static $i=10,i再加1,再输出i,函数执行后i不会被回收 第二次执行display,不会执行static $i=1…

    2017年11月13日 PHP自学教程
    0332
  • 五种PHP实现定时任务的方法

    定时运行任务对于一个网站来说,是一个比较重要的任务,比如定时发布文档,定时清理垃圾信息等,现在的网站大多数都是采用PHP动态语言开发的,而对于PHP的实现决定了它没有Java和.Net这种AppServer的概念,而http协…

    2022年6月27日
    0118
  • 如何让tomcat支持PHP运行

    java开发者都知道,tomcat是用来部署java web项目的。要求与PHP项目使用相同域名、相同端口,在不使用nginx的情况下如何实现? 得知通过Java Bridge 可以实现tomcat支持运行php运行。 下面是详细步骤。1.环境准备安…

    2023年3月29日
    00
  • PHP与数据仓库的集成。

    随着互联网和大数据的快速发展,越来越多的企业开始将数据仓库(data warehouse)作为支撑业务发展的重要基础设施。而作为一种流行的编程语言,PHP也逐渐成为了许多企业和组织的首选,那么如何将PHP与数据仓库集成…

    2023年5月21日
    02
  • 聊聊php数组排序的函数有哪些。

    以下是 PHP 数组排序函数的列表:,,- sort() – 对数组进行升序排列,- rsort() – 对数组进行降序排列,- asort() – 根据关联数组的值,对数组进行升序排列,- ksort() – 根据关联数组的…

    2024年7月13日
    00
  • PHP中的邮件处理。

    随着互联网的发展,邮件已经成为人们日常生活和工作中必不可少的一部分。在网站开发中,会经常遇到需要通过邮件发送用户注册,找回密码等信息的场景。PHP中通过使用邮件处理类库,可以方便地实现与邮件相关的操作,…

    2023年5月30日
    02
  • 如何在PHP中使用PHPUnit框架进行测试。

    随着Web开发的不断演进,测试已经成为了一个必不可少的部分。在Web开发中,测试可以帮助我们确保代码的质量以及提高开发效率。而PHPUnit框架则是PHP中最常用的测试框架之一,提供了丰富的测试工具和API,使得开发者…

    2023年5月23日
    00
  • 小编教你php 建站模板。

    “快速搭建PHP网站,提供高质量建站模板。” 建站模板为什么都是PHP? 在互联网高速发展的今天,网站已经成为了企业、个人展示自己的重要平台,而建站模板则是网站建设过程中不可或缺的一部分,为什么建…

    2024年7月25日
    00

联系我们

QQ:951076433

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