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和Docker构建可部署的应用。

    随着互联网的快速发展,越来越多的应用程序需要基于云服务器进行部署,而使用 Docker 容器技术对于实现这一目标来说是一个很好的选择。在本篇文章中,我们将介绍如何使用 PHP 和 Docker 构建可部署的应用程序,从而…

    2023年5月30日
    04
  • PHP入门指南:循环语句。

    PHP是一种广泛使用的编程语言,用于Web开发及其他许多领域。掌握PHP中的循环语句是成为优秀开发者的关键之一。在本文中,我们将讨论PHP的循环语句,包括for循环语句、while循环语句、和do-while循环语句。for循环语…

    2023年5月22日
    06
  • 如何使用PHP开发商城的预约购买功能。

    随着电商的兴起,越来越多的商家选择在网上开设自己的商城。然而,随着竞争的加剧,如何提供更好的购物体验已经成为了商家们面临的一项重要挑战。其中,预约购买功能是目前越来越受到欢迎的一种功能。那么,如何使…

    2023年5月28日
    07
  • 几种用PHP写99乘法表的方式

    首先按照规矩,还是先废话一番,对于刚学PHP的新手来讲,用php写九九乘法表无疑是非常经典的一道练习题。但不要小看这道练习题,它对于逻辑的考验还是相当到位的。也许有人会觉得,九九乘法表有什么难的,我两分钟…

    2022年6月21日
    0140
  • PHP与数据库性能调优的集成。

    随着互联网技术的飞速发展,数据库是网站开发中必不可少的一部分。而PHP是目前最常用的服务器端语言之一,它的高效性能和便捷的开发方式已经被广泛认可。但是,即使是最流行的技术也有其局限性,数据库性能问题是PH…

    2023年5月21日
    01
  • 小编分享教程宝塔面板上的PHP优化攻略。

    宝塔面板PHP优化攻略,提供有效提升网站性能的方法和技巧。 教程宝塔面板上的PHP优化攻略 在网站开发过程中,PHP是一种广泛使用的服务器端脚本语言,有时候我们可能会遇到PHP性能不佳的问题,这时候就需要对PHP进行…

    2024年7月17日
    03
  • PHP实现邮件异步发送的技术。

    随着电子邮件在我们日常生活和工作中的广泛使用,许多网站和应用程序都需要发送大量的电子邮件。然而,在大量邮件发送的情况下,同步发送可导致网站的性能下降,因为PHP必须等待邮件发送完成才能继续运行。为了避免…

    2023年5月23日
    05
  • PHP入门指南:PHP和Prometheus。

    PHP作为一种开源的脚本语言,已经有20多年的历史。它主要被用于Web开发,特别是用于服务端的脚本。PHP的使用非常广泛,它被用于构建许多大型的Web应用程序和网站。Prometheus则是一种开源的监控系统和时间序列数据…

    2023年5月22日
    07

联系我们

QQ:951076433

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