qa-image.php 2.5 KB
Newer Older
Scott committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	Description: Outputs image for a specific blob at a specific size, caching as appropriate


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/


Scott committed
23
// Ensure no PHP errors are shown in the image data
Scott committed
24

Scott committed
25
@ini_set('display_errors', 0);
Scott committed
26

Scott committed
27 28 29 30 31
function qa_image_db_fail_handler()
{
	header('HTTP/1.1 500 Internal Server Error');
	qa_exit('error');
}
Scott committed
32 33


Scott committed
34
// Load the Q2A base file which sets up a bunch of crucial stuff
Scott committed
35

36
$qa_autoconnect = false;
Scott committed
37
require 'qa-base.php';
Scott committed
38

Scott committed
39
qa_report_process_stage('init_image');
Scott committed
40 41


Scott committed
42
// Retrieve the scaled image from the cache if available
Scott committed
43

Scott committed
44
require_once QA_INCLUDE_DIR . 'db/cache.php';
Scott committed
45

Scott committed
46
qa_db_connect('qa_image_db_fail_handler');
47
qa_initialize_postdb_plugins();
Scott committed
48

Scott committed
49 50 51
$blobid = qa_get('qa_blobid');
$size = (int)qa_get('qa_size');
$cachetype = 'i_' . $size;
Scott committed
52

Scott committed
53
$content = qa_db_cache_get($cachetype, $blobid); // see if we've cached the scaled down version
Scott committed
54

Scott committed
55
header('Cache-Control: max-age=2592000, public'); // allows browsers and proxies to cache images too
Scott committed
56

Scott committed
57 58 59
if (isset($content)) {
	header('Content-Type: image/jpeg');
	echo $content;
Scott committed
60

Scott committed
61 62 63 64
} else {
	require_once QA_INCLUDE_DIR . 'app/options.php';
	require_once QA_INCLUDE_DIR . 'app/blobs.php';
	require_once QA_INCLUDE_DIR . 'util/image.php';
Scott committed
65 66


Scott committed
67
	// Otherwise retrieve the raw image and scale as appropriate
Scott committed
68

Scott committed
69
	$blob = qa_read_blob($blobid);
Scott committed
70

Scott committed
71 72 73 74 75
	if (isset($blob)) {
		if ($size > 0)
			$content = qa_image_constrain_data($blob['content'], $width, $height, $size);
		else
			$content = $blob['content'];
Scott committed
76

Scott committed
77 78 79
		if (isset($content)) {
			header('Content-Type: image/jpeg');
			echo $content;
Scott committed
80

Scott committed
81 82 83
			if (strlen($content) && ($size > 0)) {
				// to prevent cache being filled with inappropriate sizes
				$cachesizes = qa_get_options(array('avatar_profile_size', 'avatar_users_size', 'avatar_q_page_q_size', 'avatar_q_page_a_size', 'avatar_q_page_c_size', 'avatar_q_list_size'));
Scott committed
84

Scott committed
85 86
				if (array_search($size, $cachesizes))
					qa_db_cache_set($cachetype, $blobid, $content);
Scott committed
87 88 89
			}
		}
	}
Scott committed
90
}
Scott committed
91

Scott committed
92
qa_db_disconnect();