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程序设计有所帮助。

相关文章

PHP 服务器配置(使用Apache及IIS两种方法)

一、使用Apache≡ PHP 5.2.5 的安装 ≡1、到其官方站点下载 php-5.2.5-Win32.zip 并解压(据说:不要下载及使用它的Installer,这种方式虽然很自动...

Python实现的ftp服务器功能详解【附源码下载】

Python实现的ftp服务器功能详解【附源码下载】

本文实例讲述了Python实现的ftp服务器功能。分享给大家供大家参考,具体如下: python 具备强大的网络编程功能,而且代码简介,用简单的代码,就能实现一个功能强大的FTP 服务器...

PHP实现微信图片上传到服务器的方法示例

本文实例讲述了PHP实现微信图片上传到服务器的方法。分享给大家供大家参考,具体如下: $pic_img=trim( $postObj->PicUrl); if($type=="...

python快速建立超简单的web服务器的实现方法

作为临时测试用python命令来搭建web测试是最好不过的选择了; CD切换到当前目录只需要一句python命令就迅速搭建好了简单的web服务器,python linux自带又无需额外配...

使用Python来编写HTTP服务器的超级指南

使用Python来编写HTTP服务器的超级指南

首先,到底什么是网络服务器? 简而言之,它是在物理服务器上搭建的一个网络连接服务器(networking server),永久地等待客户端发送请求。当服务器收到请求之后,它会生成响应...