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中进行Cookie操作。

    在Web开发中,Cookie经常被用来记录用户的状态和跟踪用户的行为。Cookie是存储在用户浏览器上的一些数据,可以方便地在浏览器和服务器之间传递。在PHP中,操作Cookie非常简单,本文将通过以下几个方面介绍如何在PHP…

    2023年5月23日
    01
  • PHP入门指南:PHP和XML。

    PHP是一种流行的Web编程语言,已经被广泛应用于互联网和企业应用。PHP可以动态生成Web页面,提供功能强大的数据处理和交互。而XML是一种可扩展的标记语言,可以用来描绘复杂的数据结构和关系,是数据交换和存储的重…

    2023年5月30日
    00
  • 小编分享php设置虚拟主机的方法是什么意思。

    PHP虚拟主机是一种虚拟的服务器,可以在同一台物理主机上托管多个域名,并根据不同的域名提供不同的服务。在PHP中设置虚拟主机的方法有很多种,其中一种方法是在Apache PHP虚拟主机配置步骤中进行配置 。 什么是虚…

    2024年7月14日
    01
  • 我的php学习第二十一天之php基础篇

    昨日回顾 PHP变量 1)不需要提前定义,使用时直接赋一个值即可。 2)PHP的变量的命名规则,跟JS一样,允许的字符有:大小写英文字母、0-9、_ 3)PHP的变量必须以美元符号$开头;例如:$name=“周更生”; 4)PHP的变量…

    2015年12月1日
    0424
  • 如何使用PHP打造高性能的直播功能。

    随着网络技术和移动设备的不断发展,直播已成为一种流行的方式来分享和传播信息。而PHP被广泛应用于Web编程,也能用来实现高性能的直播功能。本文将介绍如何使用PHP打造高性能的直播功能。选择合适的PHP框架选择一…

    2023年5月23日
    00
  • 重蔚自留地php学习第四十二天——对象(面向对象)

    面向对象编程 面向对象编程不是技术,是一种思想而已。 面向对象编程:OOP(object oriented programming) 面向过程和面向对象 面向过程:将一个事务分成具体的某系列功能,然后将一连串的功能连起来使用,从而解…

    2018年12月24日 我php路线
    0315
  • PHP的逻辑运算符

    说明:逻辑运算符也叫“短路运算符”, 认为写的只判断一边如果达成条件直接结束给结果 逻辑或:|| 当两边的操作数其中一边为真,结果就是真。 当两边的操作数都为假时,结果就是假。 当前左边的操作数为假时,才去右…

    2017年12月7日 PHP自学教程
    0201
  • PHP8中的数组函数:array_chunk()的高效应用方法。

    随着PHP8的发布,数组函数的效率得到了大幅度提升。其中一个非常有用的数组函数是array_chunk(),它可以将一个数组按照指定大小分割成多个子数组。在本文中,我们将探讨如何高效地使用array_chunk()。简单的使用方…

    2023年5月21日
    00

联系我们

QQ:951076433

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