本文实例讲述了php实现在服务器端调整图片大小的方法。分享给大家供大家参考。具体分析如下:
在服务器端完成图片大小的调整,会比在浏览器的处理有很多的好处。
本文介绍了PHP如何在服务器端调整图片大小。
代码包括两部分:
① imageResizer() is used to process the image
② loadimage() inserts the image url in a simpler format
?
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
function imageResizer( $url , $width , $height ) {
header( 'Content-type: image/jpeg' );
list( $width_orig , $height_orig ) = getimagesize ( $url );
$ratio_orig = $width_orig / $height_orig ;
if ( $width / $height > $ratio_orig ) {
$width = $height * $ratio_orig ;
} else {
$height = $width / $ratio_orig ;
}
// This resamples the image
$image_p = imagecreatetruecolor( $width , $height );
$image = imagecreatefromjpeg( $url );
imagecopyresampled( $image_p , $image , 0, 0, 0, 0, $width , $height , $width_orig , $height_orig );
// Output the image
imagejpeg( $image_p , null, 100);
}
//works with both POST and GET
$method = $_SERVER [ 'REQUEST_METHOD' ];
if ( $method == 'GET' ) {
imageResize( $_GET [ 'url' ], $_GET [ 'w' ], $_GET [ 'h' ]);
} elseif ( $method == 'POST' ) {
imageResize( $_POST [ 'url' ], $_POST [ 'w' ], $_POST [ 'h' ]);
}
// makes the process simpler
function loadImage( $url , $width , $height ){
echo 'image.php?url=' , urlencode( $url ) ,
'&w=' , $width ,
'&h=' , $height ;
}
?>
|
用法:
?
1
2
3
|
//Above code would be in a file called image.php.
//Images would be displayed like this:
<img src= "<?php loadImage('image.jpg', 50, 50) ?>" alt= "" />
|
希望本文所述对大家的php程序设计有所帮助。
相关文章
猜你喜欢
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 41
-
2025-05-25 41
-
2025-05-25 35
-
2025-05-27 62
-
2025-05-29 53
热门评论