qa-image.php 2.69 KB
Newer Older
Gideon Greenspan committed
1 2 3 4 5 6 7
<?php

/*
	Question2Answer (c) Gideon Greenspan

	http://www.question2answer.org/

Scott Vivian committed
8

Gideon Greenspan committed
9 10 11 12 13 14 15 16 17
	File: qa-include/qa-image.php
	Version: See define()s at top of qa-include/qa-base.php
	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.
Scott Vivian committed
18

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28 29 30
	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
*/


//	Ensure no PHP errors are shown in the image data

	@ini_set('display_errors', 0);
Scott Vivian committed
31

Gideon Greenspan committed
32 33 34 35 36
	function qa_image_db_fail_handler()
	{
		header('HTTP/1.1 500 Internal Server Error');
		qa_exit('error');
	}
Scott Vivian committed
37

Gideon Greenspan committed
38 39 40 41 42 43 44 45 46 47 48

//	Load the Q2A base file which sets up a bunch of crucial stuff

	require 'qa-base.php';

	qa_report_process_stage('init_image');


//	Retrieve the scaled image from the cache if available

	require_once QA_INCLUDE_DIR.'qa-db-cache.php';
Scott Vivian committed
49

Gideon Greenspan committed
50
	qa_db_connect('qa_image_db_fail_handler');
Scott Vivian committed
51

Gideon Greenspan committed
52 53 54
	$blobid=qa_get('qa_blobid');
	$size=(int)qa_get('qa_size');
	$cachetype='i_'.$size;
Scott Vivian committed
55

Gideon Greenspan committed
56
	$content=qa_db_cache_get($cachetype, $blobid); // see if we've cached the scaled down version
Scott Vivian committed
57

Gideon Greenspan committed
58
	header('Cache-Control: max-age=2592000, public'); // allows browsers and proxies to cache images too
Scott Vivian committed
59

Gideon Greenspan committed
60 61 62 63 64 65
	if (isset($content)) {
		header('Content-Type: image/jpeg');
		echo $content;

	} else {
		require_once QA_INCLUDE_DIR.'qa-app-options.php';
Gideon Greenspan committed
66
		require_once QA_INCLUDE_DIR.'qa-app-blobs.php';
Gideon Greenspan committed
67
		require_once QA_INCLUDE_DIR.'qa-util-image.php';
Scott Vivian committed
68

Gideon Greenspan committed
69 70

	//	Otherwise retrieve the raw image and scale as appropriate
Scott Vivian committed
71

Gideon Greenspan committed
72
		$blob=qa_read_blob($blobid);
Scott Vivian committed
73

Gideon Greenspan committed
74 75 76 77 78
		if (isset($blob)) {
			if ($size>0)
				$content=qa_image_constrain_data($blob['content'], $width, $height, $size);
			else
				$content=$blob['content'];
Scott Vivian committed
79

Gideon Greenspan committed
80 81 82 83 84 85 86
			if (isset($content)) {
				header('Content-Type: image/jpeg');
				echo $content;

				if (strlen($content) && ($size>0)) {
					$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'));
						// to prevent cache being filled with inappropriate sizes
Scott Vivian committed
87

Gideon Greenspan committed
88 89 90
					if (array_search($size, $cachesizes))
						qa_db_cache_set($cachetype, $blobid, $content);
				}
Scott Vivian committed
91
			}
Gideon Greenspan committed
92 93 94 95 96 97 98 99 100
		}
	}

	qa_db_disconnect();


/*
	Omit PHP closing tag to help avoid accidental output
*/