php实现在服务器端调整图片大小的方法


Posted in PHP onJune 16, 2015

本文实例讲述了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 相关文章推荐
让你的网站首页自动选择语言转跳
Dec 06 PHP
php下过滤HTML代码的函数
Dec 10 PHP
JS 网站性能优化笔记
May 24 PHP
PHP实现把数字ID转字母ID
Aug 12 PHP
php取整函数ceil,floo,round的用法及介绍
Aug 31 PHP
php开启openssl的方法
May 15 PHP
PHP+MySQL之Insert Into数据插入用法分析
Sep 27 PHP
PHP实现Google plus的好友拖拽分组效果
Oct 21 PHP
PHP文件与目录操作示例
Dec 24 PHP
PHP实现上传多图即时显示与即时删除的方法
May 09 PHP
yii2中LinkPager增加总页数和总记录数的实例
Aug 28 PHP
PHP addcslashes()函数讲解
Feb 03 PHP
PHP正则验证Email的方法
Jun 15 #PHP
PHP实现通过正则表达式替换回调的内容标签
Jun 15 #PHP
PHP检测用户语言的方法
Jun 15 #PHP
php实现求相对时间函数
Jun 15 #PHP
php数组随机排序实现方法
Jun 13 #PHP
隐性调用php程序的方法
Jun 13 #PHP
PHP获取数组的键与值方法小结
Jun 13 #PHP
You might like
NOT NULL 和NULL
2007/01/15 PHP
PHP读取数据库并按照中文名称进行排序实现代码
2013/01/29 PHP
利用PHP获取网站访客的所在地位置
2017/01/18 PHP
PHP getName()函数讲解
2019/02/03 PHP
php写入mysql中文乱码的实例解决方法
2019/09/17 PHP
解决php用mysql方式连接数据库出现Deprecated报错问题
2019/12/25 PHP
面向对象的javascript(笔记)
2009/10/06 Javascript
jquery设置按钮停顿3秒不可用
2014/03/07 Javascript
jquery+easeing实现仿flash的载入动画
2015/03/10 Javascript
JavaScript实现为事件句柄绑定监听函数的方法分析
2017/11/14 Javascript
不使用 JS 匿名函数理由
2017/11/17 Javascript
nodejs express配置自签名https服务器的方法
2018/05/22 NodeJs
JavaScript实现与使用发布/订阅模式详解
2019/01/19 Javascript
ant-design表单处理和常用方法及自定义验证操作
2020/10/27 Javascript
解决Vue watch里调用方法的坑
2020/11/07 Javascript
[46:55]完美世界DOTA2联赛决赛 FTD vs Phoenix 第三场 11.08
2020/11/11 DOTA
Python调用C语言开发的共享库方法实例
2015/03/18 Python
Python迭代器与生成器用法实例分析
2018/07/09 Python
python flask框架实现重定向功能示例
2019/07/02 Python
使用python代码进行身份证号校验的实现示例
2019/11/21 Python
使用python实现希尔、计数、基数基础排序的代码
2019/12/25 Python
python scrapy重复执行实现代码详解
2019/12/28 Python
Python 实现递归法解决迷宫问题的示例代码
2020/01/12 Python
推荐WEB开发者最佳HTML5和CSS3代码生成器
2015/11/24 HTML / CSS
方太官方网上商城:销售方太抽油烟机、燃气灶、消毒柜等
2017/01/17 全球购物
平面设计自荐信
2013/10/07 职场文书
初二政治教学反思
2014/01/12 职场文书
建筑总经理岗位职责
2014/02/02 职场文书
《在大海中永生》教学反思
2014/02/24 职场文书
学习保证书范文
2014/04/30 职场文书
国际商贸专业自荐信
2014/06/09 职场文书
2014年党的群众路线活动个人整改措施
2014/10/28 职场文书
学校群众路线专项整治方案
2014/10/31 职场文书
三严三实·严以修身心得体会
2016/01/15 职场文书
2016创先争优活动党员公开承诺书
2016/03/24 职场文书
《刺客之王:C罗全景传记》:时代从来不会亏待手艺人
2019/11/28 职场文书