php实现在服务器端调整图片大小的方法
作者:小卒过河 时间:2024-05-13 09:25:46
本文实例讲述了php实现在服务器端调整图片大小的方法。分享给大家供大家参考。具体分析如下:
在服务器端完成图片大小的调整,会比在浏览器的处理有很多的好处。
本文介绍了PHP如何在服务器端调整图片大小。
代码包括两部分:
① imageResizer() is used to process the image
② loadimage() inserts the image url in a simpler format
<?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;
}
?>
用法:
//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程序设计有所帮助。
标签:php,服务器,图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
javascript获取select值的方法完整实例
2024-04-22 12:49:18
![](https://img.aspxhome.com/file/2023/0/136090_0s.gif)
在ASP处理程序时,进度显示
2008-08-04 13:27:00
详细分析Python collections工具库
2022-06-28 01:18:57
Centos 6.4源码安装mysql-5.6.28.tar.gz教程
2024-01-25 09:39:58
如何查看连接MYSQL数据库的IP信息
2024-01-16 17:26:51
pydantic进阶用法示例详解
2022-01-31 07:00:03
使用单通道实现半透明效果
2009-12-12 17:40:00
![](https://img.aspxhome.com/file/UploadPic/200912/12/pic_0-96s.png)
Vuex实现简单购物车
2024-05-08 10:43:45
![](https://img.aspxhome.com/file/2023/5/127405_0s.jpg)
SQL Server时间戳功能与用法详解
2024-01-27 23:14:20
Python+Opencv识别两张相似图片
2022-11-07 09:12:55
![](https://img.aspxhome.com/file/2023/0/97070_0s.jpg)
exe反编译为.py文件的方法
2022-06-10 16:18:16
![](https://img.aspxhome.com/file/2023/4/117574_0s.jpg)
python使用pyecharts库画地图数据可视化的实现
2023-04-18 09:44:25
![](https://img.aspxhome.com/file/2023/0/70890_0s.jpg)
10个python3常用排序算法详细说明与实例(快速排序,冒泡排序,桶排序,基数排序,堆排序,希尔排序,归并排序,计数排序)
2021-05-21 05:28:13
![](https://img.aspxhome.com/file/2023/8/88978_0s.png)
python列表倒序的几种方法(切片、reverse()、reversed())
2022-01-28 02:46:52
CentOS6.9下mysql 5.7.17安装配置方法图文教程
2024-01-23 12:26:03
![](https://img.aspxhome.com/file/2023/4/107914_0s.png)
用JS实现网页元素阴影效果的研究总结
2024-05-02 16:12:02
ASP trim,ltrim,rtrim 去前后空格 函数
2011-03-03 10:39:00
SQLServer 优化SQL语句 in 和not in的替代方案
2024-01-18 00:31:02
解决PyCharm同目录下导入模块会报错的问题
2023-06-12 22:39:55
以tensorflow库为例讲解Pycharm中如何更新第三方库
2022-10-18 09:53:27
![](https://img.aspxhome.com/file/2023/7/120917_0s.png)