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

相关文章

Python实现简易版的Web服务器(推荐)

Python实现简易版的Web服务器(推荐)

下面给大家介绍python实现简易版的web服务器,具体内容详情大家通过本文学习吧! 1、请自行了解HTTP协议 /post/133883.htm(点击跳转) 2、创建Socket服务...

基于python3监控服务器状态进行邮件报警

基于python3监控服务器状态进行邮件报警

在正式的生产环境中,我们常常会需要监控服务器的状态,以保证公司整个业务的正常运转,常常我们会用到像nagios、zabbix这类工具进行实时监控,那么用python我们怎么进行监控呢?这...

Tornado Web Server框架编写简易Python服务器

我们都知道在Web开发中,都需要服务器,比如Java Web开发的Tomcat,WebLogic,WebSphere,现在来看利用Tornado Web Server框架如何写一个简易的...

django自带调试服务器的使用详解

django自带调试服务器的使用详解

开启服务器 在终端(虚拟环境)下输入: python manage.py runserver 就可以开启服务器 输入后,注意随后弹出的服务器地址。 点击后就会跳转至调试服务器。...

在DigitalOcean的服务器上部署flaskblog应用

在DigitalOcean上部署了flaskblog,项目虽小,部署中也学到了很多东西。 操作系统选择的是Ubuntu14.04,原因就是平时自己主要使用这个版本,顺手而已,所以你自己...