search
HomeBackend DevelopmentPHP TutorialHow header() outputs image cache usage example
How header() outputs image cache usage exampleJun 24, 2017 pm 01:29 PM
headerpicturehowcacheoutput

When we generate the verification code, we need to directly input the image, usually using header("Content-type: image/jpeg"); to achieve this. Here is a brief introduction.

In many developments, we try to use header("Content-type: image/jpeg"); to output pictures, and try to use some php image processing technology to allow the output Pictures are more intelligent and dynamic. But we often encounter new problems. Unless you specify different URL structures and use server technology to cache images, it is very likely that these output images will consume a lot of traffic. How to cache them and call the cache the next time the user visits? (The premise is that you want this image to remain unchanged)

// put this above any php image generation code:
session_start(); 
header("Cache-Control: private, max-age=10800, pre-check=10800");
header("Pragma: private");
header("Expires: " . date(DATE_RFC822,strtotime(" 2 day")));

Add the above code above header("Content-type: image/jpeg");, which will specify the cache time of the current page (two days) and use this cache time node on the next visit.
Next, determine whether there is already a cache. If so, use the cache.

Situation 1: If the browser already caches the current page, then use it directly.

// the browser will send a $_SERVER['HTTP_IF_MODIFIED_SINCE'] if it has a cached copy 
if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])){
  // if the browser has a cached version of this image, send 304
  header('Last-Modified: '.$_SERVER['HTTP_IF_MODIFIED_SINCE'],true,304);
  exit;
}

Scenario 2: The browser caches the current page. Although we have updated some image information, the source image itself has not changed, and we want to use the previous cache, so we also use the cache.

$img = "some_image.png";
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && (strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == 
filemtime($img))) {
  // send the last mod time of the file back
  header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($img)).' GMT',true, 304);
  exit;
}

Of course, there are some special situations that we must consider, but the above code can basically lead our thinking. By the way, remember to put them all above header("Content-type: image/jpeg").
Let’s take a look at an example

<?php
//调整图片大小
/**
 *图片按比例调整大小的原理:
 *1、比较原图大小是否小于等于目标大小,如果是则直接采用原图宽高
 *2、如果原图大小超过目标大小,则对比原图宽高大小
 *3、如:宽>高,则宽=目标宽, 高=目标宽的比例 * 原高
 *4、如:高>宽,则高=目标高,宽=目标高的比例 * 原宽   
**/
 
$image = "test.jpg";
$max_width = 200;
$max_height = 200;
 
$size = getimagesize($image);   //得到图像的大小
$width = $size[0];             
$height = $size[1];
 
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
 
if (($width <= $max_width) && ($height <= $max_height))
{
    $tn_width = $width;
    $tn_height = $height;
}
elseif (($x_ratio * $height) < $max_height)
{
    $tn_height = ceil($x_ratio * $height);
    $tn_width = $max_width;
}
else
{
    $tn_width = ceil($y_ratio * $width);
    $tn_height = $max_height;
}
 
