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 相关文章推荐
探讨:web上存漏洞及原理分析、防范方法
Jun 29 PHP
mcrypt启用 加密以及解密过程详细解析
Aug 07 PHP
php5.5中类级别的常量使用介绍
Oct 02 PHP
php防止SQL注入详解及防范
Nov 12 PHP
PHP5.3以上版本安装ZendOptimizer扩展
Mar 27 PHP
PHP使用PHPExcel删除Excel单元格指定列的方法
Jul 06 PHP
php is_writable判断文件是否可写实例代码
Oct 13 PHP
php常用数组函数实例小结
Dec 29 PHP
PHP unlink与rmdir删除目录及目录下所有文件实例代码
Feb 07 PHP
PHP 爬取网页的主要方法
Jul 13 PHP
laravel5.1框架model类查询的实现方法
Oct 08 PHP
Laravel如何实现适合Api的异常处理响应格式
Jun 14 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
php zend 相对路径问题
2009/01/12 PHP
php+jquery编码方面的一些心得(utf-8 gb2312)
2010/10/12 PHP
weiphp微信公众平台授权设置
2016/01/04 PHP
php 替换文章中的图片路径,下载图片到本地服务器的方法
2018/02/06 PHP
关于JavaScript定义类和对象的几种方式
2010/11/09 Javascript
各情景下元素宽高的获取实现代码
2011/09/13 Javascript
JS分页控件 可用于无刷新分页
2013/07/23 Javascript
javascript递归回溯法解八皇后问题
2015/04/22 Javascript
jQuery平滑旋转幻灯片特效代码分享
2015/09/07 Javascript
jQuery简介_动力节点Java学院整理
2017/07/04 jQuery
浅谈JavaScript中的属性:如何遍历属性
2017/09/14 Javascript
vue中使用codemirror的实例详解
2018/11/01 Javascript
使用 js 简单的实现 bind、call 、aplly代码实例
2019/09/07 Javascript
vue下axios拦截器token刷新机制的实例代码
2020/01/17 Javascript
openLayer4实现动态改变标注图标
2020/08/17 Javascript
Vue组件简易模拟实现购物车
2020/12/21 Vue.js
Python高级应用实例对比:高效计算大文件中的最长行的长度
2014/06/08 Python
Python实现的百度站长自动URL提交小工具
2014/06/27 Python
Python函数中的函数(闭包)用法实例
2016/03/15 Python
Python读取MRI并显示为灰度图像实例代码
2018/01/03 Python
Python中使用pypdf2合并、分割、加密pdf文件的代码详解
2019/05/21 Python
Python学习笔记之列表和成员运算符及列表相关方法详解
2019/08/22 Python
Python3视频转字符动画的实例代码
2019/08/29 Python
Django model.py表单设置默认值允许为空的操作
2020/05/19 Python
英国家庭家具、照明和花园家具购物网站:Furniture123
2018/12/31 全球购物
高分子材料与工程专业个人求职信
2013/12/15 职场文书
小学教师师德感言
2014/02/10 职场文书
党员公开承诺书内容
2014/05/20 职场文书
绘画专业自荐信
2014/07/04 职场文书
关于感恩的演讲稿400字
2014/08/26 职场文书
党的群众路线教育实践活动批评与自我批评发言稿
2014/10/16 职场文书
汽车质检员岗位职责
2015/04/08 职场文书
诚信考试承诺书范文
2015/04/29 职场文书
刑事法律意见书
2015/06/04 职场文书
元宵节晚会主持词
2015/07/01 职场文书
《追风筝的人》:人心中的成见是座大山,但请不忘初心
2019/11/15 职场文书