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回调函数以及匿名函数用法与概念详解(基础篇)

    1、回调函数 PHP的回调函数其实和C、Java等语言的回调函数的作用是一模一样的,都是在主线程执行的过程中,突然跳去执行设置的回调函数;回调函数执行完毕之后,再回到主线程处理接下来的流程而在php调用回调函数...

    2018年8月30日
    0295
  • PHP商城的物流配送系统设计与实现。

    随着电商行业的不断发展壮大,物流配送系统已经成为了电商企业中不可或缺的一部分。在PHP商城开发中,物流配送系统的设计和实现显得尤为重要。通过合理的物流配送系统设计,可以提高顾客的购物体验,同时也可以节...

    2023年5月23日
    06
  • 如何进行PHP的安全管理?

    PHP作为一种开源的编程语言,广泛应用于Web开发领域。然而,随着网络安全威胁不断增加,对PHP应用的安全管理也变得越来越重要。在这篇文章中,我们将探讨如何进行PHP的安全管理,以保障应用的安全性。优化代码在...

    2023年5月17日
    02
  • PHP 生成唯一订单号函数

    PHP 生成唯一订单号函数 一、应用场景        有电子商城项目,需要生成订单号。当时的考虑很简单,取系统时间加上随机数,或者使用 uniqid() 方法。仔细考虑下上述方法,在顾客购买量少的情况下,订单重复的可能...

    2018年4月28日 PHP案例操作
    0242
  • PHP与数据库性能分析的集成。

    PHP作为一种开源的服务器端脚本语言,广泛应用于Web开发领域。为了提高服务器端应用程序的性能,开发人员需要针对系统进行性能分析,找出瓶颈所在并加以优化。在众多性能分析工具中,数据库性能分析工具是至关重...

    2023年5月21日
    00
  • 如何在PHP商城开发中增强数据安全性

    随着电商市场的不断发展,越来越多的企业开始使用PHP开发电商平台,并将其作为开发工具。但是,由于缺少对数据安全的有效注意,许多商家经常面临数据泄漏和网站遭受黑客攻击等问题。因此,保障电商平台的数据安全...

    2023年5月18日
    05
  • PHP中的自动化测试工具。

    随着现代软件开发的进化,自动化测试已成为不可或缺的一环。在PHP开发中,自动化测试工具的使用也越来越普遍。本文将介绍PHP中常用的自动化测试工具以及它们的优缺点。PHPUnitPHPUnit是PHP最流行的自动化测试框架...

    2023年5月30日
    01
  • PHP入门指南:PHP和Logstash。

    首先,让我们简单介绍一下PHP和Logstash是什么。PHP是一种用于Web开发的脚本语言,它广泛应用于服务器端的开发,可用于构建动态网站,Web应用程序和Web服务。它也可以与MySQL和其他数据库一起工作,以便收集和处...

    2023年5月22日
    04

联系我们

QQ:951076433

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