在我们的工作的项目中,有时候我们需要显示规定尺寸的图片,虽然可以通过css来控制显示大小。但是如果图片过大,会造成加载的延迟,影响网站整体性能。因此,我们需要一个服务器来帮助我们进行图片的裁剪。流程大致是,首先我们传给服务器原图像和裁剪的尺寸,然后服务器进行裁剪,生成对应的裁剪图片,下次我们再访问相同图像和相同的裁剪尺寸的时候,我们就不需要裁剪,直接进行图片的访问就行。

Talk is cheap, show me the code.

// ①构建图片请求地址比如  http://xxx.com/resize.php?site=www&width=300&height=200&mode=2&path=uploadfile/helloworld.png

// ②配置nginx重写规则  rewrite /s/(.*)/(\d+)x(\d+)-(\d)/(.*) /s/resize.php?site=$1&width=$2&height=$3&mode=$4&path=$5 last;

//③进行裁剪图片的处理

$path = trim($_GET['path']);

$mode = intval($_GET['mode']);

$site = trim($_GET['site']);

$width = intval($_GET['width']);

$height = intval($_GET['height']);

$site_list = array('crop' => '.');

$orig_dir = dirname(__FILE__);

if (!array_key_exists($site, $site_list)) {

header('HTTP/1.1 400 Bad Request');

exit();

}

if ($mode > 3 || $mode < 0) {

header('HTTP/1.1 400 Bad Request');

exit();

}

$orig_file = $site_list[$site] . $path;

if (!file_exists($orig_file)) {

header('HTTP/1.1 404 Not Found');

exit();

}

$file_ext = '.' . pathinfo($path, PATHINFO_EXTENSION);

$file_name = basename($path, $file_ext);

$save_path = "{$orig_dir}/{$site}/{$width}x{$height}-{$mode}{$path}";

$save_dir = dirname($save_path);

if (!file_exists($save_dir)) {

wpx_mkdir($save_dir);

}

$target_width = $width;

$target_height = $height;

$save_image = $save_dir . '/' . $file_name . '.jpg';

if (file_exists($save_image)) {

header('Content-Type: image/jpeg');

header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');

echo file_get_contents($save_image);

}

imagecropper2($orig_file, $target_width, $target_height, $save_image);

die;

//原图像对应缩放裁剪,会拉伸图片

function imagecropper2($source_path, $width, $height, $save_image)

{

//获取原图像$filename的宽度$width_orig和高度$height_orig

$info =  getimagesize($source_path);

$width_orig = $info[0];

$height_orig = $info[1];

$mime = $info['mime'];

//根据参数$width和$height值,换算出等比例缩放的高度和宽度

if ($width && ($width_orig

$width = ($height/$height_orig)*$width_orig;

}else{

$height = ($width / $width_orig)*$height_orig;

}

//将原图缩放到这个新创建的图片资源中

$image_p = imagecreatetruecolor($width, $height);

//获取原图的图像资源

if($mime=='image/jpeg'){

$image = imagecreatefromjpeg($source_path);

}elseif($mime=='image/png'){

$image = imagecreatefrompng($source_path);

}elseif($mime=='image/gif'){

$image = imagecreatefromgif($source_path);

}

//使用imagecopyresampled()函数进行缩放设置

imagecopyresampled($image_p,$image,0,0,0,0,$width,$height,$width_orig,$height_orig);

//将缩放后的图片$image_p保存,100(质量最佳,文件最大)

if($mime=='image/jpeg'){

imagejpeg($image_p,$save_image);

header('Content-Type: image/jpeg');

imagejpeg($image_p);

}elseif($mime=='image/png'){

imagepng($image_p,$save_image);

header('Content-Type: image/jpeg');

imagepng($image_p);

}else{

imagegif($image_p,$save_image);

header('Content-Type: image/jpeg');

imagegif($image_p);

}

}

//进行比例保存裁剪,会丢失图像部分像素

function imagecropper($source_path, $target_width, $target_height, $save_image)

{

$source_info = getimagesize($source_path);

$source_width = $source_info[0];

$source_height = $source_info[1];

$source_mime = $source_info['mime'];

$source_ratio = $source_height / $source_width;

$target_ratio = $target_height / $target_width;

// 源图过高

if ($source_ratio > $target_ratio) {

$cropped_width = $source_width;

$cropped_height = $source_width * $target_ratio;

$source_x = 0;

$source_y = ($source_height - $cropped_height) / 2;

}

// 源图过宽

elseif ($source_ratio < $target_ratio) {

$cropped_width = $source_height / $target_ratio;

$cropped_height = $source_height;

$source_x = ($source_width - $cropped_width) / 2;

$source_y = 0;

}

// 源图适中

else {

$cropped_width = $source_width;

$cropped_height = $source_height;

$source_x = 0;

$source_y = 0;

}

switch ($source_mime) {

case 'image/gif':

$source_image = imagecreatefromgif($source_path);

break;

case 'image/jpeg':

$source_image = imagecreatefromjpeg($source_path);

break;

case 'image/png':

$source_image = imagecreatefrompng($source_path);

break;

default:

return false;

break;

}

$target_image = imagecreatetruecolor($target_width, $target_height);

$cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);

// 裁剪

$bool = imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);

// 缩放

$bool = imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);

imagejpeg($target_image, $save_image);

header('Content-Type: image/jpeg');

imagejpeg($target_image);

imagedestroy($source_image);

imagedestroy($target_image);

imagedestroy($cropped_image);

}

// 循环生成目录

function wpx_mkdir($dir, $mode = 0777)

