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哪个公司的,PHP哪个版本比较好。

    PHP是一种广泛使用的开源通用脚本语言,尤其适合于网络开发并可嵌入HTML,它在Web开发中占有非常重要的地位,许多网站都在使用PHP进行开发,哪个公司的PHP版本比较好呢? 我们需要明确一点:PHP的版本并不是由某个…

    2024年6月16日
    02
  • Redis中的布隆过滤器和PHP的使用方法。

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

    2023年5月21日
    00
  • PHP中的负载均衡器。

    负载均衡器(Load Balancer)是一种重要的技术,它可以在多个服务器之间分配请求,确保每个服务器都不会过载,并且尽可能提高系统可用性和性能。PHP是一种在Web应用程序开发中常用的编程语言,而在PHP中,使用负载…

    2023年5月28日
    00
  • PHP8.0中的日志库:Monolog

    随着互联网技术的不断发展和进步,越来越多的应用程序需要处理大量的数据和请求。为了确保应用程序能够正常运行和及时发现问题,记录日志以便排查问题变得尤为关键。日志是一种用于追踪和记录系统运行情况的信息记…

    2023年5月18日
    04
  • PHP使用curl库发送HTTP请求。

    在Web开发中,发送HTTP请求是一项非常重要的任务。无论是通过API获取数据,还是与第三方服务进行通信,都需要使用HTTP请求来进行数据传输。在PHP中,可以使用curl库来发送HTTP请求,本文就来详细介绍curl库的使用方…

    2023年5月23日
    01
  • 重蔚php学习第二十八天——引用文件(载入文件)

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

    2017年10月4日 PHP自学教程
    0410
  • php的数组类型array

    标识一系列数据的“有序排列”的集合体。 php中,数组的下标可以使用整数或字符串。 数字下标常说“索引号”, 字符串下标常说“键名”。 实际上,在php的报错系统中,都叫做“index”,或offset 数组中还可以存储数组,就…

    2018年3月17日
    0238
  • 如何在PHP-Slim框架中使用CORS跨域请求。

    在Web开发中,跨域请求是一个常见的问题。这是因为浏览器对于不同域名之间的请求有严格的限制。例如,网站A的前端代码无法直接向网站B的API发送请求,除非网站B允许跨域请求。为了解决这个问题,出现了CORS(跨域资…

    2023年6月3日
    04

联系我们

QQ:951076433

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