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

yipeiwu_com5年前服务器

本文实例讲述了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程序设计有所帮助。

相关文章

python实现可将字符转换成大写的tcp服务器实例

本文实例讲述了python实现可将字符转换成大写的tcp服务器。分享给大家供大家参考。具体分析如下: 下面的python代码执行后通过tcp监控8081端口,用于将用户发送的请求字符串转...

在 Django/Flask 开发服务器上使用 HTTPS

使用 Django 或 Flask 这种框架开发 web app 的时候一般都会用内建服务器开发和调试程序,等程序完成后再移交到生产环境部署。问题是这些内建服务器通常都不支持 HTTPS...

python获取服务器响应cookie的实例

总结 调试网站获取cookies时请查看,r.header和r.request.header这两个属性,因为cookie说不准出现在他们俩谁里面。 先贴一个代码 import re...

paramiko模块安装和使用(远程登录服务器)

一:简介 由paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接。 由于使用的是python这样的能够跨平台运行的语言,所以所...

用Zend Studio+PHPnow+Zend Debugger搭建PHP服务器调试环境步骤

用Zend Studio+PHPnow+Zend Debugger搭建PHP服务器调试环境步骤

本人主要是做ASP.NET开发的,但有时候也会接触到PHP,而且我认为PHP有很多源码值得学习,我们不是学习PHP代码的写法,而是学习源码的实现思路,或者免强叫为算法。 作为一名非专业的...