{

if (is_dir($dir) || @mkdir($dir, $mode)) {

return true;

}

if (!wpx_mkdir(dirname($dir), $mode)) {

return false;

}

return @mkdir($dir, $mode);

}

通过上面的处理,我们就将图片按照我们设置的尺寸进行了裁剪。我们还可以定期对裁剪图片进行清理,这样就不需要占用太多服务器空间。只有经常访问的图片才会一直保存。

php 图片服务器搭建,php图像裁剪服务器搭建相关推荐

  1. base64图裁剪 php_世界上最好的编程语言PHP图层裁剪服务搭建详解

    IT技术研习社,专注互联网技术研究与分享,喜欢的朋友可以点击[关注]:把经验传递给有梦想的人: PHP图像裁剪服务搭建 概述 每一个做过WEB程序开发的程序员(比如,博客.电商),应该都有这样的体验, ...

  2. 【Android RTMP】NV21 图像旋转处理 ( 快速搭建 RTMP 服务器 Shell 脚本 | 创建 RTMP 服务器镜像 | 浏览器观看直播 | 前置 / 后置摄像头图像旋转效果展示 )

    文章目录 安卓直播推流专栏博客总结 一. 编写快速搭建 RTMP 服务器 Shell 脚本 二. RTMP 快速搭建方法 三.创建阿里云 RTMP 服务器镜像 四.浏览器查看直播内容 五.前置 / 后 ...

  3. 写给大忙人看的 - Java中图片压缩上传至MinIO服务器(4)

    之前文章已经介绍了 MinIO 的环境搭建,已经对文件的上传下载方法,本篇文章一起与大家来学习图片压缩上传的方法 1.背景 最近客户总抱怨 APP 中图片显示较慢, 升级服务器带宽又没有多的预算.查看 ...

  4. vue图片裁剪组件_使用Vue-Rx的Vue.js图像裁剪组件

    vue图片裁剪组件 Vuejs夹 (vuejs-clipper) Vue.js image clipping components using Vue-Rx. 使用Vue-Rx的Vue.js图像裁剪组 ...

  5. WordPress上传图片提示:服务器无法处理图像

    介绍 WordPress上传图片提示服务器无法处理图像,如果服务器繁忙或没有足够的资源来完成任务,就会发生这种情况.上传较小的图像可能会有所帮助.建议的最大尺寸为2560像素 教程 把下方代码复制到你 ...

  6. 一、node.js搭建最简单的服务器

    node.js搭建最简单的服务器 代码演示: // 1. 加载http核心模块 var http = require('http')// 2. 使用http.createServer()方法创建一个W ...

  7. Ubuntu Linux系统下搭建自己的Web服务器

    经常被拿来当服务器的有CentOS.Ubuntu......考虑到自己只有Ubuntu的镜像并且只是自己做一些简单的测试使用,所以选择的版本是Ubuntu 14.04 LTS.在这个平台上搭建自己的W ...

  8. 搭建深度学习后台服务器

    本篇文章的原创为国外的一篇文章(一个可扩展的Keras深度学习REST API),链接为: https://www.pyimagesearch.com/2018/01/29/scalable-kera ...

  9. 群晖nas中使用registry搭建docker镜像私人服务器以及设置群晖远程docker服务

    群晖nas中使用registry搭建docker镜像私人服务器以及设置群晖远程docker服务 折腾群晖服务器很有快感,一是因为自家群晖ds216+低功耗,24小时开机也不觉得费电,二是因为群晖系统也 ...

最新文章

  1. Notification 使用详解
  2. englis translate,word
  3. Java黑皮书课后题第7章:7.10(找出最小元素的下标)使用下面的方法头编写一个方法,求出一个整数数组中的最小元素下标。编写测试程序,提示用户输入10个数字,调用这个方法返回最小值的下标(多个则最小
  4. 毕业论文计算机附录模板,毕业论文格式是什么,附录又是什么?
  5. opencv30-图像矩
  6. python 迭代器协议_浅谈Python中的生成器和迭代器
  7. easyui combogrid组件 setValue 影响
  8. Python 获取LOL所有英雄的传说
  9. 计算机毕业设计Java宠物收养管理系统(源码+系统+mysql数据库+lw文档)
  10. python就业前景不好_Python就业发展前景分析
  11. h61 nvme硬盘_一个不够用两个刚刚好ORICO双盘位硬盘盒_移动硬盘盒
  12. 1226. The Dining Philosophers (Leetcode 1226)
  13. 美团再次冲击网约车市场,滴滴或将加快上市进程
  14. 创建制作SDK的静态库工程
  15. 2014中国机器视觉行业十大知名品牌
  16. 下拉列表(select标签)
  17. Python的import this 惊喜彩蛋:Python之禅(The Zen of Python)
  18. C++设计模式——桥接模式(高屋建瓴)
  19. 1. 【Part2】 Contour Detection and Hierarchical Image Segmentation【轮廓检测图像分割】
  20. JAVA 实现《五子棋》游戏|CSDN创作打卡

热门文章

  1. Java基础学习总结(84)——Java面向对象六大原则和设计模式
  2. php a标签里 href的mysql_php,正则表达式_php提取html中指定div下a标签的text和href问题,php,正则表达式 - phpStudy...
  3. mysql 运维常见操作
  4. fn有toString方法,string没有toFunction方法,自定义一个toFunction方法
  5. PHP获取一段时间内的每个周几, 每月几号, 遇到特殊日子就往后延
  6. Linux 用户线程数与文件句柄树调整(nproc与nofile的问题)
  7. java集合类中的迭代器
  8. 用户名和密码都正确,无法直接登陆虚拟机上的linux
  9. html标签整合和css框架处理
  10. 微软宣布免费 Web 版 Office 2010发布日期