1<?php
2//////////////////////////////////////////////////////////////
3//   phpThumb() by James Heinrich <info@silisoftware.com>   //
4//        available at http://phpthumb.sourceforge.net      //
5//         and/or https://github.com/JamesHeinrich/phpThumb //
6//////////////////////////////////////////////////////////////
7///                                                         //
8// phpThumb.demo.object.php                                 //
9// James Heinrich <info@silisoftware.com>                   //
10//                                                          //
11// Example of how to use phpthumb.class.php as an object    //
12//                                                          //
13//////////////////////////////////////////////////////////////
14
15// Note: phpThumb.php is where the caching code is located, if
16//   you instantiate your own phpThumb() object that code is
17//   bypassed and it's up to you to handle the reading and
18//   writing of cached files, if appropriate.
19
20die('For security reasons, this demo is disabled by default. Please comment out line '.__LINE__.' in '.basename(__FILE__));
21
22require_once '../phpthumb.class.php';
23
24// create phpThumb object
25$phpThumb = new phpThumb();
26
27// create 3 sizes of thumbnail
28$thumbnail_widths = array(160, 320, 640);
29$capture_raw_data = false; // set to true to insert to database rather than render to screen or file (see below)
30foreach ($thumbnail_widths as $thumbnail_width) {
31	// this is very important when using a single object to process multiple images
32	$phpThumb->resetObject();
33
34	// set data source -- do this first, any settings must be made AFTER this call
35	$phpThumb->setSourceFilename('images/loco.jpg');  // for static demo only
36	//$phpThumb->setSourceFilename($_FILES['userfile']['tmp_name']);
37	// or $phpThumb->setSourceData($binary_image_data);
38	// or $phpThumb->setSourceImageResource($gd_image_resource);
39
40	// PLEASE NOTE:
41	// You must set any relevant config settings here. The phpThumb
42	// object mode does NOT pull any settings from phpThumb.config.php
43	//$phpThumb->setParameter('config_document_root', '/home/groups/p/ph/phpthumb/htdocs/');
44	//$phpThumb->setParameter('config_cache_directory', '/tmp/persistent/phpthumb/cache/');
45
46	// set parameters (see "URL Parameters" in phpthumb.readme.txt)
47	$phpThumb->setParameter('w', $thumbnail_width);
48	//$phpThumb->setParameter('h', 100);
49	//$phpThumb->setParameter('fltr', 'gam|1.2');
50	//$phpThumb->setParameter('fltr', 'wmi|../watermark.jpg|C|75|20|20');
51
52	// set options (see phpThumb.config.php)
53	// here you must preface each option with "config_"
54	$phpThumb->setParameter('config_output_format', 'jpeg');
55	$phpThumb->setParameter('config_imagemagick_path', '/usr/local/bin/convert');
56	//$phpThumb->setParameter('config_allow_src_above_docroot', true); // needed if you're working outside DOCUMENT_ROOT, in a temp dir for example
57
58	// generate & output thumbnail
59	$output_filename = './thumbnails/'.basename($_FILES['userfile']['name']).'_'.$thumbnail_width.'.'.$phpThumb->config_output_format;
60	if ($phpThumb->GenerateThumbnail()) { // this line is VERY important, do not remove it!
61		$output_size_x = imagesx($phpThumb->gdimg_output);
62		$output_size_y = imagesy($phpThumb->gdimg_output);
63		if ($output_filename || $capture_raw_data) {
64			if ($capture_raw_data && $phpThumb->RenderOutput()) {
65				// RenderOutput renders the thumbnail data to $phpThumb->outputImageData, not to a file or the browser
66				$mysqli->query("INSERT INTO `table` (`thumbnail`) VALUES ('".mysqli_real_escape_string($phpThumb->outputImageData)."') WHERE (`id` = '".mysqli_real_escape_string($id)."')");
67			} elseif ($phpThumb->RenderToFile($output_filename)) {
68				// do something on success
69				echo 'Successfully rendered:<br><img src="'.$output_filename.'">';
70			} else {
71				// do something with debug/error messages
72				echo 'Failed (size='.$thumbnail_width.'):<pre>'.implode("\n\n", $phpThumb->debugmessages).'</pre>';
73			}
74			$phpThumb->purgeTempFiles();
75		} else {
76			$phpThumb->OutputThumbnail();
77		}
78	} else {
79		// do something with debug/error messages
80		echo 'Failed (size='.$thumbnail_width.').<br>';
81		echo '<div style="background-color:#FFEEDD; font-weight: bold; padding: 10px;">'.$phpThumb->fatalerror.'</div>';
82		echo '<form><textarea rows="10" cols="60" wrap="off">'.htmlentities(implode("\n* ", $phpThumb->debugmessages)).'</textarea></form><hr>';
83	}
84
85}
86