Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[PHP] Gd-Lib Bilder Caching

Empfohlene Antworten

Veröffentlicht

Hallo zusammen,

ich habe eine Seite, in der ich via PHP Bilder generiere.

Mir kommt es so vor, als würden die Browser diese generierten Bilder nicht cachen...

Kann ich das irgendwie kontrollieren?

Muss ich evtl irgendeinen bestimmten Header senden, damit der Browser das ganze cached?

Gruß,

Markus

GD generiert images on-the-fly - das lässt sich aber z.b. umgehen, indem bilder bei vorhandensein aus einem verzeichnis geladen werden, andernfalls zuerst generiert und dann im cache abgelegt werden:

<?php
// set the PNG header type for the request
header("Content-Type: image/png");

// see if a width and height for the image have been provided in GET VARS
// if not, use some defaults
if ($_GET["logo_width"]){
$width = $_GET["logo_width"];
$height = $_GET["logo_height"];
}else{
$width = 466;
$height = 100;
}

// now check if an image created for of these dimensions
// is in the image cache
// if not, then we will have to make a new one

$image_path = "../image-cache/logo_".$width."x".$height.".png";

if (file_exists($image_path)) {
// send the cached image to the output buffer (browser)
readfile($image_path);
}else{
// create a new image using GD functions

// the original PNG logo image
$image = ImageCreateFromPNG('logo.png');
// the new image we will create with GD to the new dimensions
$new_image = ImageCreateTrueColor($width,$height);

// copy and re-sample the original image to make the new one
ImageCopyResampled($new_image, $image, 0, 0, 0, 0, $width, $height, 466, 100);

// send the new image to the browser
ImagePNG($new_image);
// now save a copy of the new image to the cache directory
ImagePNG($new_image, $image_path);

// destroy the image in memory to free up server resources
ImageDestroy($new_image);
}

[/PHP]

s'Amstel

Hi Amstel,

danke für die Info, aber das ist mir schon alles bewusst...

Nur kommt mir so vor, auch wenn die Datei gecached auf dem Server liegt, dass der Browser diese trotzdem nicht cached.

Der Browser cached ja die Bilder normalerweise, aber wie gesagt, wenn diese mit PHP generiert sind anscheinend nicht... Evtl hilft da nen header() ?

Wie man die Gd-Lib verwendet ist mir bewusst...

Gruß,

Markus

Hi,

Ich vermute, wenn ich den Last-Modified Header mitschicke, müsste der Browser selbst entscheiden können, ob er cached oder nicht, oder wie seht ihr das?

Das mit dem Cache-Control bringt mich glaub ich nicht weiter.

Gruß,

Markus

d.h. ich muss einen 304 Header schicken ala

header("HTTP/1.0 304 Not Modified");

?

Hi,

Danke für die Antwort!

Aktuell siehst jetzt so aus:


header('Content-Type: image/'.$thumb->img['format']);
header('HTTP/1.1 304 Not Modified');
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $cachetime));
readfile($cachepath);[/php]

Macht es jetzt überhaupt noch Sinn, noch den Last-Modified-Header mitzuschicken?

Dann kannst du auch die Vorteile von HTTP 1.1 nutzen.

Die da wären?

Gruß,

Markus

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.