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

相关推荐

  • Redis中的布隆过滤器和PHP的使用方法。

    Redis是一个开源的内存数据库,被广泛应用于缓存、消息队列、分布式锁等场景。其中,布隆过滤器是一种高效的数据结构,可以用于判断一个元素是否存在于一个集合中,在Redis中得到了广泛的应用。本文将介绍Redis中...

    2023年5月21日
    00
  • PHP文件下载练习

    html语言本身可以提供下载功能 格式: <a  href=’1.zip’>下载</a> html下载的缺点:   1、只能实现某几种格式的文件下载 2、暴露文件所在服务器的完整路径 我们可以使用php下载解决以上问题: p...

    2017年11月15日
    0210
  • PHP操作MySQL的流程

    1.链接数据库 2.选择数据库并设置编码 3.准备SQL语句 4.发生SQL语句到MySQL服务器 5.接收返回的结果集资源 6.解析结果集资源 7.关闭链接资源 流程图:

    2018年3月23日
    0290
  • PHP如何处理微信小程序中的session问题。

    近年来,微信小程序风靡全球,已经成为了许多企业和个人开发者的首选平台。在小程序的开发中,我们经常会遇到session问题,也就是如何在小程序中保存用户登录状态。这个问题对于网站开发者来说并不陌生,但在小程...

    2023年6月3日
    05
  • PHP函数的DI框架。

    随着互联网技术的不断发展,各种编程语言和框架层出不穷。其中,PHP作为一种广泛应用于Web开发的编程语言,已经成为了建立Web应用程序的热门选择之一。同时,由于许多Web应用程序都需要复杂的逻辑控制,因此使用...

    2023年5月23日
    02
  • PHP8.0中的WeakReference类型

    PHP8.0中的WeakReference类型随着PHP8.0版本的正式发布,我们迎来了许多新特性和改进,其中一个重要的改进就是加入了WeakReference类型。对于一部分PHP开发者来说,WeakReference类型可能还是一个不太熟悉的概念...

    2023年5月18日
    00
  • 附件三:DedeCMS二次开发——代码篇

    二、代码篇 2.1、common.func.php 公用函数 获得当前的脚本网址 function GetCurUrl() 返回格林威治标准时间 function MyDate($format='Y-m-d H:i:s',$timest=0) 把全角数字转为半角 function GetAlabNum($fnum) ...

    2017年5月5日
    0277
  • PHP入门指南:DOM扩展。

    PHP是最常用的网络编程语言之一,其使用广泛,而且应用领域非常广泛。其中,DOM扩展是PHP中用来处理XML文档的一种常用方法。本文将介绍初学者如何使用PHP的DOM扩展来操作XML文档。什么是DOM扩展?DOM(Document O...

    2023年5月28日
    01

联系我们

QQ:951076433

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