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

yipeiwu_com6年前服务器

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

相关文章

windows服务器中检测PHP SSL是否开启以及开启SSL的方法

一、检测服务器是否开启了SSL 复制代码 代码如下:<?phpphpinfo();?>检查页面的openssl栏目,如果该栏目的OpenSSL support的值为enabl...

python实现对服务器脚本敏感信息的加密解密功能

背景 在实际项目实施中,会编写很多在服务器执行的作业脚本。程序中凡是涉及到数据库链接、操作系统用户链接、IP地址、主机名称的内容都是敏感信息。在纯内网系统中往因为开发时间紧迫,往往都直接...

PHP实现将HTML5中Canvas图像保存到服务器的方法

本文实例讲述了PHP实现将HTML5中Canvas图像保存到服务器的方法。分享给大家供大家参考。具体实现方法如下: 一、问题: 在几年前HTML5还没有流行的时候,我们的项目经理曾经向我...

python探索之BaseHTTPServer-实现Web服务器介绍

在Python探索之SocketServer详解中我们介绍了Python标准库中的SocketServer模块,了解了要实现网络通信服务,就要构建一个服务器类和请求处理类。同时,该模块还...

python下paramiko模块实现ssh连接登录Linux服务器

本文实例讲述了python下paramiko模块实现ssh连接登录Linux服务器的方法。分享给大家供大家参考。具体分析如下: python下有个paramiko模块,这个模块可以实现s...