PHP生成缩略图的方法实例(附代码)

生成缩略图需要用到如下代码:

$source_path:原图的路径

$NewImagePath:生成缩略图路径

$target_width:缩略图宽度

$target_height:缩略图高度

详细代码如下:

 

 $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);
        
    }

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

= $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/4476.html

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

(0)
重蔚重蔚管理团队
上一篇 2018年2月28日 09:48
下一篇 2018年2月28日 14:59

相关推荐

  • 重蔚php学习第二十八天——引用文件(载入文件)

    相关函数 l  require()  :载入某个文件 l  include()  :载入某个文件 l  require_once() :载入某个文件,只载入一次 l  include_once() :载入某个文件,只载入一次 主要作用: 1)网站整体布局     (前台) 2…

    2017年10月4日 PHP自学教程
    0409
  • PHP8.0中的trait组合

    随着PHP语言的不断发展和升级,trait(特征)这个概念也越来越被程序员所认知和广泛应用。在PHP8.0版本中,trait组合成为了一个非常有价值的特性,对于编写高质量、易维护的代码来说,这是至关重要的。在过去的版本…

    2023年5月18日
    09
  • PHP实现数据库分区的方法。

    随着互联网应用的不断发展,数据量的增长也呈现出爆发式的增长趋势。对于存储海量数据的数据库而言,不仅需要具备高并发、高可用、高性能等特性,还需要满足数据治理、数据隔离、数据分级等数据安全需求。在此背景…

    2023年5月21日
    09
  • PHP实现数据库主从复制故障恢复的方法。

    随着互联网的迅速发展,大量的数据需要存储和处理,因此数据库成为现代应用开发中不可或缺的一部分。而在现实应用中,由于网络环境、硬件故障等多种因素的影响,数据库主从复制的故障恢复常常是一项必不可少的任务…

    2023年5月21日
    04
  • 阿里云PHP SMS短信服务验证码发送方法详解

    开通SMS服务首先去这个网站开通阿里云的SMS短信服务:https://www.aliyun.com/product/sms?spm=5176.8142029.388261.295.vU5T5g创建签名、模板要使用短信服务器需要先创建签名和模板,并提交给阿里云审核通过才可以…

    2022年6月17日 PHP自学教程
    0346
  • PHP网站布局的方式

    代码实现:准备模板 创建目录   创建配置文件 实现功能--封装函数

    2018年4月7日
    0207
  • 如何利用PHP实现商城的积分商城功能。

    现如今,随着电商行业的不断发展,积分商城功能已经越来越受到人们的关注和喜爱。顾客可以通过积分兑换商城中的商品,不仅为商家积攒人气,也使得顾客消费更具有吸引力。在这方面,PHP作为一种流行的编程语言,其优…

    2023年5月30日
    03
  • PHP入门指南:微服务架构。

    随着互联网的不断发展,越来越多的网站和应用程序应运而生。而对于开发者来说,如何快速高效地构建应用程序,是一个重要的挑战。其中,微服务架构已经成为了一个越来越受欢迎的解决方案。而PHP作为一种最受欢迎的We…

    2023年5月23日
    03

联系我们

QQ:951076433

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