$src = imagecreatefromjpeg($image);
$dst = imagecreatetruecolor($tn_width, $tn_height); //新建一个真彩色图像
imagecopyresampled($dst, $src, 0, 0, 0, 0,$tn_width, $tn_height, $width, $height);        //重采样拷贝部分图像并调整大小
header(&#39;Content-Type: image/jpeg&#39;);
imagejpeg($dst,null,100);
imagedestroy($src);
imagedestroy($dst);
?>


The above is the detailed content of How header() outputs image cache usage example. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
如何修复 Outlook 中缺少的 Microsoft Teams 插件如何修复 Outlook 中缺少的 Microsoft Teams 插件May 11, 2023 am 11:01 AM

团队在Outlook中有一个非常有用的加载项,当您在使用Outlook2013或更高版本的应用程序时安装以前的应用程序时,它会自动安装。安装这两个应用程序后,只需打开Outlook,您就可以找到预装的加载项。但是,一些用户报告了在Outlook中找不到Team插件的异常情况。修复1–重新注册DLL文件有时需要重新注册特定的Teams加载项dll文件。第1步-找到MICROSOFT.TEAMS.ADDINLOADER.DLL文件1.首先,您必须确保

如何在 Windows 10 中清除地址解析协议 (ARP) 缓存如何在 Windows 10 中清除地址解析协议 (ARP) 缓存Apr 13, 2023 pm 07:43 PM

地址解析协议 (ARP) 用于将 MAC 地址映射到 IP 地址。网络上的所有主机都有自己的 IP 地址,但网络接口卡 (NIC) 将有 MAC 地址而不是 IP 地址。ARP 是用于将 IP 地址与 MAC 地址相关联的协议。所有这些条目都被收集并放置在 ARP 缓存中。映射的地址存储在缓存中,它们通常不会造成任何损害。但是,如果条目不正确或 ARP 缓存损坏,则会出现连接问题、加载问题或错误。因此,您需要清除 ARP 缓存并修复错误。在本文中,我们将研究如何清除 ARP 缓存的不同方法。方法

如何在Mac上清除图标缓存?如何在Mac上清除图标缓存?Apr 22, 2023 pm 07:49 PM

如何在Mac上清除和重置图标缓存警告:因为您将使用终端和rm命令,所以在继续执行任何操作之前,最好使用TimeMachine或您选择的备份方法备份您的Mac。输入错误的命令可能会导致永久性数据丢失,因此请务必使用准确的语法。如果您对命令行不满意,最好完全避免这种情况。启动终端并输入以下命令并按回车键:sudorm-rfv/Library/Caches/com.apple.iconservices.store接下来,输入以下命令并按回车键:sudofind/private/var

如何修复 Microsoft Teams 错误代码 caa70004 问题如何修复 Microsoft Teams 错误代码 caa70004 问题Apr 14, 2023 am 09:25 AM

尝试在其设备上启动 Microsoft Teams 桌面客户端的用户在空白应用页面中报告了错误代码 caa70004。错误代码说:“我们很抱歉——我们遇到了问题。”以及重新启动 Microsoft Teams 以解决问题的选项。您可以尝试实施许多解决方案并再次加入会议。解决方法——1. 您应该尝试的第一件事是重新启动 Teams 应用程序。只需在错误页面上点击“重新启动”即可。

linux的header是什么意思linux的header是什么意思Jul 18, 2023 pm 03:34 PM

linux的header是指在文件或数据流中的开头部分,用于包含关于内容的元数据,通过正确地编写和使用Header文件,开发者能够更好地利用系统资源,提高代码的可读性和可维护性。

SpringBoot怎么通过Feign调用传递Header中参数SpringBoot怎么通过Feign调用传递Header中参数May 16, 2023 pm 08:38 PM

【SpringBoot】通过Feign调用传递Header中参数如何通过Feign传递Header参数问题描述我们在SpringCloud中使用Feign请求另一个服务的Api接口时,有将Header中参数传递下去的需求,如果不做特殊处理,就会将Header中的参数丢失。解决方案方案一:通过@RequestHeader(name="headerName")来传递例如:Feign定义如下@FeignClient(name="service-name")pub

如何在 Windows 11 上清理缓存:详细的带图片教程如何在 Windows 11 上清理缓存:详细的带图片教程Apr 24, 2023 pm 09:37 PM

什么是缓存?缓存(发音为ka·shay)是一种专门的高速硬件或软件组件,用于存储经常请求的数据和指令,这些数据和指令又可用于更快地加载网站、应用程序、服务和系统的其他部分。缓存使最常访问的数据随时可用。缓存文件与缓存内存不同。缓存文件是指经常需要的文件,如PNG、图标、徽标、着色器等,多个程序可能需要这些文件。这些文件存储在您的物理驱动器空间中,通常是隐藏的。另一方面,高速缓存内存是一种比主内存和/或RAM更快的内存类型。它极大地减少了数据访问时间,因为与RAM相比,它更靠近CPU并且速度

vue的缓存有几种实现方式vue的缓存有几种实现方式Dec 22, 2021 pm 06:00 PM

vue缓存数据有4种方式:1、利用localStorage,语法“localStorage.setItem(key,value)”;2、利用sessionStorage,语法“sessionStorage.setItem(key,value)”;3、安装并引用storage.js插件,利用该插件进行缓存;4、利用vuex,它是一个专为Vue.js应用程序开发的状态管理模式。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools