服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - PHP实现原比例生成缩略图的方法

PHP实现原比例生成缩略图的方法

2020-12-19 16:50PHP教程网 PHP教程

这篇文章主要介绍了PHP实现原比例生成缩略图的方法,涉及PHP的图形操作及数学运算相关技巧,非常简单实用,需要的朋友可以参考下

本文实例讲述了PHP实现原比例生成缩略图的方法。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
$image = "jiequ.jpg"; // 原图
$imgstream = file_get_contents($image);
$im = imagecreatefromstring($imgstream);
$x = imagesx($im);//获取图片的宽
$y = imagesy($im);//获取图片的高
// 缩略后的大小
$xx = 140;
$yy = 200;
if($x>$y){
//图片宽大于高
  $sx = abs(($y-$x)/2);
  $sy = 0;
  $thumbw = $y;
  $thumbh = $y;
} else {
//图片高大于等于宽
  $sy = abs(($x-$y)/2.5);
  $sx = 0;
  $thumbw = $x;
  $thumbh = $x;
 }
if(function_exists("imagecreatetruecolor")) {
 $dim = imagecreatetruecolor($yy, $xx); // 创建目标图gd2
} else {
 $dim = imagecreate($yy, $xx); // 创建目标图gd1
}
imageCopyreSampled ($dim,$im,0,0,$sx,$sy,$yy,$xx,$thumbw,$thumbh);
header ("Content-type: image/jpeg");
imagejpeg ($dim, false, 100);
?>

 

希望本文所述对大家PHP程序设计有所帮助。

延伸 · 阅读

精彩推荐