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与NoSQL数据库的对比

    PHP和NoSQL数据库都是现今非常流行的技术,前者是一种流行的服务器端编程语言,后者则是一种非关系型数据库,主要用于处理半结构化和非结构化数据。PHP和NoSQL数据库分别在不同领域得到了广泛应用,但它们之间的...

    2023年5月19日
    00
  • PHP入门指南:CDN加速。

    在现代的网站开发中,网站的性能对于用户体验和搜索引擎排名都起着至关重要的作用。其中,网站的速度是用户最为关注的一个因素。如果网站的加载速度很慢,用户往往会选择离开这个网站或寻找速度更快的替代品。因...

    2023年5月22日
    00
  • PHP入门指南:表单处理。

    PHP是一种十分常见的服务器端编程语言,其应用广泛,尤其在Web开发中被广泛采用。通过使用PHP,我们可以访问数据库、创建动态页面、从表单中收集数据等。本篇文章将介绍如何使用PHP处理表单。前置知识在开始学习P...

    2023年5月22日
    04
  • PHP微信开发:如何实现多公众号管理

    随着微信公众号市场的日益火热,越来越多的企业和个人开始关注微信公众号开发,尤其是PHP微信开发技术。但是,对于需要同时管理多个公众号的人来说,如何实现多公众号的管理,是一个需要解决的问题。本文将介绍PH...

    2023年5月18日
    04
  • 谈谈PHP中interface的用处

    确实,PHP 接口是有它的目的的。它们是契约,是给其他开发人员的说明手册。然而,还是很难理解接口有什么用。基础接口是抽象的类,无法直接实例化,但是可被实现。这是一个基本的例子interface MyInterface { pub...

    2022年6月11日
    0156
  • PHP实用函数集合

    实用函数集合<?php if (!function_exists('number_random'😉) { /** * 生成随机数字串 * * @param int $length * @return string */ function number_random($length = 6) { $result = '';...

    2022年6月27日
    0116
  • PHP中限制IP段访问、禁止IP提交表单的代码(附代码)

    本文为大家讲解的是PHP中限制IP段访问、禁止IP提交表单的代码示例,感兴趣的同学参考下。 下边只是一个PHP限制IP的实例代码,如果您打算应用到CMS中,请自行修改

    2017年9月27日
    0382
  • php如何使用PHPUnit进行单元测试。

    作为一种流行的开源Web编程语言,PHP在Web开发领域中得到了广泛的应用。单元测试是一种非常重要的开发方式,能够有效保证代码的可靠性和稳定性。而PHPUnit则是PHP领域中使用最广泛的单元测试框架,具有丰富的功能...

    2023年6月3日
    00

联系我们

QQ:951076433

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